1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! This module computes a fixed-length fingerprint for an arbitrary long
//! message.
//!
//! Sample use cases:
//!
//! - File integrity checking
//! - Creating unique identifiers to index arbitrary long data
use ;
use ;
use secmem;
use ptr;
pub const BYTES_MIN: usize = 16;
pub const BYTES_MAX: usize = 64;
pub const BYTES: usize = 32;
pub const KEYBYTES_MIN: usize = 16;
pub const KEYBYTES_MAX: usize = 64;
pub const KEYBYTES: usize = 32;
extern "C"
/// The *hash()* function calculates a fingerprint of the message. The output
/// size can be chosen by the application.
///
/// The minimum recommended output size is *BYTES*. This size makes it
/// practically impossible for two messages to produce the same fingerprint.
///
/// But for specific use cases, the size can be any value between *BYTES_MIN*
/// (included) and *BYTES_MAX* (included). This can be specified in the
/// optional *s* argument. If *s* is not supplied the output length will be
/// *BYTES* long.
///
/// An optional key can an also be specified. A message will always have the
/// same fingerprint for a given key, but different keys used to hash the same
/// message are very likely to produce distinct fingerprints.
///
/// If no key is supplied, a message will always have the same fingerprint,
/// similar to the MD5 or SHA-1 functions for which *hash()* is a faster and
/// more secure alternative.
///
/// In particular, the key can be used to make sure that different applications
/// generate different fingerprints even if they process the same data.
///
/// The recommended key size is *KEYBYTE* bytes.
///
/// However, the key size can by any value between *KEYBYTES_MIN* (included) and
/// *KEYBYTES_MAX* (included).
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::init;
/// use sodium_sys::crypto::hash::generichash;
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Generate the hash.
/// let hash = generichash::hash(b"test", None, None).unwrap();
/// assert!(hash.len() == generichash::BYTES);
///
/// // Generate a minimum length hash
/// let shash = generichash::hash(b"test",
/// Some(generichash::BYTES_MIN),
/// None).unwrap();
/// assert!(shash.len() == generichash::BYTES_MIN);
///
/// // Generate a maximum length hash
/// let lhash = generichash::hash(b"test",
/// Some(generichash::BYTES_MAX),
/// None).unwrap();
/// assert!(lhash.len() == generichash::BYTES_MAX);
///
/// // Generate a hash with a key.
/// let key: [u8; generichash::KEYBYTES] = [0; generichash::KEYBYTES];
/// let khash = generichash::hash(b"test",
/// None,
/// Some(&key)).unwrap();
/// assert!(khash.len() == generichash::BYTES);
///
/// // Compare two hashes generated with same key.
/// let khash1 = generichash::hash(b"test",
/// None,
/// Some(&key)).unwrap();
/// assert!(khash == khash1);
/// ```
/// The *state_size()* function should be used in conjunction with
/// *utils::malloc()* to allocate the memory for the generic hash state at
/// runtime.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::{init, secmem};
/// use sodium_sys::crypto::hash::generichash;
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Initialize the hash state.
/// let state_size = generichash::state_size().unwrap();
/// let state = secmem::malloc(state_size);
/// assert!(state.len() == state_size);
/// ```
/// Initialize the sha256 multi-part hash.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::{init, secmem};
/// use sodium_sys::crypto::hash::generichash;
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Initialize the hash state.
/// let state_size = generichash::state_size().unwrap();
/// let mut state = secmem::malloc(state_size);
/// let outlen = 64;
/// let _ = generichash::init(&mut state, outlen, None).unwrap();
/// assert!(state.len() == state_size);
/// ```
/// Update the sha256 multi-part hash.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::{init, secmem};
/// use sodium_sys::crypto::hash::generichash;
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Initialize the hash state.
/// let state_size = generichash::state_size().unwrap();
/// let mut state = secmem::malloc(state_size);
/// let outlen = 64;
/// let _ = generichash::init(&mut state, outlen, None).unwrap();
///
/// // Update the hash state
/// let _ = generichash::update(&mut state, b"test").unwrap();
/// let _ = generichash::update(&mut state, b"test").unwrap();
/// ```
/// Finalize and return the sha256 multi-part hash.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::{init, secmem};
/// use sodium_sys::crypto::hash::generichash;
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Initialize the hash state.
/// let state_size = generichash::state_size().unwrap();
/// let mut state = secmem::malloc(state_size);
/// let outlen = 64;
/// let _ = generichash::init(&mut state, outlen, None).unwrap();
///
/// // Update the hash state
/// let _ = generichash::update(&mut state, b"test").unwrap();
/// let _ = generichash::update(&mut state, b"test").unwrap();
///
/// // Finalize the hash.
/// let hash = generichash::finalize(&mut state, outlen).unwrap();
/// assert!(hash.len() == outlen);
/// ```