4444//! [`hash`]: struct.Hash.html#method.hash
4545//! [`finish`]: struct.Hash.html#method.finish
4646
47- use crate :: { Error , Result } ;
4847use crate :: buffer:: Buffer ;
4948use crate :: helpers:: { AlgoHandle , Handle } ;
50- use winapi:: shared:: bcrypt:: * ;
51- use winapi:: shared:: minwindef:: { DWORD , ULONG , PUCHAR } ;
49+ use crate :: { Error , Result } ;
5250use std:: ptr:: null_mut;
51+ use winapi:: shared:: bcrypt:: * ;
52+ use winapi:: shared:: minwindef:: { DWORD , PUCHAR , ULONG } ;
5353
5454/// Hashing algorithm identifiers
5555#[ derive( Debug , Clone , Copy , PartialOrd , PartialEq ) ]
@@ -141,17 +141,19 @@ impl HashAlgorithm {
141141 let mut hash_handle = HashHandle :: new ( ) ;
142142 let mut object = Buffer :: new ( object_size) ;
143143 unsafe {
144- Error :: check (
145- BCryptCreateHash (
146- self . handle . as_ptr ( ) ,
147- hash_handle. as_mut_ptr ( ) ,
148- object. as_mut_ptr ( ) ,
149- object. len ( ) as ULONG ,
150- null_mut ( ) ,
151- 0 ,
152- 0
153- )
154- ) . map ( |_| Hash { handle : hash_handle, _object : object } )
144+ Error :: check ( BCryptCreateHash (
145+ self . handle . as_ptr ( ) ,
146+ hash_handle. as_mut_ptr ( ) ,
147+ object. as_mut_ptr ( ) ,
148+ object. len ( ) as ULONG ,
149+ null_mut ( ) ,
150+ 0 ,
151+ 0 ,
152+ ) )
153+ . map ( |_| Hash {
154+ handle : hash_handle,
155+ _object : object,
156+ } )
155157 }
156158 }
157159}
@@ -169,7 +171,9 @@ impl HashHandle {
169171impl Drop for HashHandle {
170172 fn drop ( & mut self ) {
171173 if !self . handle . is_null ( ) {
172- unsafe { BCryptDestroyHash ( self . handle ) ; }
174+ unsafe {
175+ BCryptDestroyHash ( self . handle ) ;
176+ }
173177 }
174178 }
175179}
@@ -212,7 +216,7 @@ impl Hash {
212216 self . handle . as_ptr ( ) ,
213217 data. as_ptr ( ) as PUCHAR ,
214218 data. len ( ) as ULONG ,
215- 0
219+ 0 ,
216220 ) )
217221 }
218222 }
@@ -242,14 +246,13 @@ impl Hash {
242246 let mut result = Buffer :: new ( hash_size) ;
243247
244248 unsafe {
245- Error :: check (
246- BCryptFinishHash (
247- self . handle . as_ptr ( ) ,
248- result. as_mut_ptr ( ) ,
249- result. len ( ) as ULONG ,
250- 0
251- )
252- ) . map ( |_| result)
249+ Error :: check ( BCryptFinishHash (
250+ self . handle . as_ptr ( ) ,
251+ result. as_mut_ptr ( ) ,
252+ result. len ( ) as ULONG ,
253+ 0 ,
254+ ) )
255+ . map ( |_| result)
253256 }
254257 }
255258
@@ -266,7 +269,9 @@ impl Hash {
266269 /// assert_eq!(hash_size, 32);
267270 /// ```
268271 pub fn hash_size ( & self ) -> Result < usize > {
269- self . handle . get_property :: < DWORD > ( BCRYPT_HASH_LENGTH ) . map ( |hash_size| hash_size as usize )
272+ self . handle
273+ . get_property :: < DWORD > ( BCRYPT_HASH_LENGTH )
274+ . map ( |hash_size| hash_size as usize )
270275 }
271276}
272277
@@ -278,71 +283,92 @@ mod tests {
278283
279284 #[ test]
280285 fn sha1 ( ) {
281- check_hash ( HashAlgorithmId :: Sha1 , DATA . as_bytes ( ) , & [
282- 0x2B , 0x44 , 0x89 , 0x60 , 0x6A , 0x23 , 0xFB , 0x31 ,
283- 0xFC , 0xDC , 0x84 , 0x9F , 0xA7 , 0xE5 , 0x77 , 0xBA ,
284- 0x90 , 0xF6 , 0xD3 , 0x9A ,
285- ] )
286+ check_hash (
287+ HashAlgorithmId :: Sha1 ,
288+ DATA . as_bytes ( ) ,
289+ & [
290+ 0x2B , 0x44 , 0x89 , 0x60 , 0x6A , 0x23 , 0xFB , 0x31 , 0xFC , 0xDC , 0x84 , 0x9F , 0xA7 , 0xE5 ,
291+ 0x77 , 0xBA , 0x90 , 0xF6 , 0xD3 , 0x9A ,
292+ ] ,
293+ )
286294 }
287295
288296 #[ test]
289297 fn sha256 ( ) {
290- check_hash ( HashAlgorithmId :: Sha256 , DATA . as_bytes ( ) , & [
291- 0x0E , 0xA3 , 0x7C , 0x24 , 0x3F , 0x60 , 0x97 , 0x4B ,
292- 0x0D , 0x54 , 0xC6 , 0xB2 , 0xD7 , 0x6C , 0xEC , 0xE3 ,
293- 0xF4 , 0xC7 , 0x42 , 0x49 , 0x2C , 0xCE , 0x48 , 0xEA ,
294- 0xF8 , 0x1F , 0x35 , 0x79 , 0x31 , 0xD6 , 0xD6 , 0x9E ,
295- ] )
298+ check_hash (
299+ HashAlgorithmId :: Sha256 ,
300+ DATA . as_bytes ( ) ,
301+ & [
302+ 0x0E , 0xA3 , 0x7C , 0x24 , 0x3F , 0x60 , 0x97 , 0x4B , 0x0D , 0x54 , 0xC6 , 0xB2 , 0xD7 , 0x6C ,
303+ 0xEC , 0xE3 , 0xF4 , 0xC7 , 0x42 , 0x49 , 0x2C , 0xCE , 0x48 , 0xEA , 0xF8 , 0x1F , 0x35 , 0x79 ,
304+ 0x31 , 0xD6 , 0xD6 , 0x9E ,
305+ ] ,
306+ )
296307 }
297308
298309 #[ test]
299310 fn sha384 ( ) {
300- check_hash ( HashAlgorithmId :: Sha384 , DATA . as_bytes ( ) , & [
301- 0x2A , 0x10 , 0x60 , 0x89 , 0x6A , 0xCB , 0xA9 , 0xFA ,
302- 0x37 , 0x11 , 0xBF , 0x10 , 0x9E , 0x90 , 0x24 , 0xEA ,
303- 0x19 , 0xF5 , 0xFC , 0x33 , 0xAF , 0x0F , 0x47 , 0x15 ,
304- 0xC3 , 0xE9 , 0xD8 , 0x63 , 0xB3 , 0x24 , 0xA5 , 0x08 ,
305- 0x9F , 0xAB , 0x95 , 0x36 , 0xB2 , 0xAC , 0x10 , 0xF6 ,
306- 0xC1 , 0xE7 , 0x31 , 0x03 , 0x09 , 0x54 , 0x18 , 0x41 ,
307- ] )
311+ check_hash (
312+ HashAlgorithmId :: Sha384 ,
313+ DATA . as_bytes ( ) ,
314+ & [
315+ 0x2A , 0x10 , 0x60 , 0x89 , 0x6A , 0xCB , 0xA9 , 0xFA , 0x37 , 0x11 , 0xBF , 0x10 , 0x9E , 0x90 ,
316+ 0x24 , 0xEA , 0x19 , 0xF5 , 0xFC , 0x33 , 0xAF , 0x0F , 0x47 , 0x15 , 0xC3 , 0xE9 , 0xD8 , 0x63 ,
317+ 0xB3 , 0x24 , 0xA5 , 0x08 , 0x9F , 0xAB , 0x95 , 0x36 , 0xB2 , 0xAC , 0x10 , 0xF6 , 0xC1 , 0xE7 ,
318+ 0x31 , 0x03 , 0x09 , 0x54 , 0x18 , 0x41 ,
319+ ] ,
320+ )
308321 }
309322
310323 #[ test]
311324 fn sha512 ( ) {
312- check_hash ( HashAlgorithmId :: Sha512 , DATA . as_bytes ( ) , & [
313- 0x39 , 0x50 , 0xAC , 0xCD , 0xFE , 0xF7 , 0x46 , 0x20 ,
314- 0x71 , 0x42 , 0x78 , 0x76 , 0x5B , 0xBD , 0xCE , 0x04 ,
315- 0xD4 , 0x57 , 0x90 , 0x4B , 0x7C , 0xEA , 0x86 , 0x31 ,
316- 0x39 , 0x6C , 0xBA , 0x6D , 0x8B , 0xCE , 0xFC , 0xE0 ,
317- 0x30 , 0x8F , 0xC4 , 0x7C , 0xFB , 0x88 , 0x5B , 0xC8 ,
318- 0x9E , 0xBD , 0xF4 , 0xFF , 0xA6 , 0xF9 , 0x8F , 0xC8 ,
319- 0x51 , 0x05 , 0x54 , 0x7C , 0xBD , 0xDF , 0x56 , 0x57 ,
320- 0xB6 , 0xAD , 0xBD , 0xDD , 0xA3 , 0x8C , 0xB9 , 0xB5 ,
321- ] )
325+ check_hash (
326+ HashAlgorithmId :: Sha512 ,
327+ DATA . as_bytes ( ) ,
328+ & [
329+ 0x39 , 0x50 , 0xAC , 0xCD , 0xFE , 0xF7 , 0x46 , 0x20 , 0x71 , 0x42 , 0x78 , 0x76 , 0x5B , 0xBD ,
330+ 0xCE , 0x04 , 0xD4 , 0x57 , 0x90 , 0x4B , 0x7C , 0xEA , 0x86 , 0x31 , 0x39 , 0x6C , 0xBA , 0x6D ,
331+ 0x8B , 0xCE , 0xFC , 0xE0 , 0x30 , 0x8F , 0xC4 , 0x7C , 0xFB , 0x88 , 0x5B , 0xC8 , 0x9E , 0xBD ,
332+ 0xF4 , 0xFF , 0xA6 , 0xF9 , 0x8F , 0xC8 , 0x51 , 0x05 , 0x54 , 0x7C , 0xBD , 0xDF , 0x56 , 0x57 ,
333+ 0xB6 , 0xAD , 0xBD , 0xDD , 0xA3 , 0x8C , 0xB9 , 0xB5 ,
334+ ] ,
335+ )
322336 }
323337
324338 #[ test]
325339 fn md2 ( ) {
326- check_hash ( HashAlgorithmId :: Md2 , DATA . as_bytes ( ) , & [
327- 0x08 , 0x18 , 0x53 , 0xA0 , 0x5C , 0x1F , 0x58 , 0xC6 ,
328- 0xED , 0x43 , 0x46 , 0x4C , 0x79 , 0x7D , 0x65 , 0x26 ,
329- ] )
340+ check_hash (
341+ HashAlgorithmId :: Md2 ,
342+ DATA . as_bytes ( ) ,
343+ & [
344+ 0x08 , 0x18 , 0x53 , 0xA0 , 0x5C , 0x1F , 0x58 , 0xC6 , 0xED , 0x43 , 0x46 , 0x4C , 0x79 , 0x7D ,
345+ 0x65 , 0x26 ,
346+ ] ,
347+ )
330348 }
331349
332350 #[ test]
333351 fn md4 ( ) {
334- check_hash ( HashAlgorithmId :: Md4 , DATA . as_bytes ( ) , & [
335- 0x24 , 0x3C , 0xDA , 0xF5 , 0x91 , 0x4A , 0xE8 , 0x70 ,
336- 0x91 , 0xC7 , 0x13 , 0xB5 , 0xFA , 0x9F , 0xA7 , 0x98 ,
337- ] )
352+ check_hash (
353+ HashAlgorithmId :: Md4 ,
354+ DATA . as_bytes ( ) ,
355+ & [
356+ 0x24 , 0x3C , 0xDA , 0xF5 , 0x91 , 0x4A , 0xE8 , 0x70 , 0x91 , 0xC7 , 0x13 , 0xB5 , 0xFA , 0x9F ,
357+ 0xA7 , 0x98 ,
358+ ] ,
359+ )
338360 }
339361
340362 #[ test]
341363 fn md5 ( ) {
342- check_hash ( HashAlgorithmId :: Md5 , DATA . as_bytes ( ) , & [
343- 0xE8 , 0x89 , 0xD8 , 0x2D , 0xD1 , 0x11 , 0xD6 , 0x31 ,
344- 0x5D , 0x7B , 0x1E , 0xDC , 0xE2 , 0xB1 , 0xB3 , 0x0F ,
345- ] )
364+ check_hash (
365+ HashAlgorithmId :: Md5 ,
366+ DATA . as_bytes ( ) ,
367+ & [
368+ 0xE8 , 0x89 , 0xD8 , 0x2D , 0xD1 , 0x11 , 0xD6 , 0x31 , 0x5D , 0x7B , 0x1E , 0xDC , 0xE2 , 0xB1 ,
369+ 0xB3 , 0x0F ,
370+ ] ,
371+ )
346372 }
347373
348374 fn check_hash ( algo_id : HashAlgorithmId , data : & [ u8 ] , expected_hash : & [ u8 ] ) {
@@ -355,4 +381,4 @@ mod tests {
355381 assert_eq ! ( hash_size, expected_hash. len( ) ) ;
356382 assert_eq ! ( result. as_slice( ) , expected_hash) ;
357383 }
358- }
384+ }
0 commit comments