3434 that follows (in reverse chronological order):
3535
3636 2000-07-03 lpd Patched to eliminate warnings about "constant is
37- unsigned in ANSI C, signed in traditional";
38- made test program self-checking.
37+ unsigned in ANSI C, signed in traditional";
38+ made test program self-checking.
3939 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
4040 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
4141 1999-05-03 lpd Original version.
6464main (void )
6565{
6666 static const char * const test [7 * 2 ] = {
67- "" , "d41d8cd98f00b204e9800998ecf8427e" ,
68- "a" , "0cc175b9c0f1b6a831c399e269772661" ,
69- "abc" , "900150983cd24fb0d6963f7d28e17f72" ,
70- "message digest" , "f96b697d7cb7938d525a2f31aaf161d0" ,
71- "abcdefghijklmnopqrstuvwxyz" , "c3fcd3d76192e4007dfb496cca67e13b" ,
72- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ,
73- "d174ab98d277d9f5a5611c2c9f419d9f" ,
74- "12345678901234567890123456789012345678901234567890123456789012345678901234567890" , "57edf4a22be3c955ac49da2e2107b67a"
67+ "" , "d41d8cd98f00b204e9800998ecf8427e" ,
68+ "a" , "0cc175b9c0f1b6a831c399e269772661" ,
69+ "abc" , "900150983cd24fb0d6963f7d28e17f72" ,
70+ "message digest" , "f96b697d7cb7938d525a2f31aaf161d0" ,
71+ "abcdefghijklmnopqrstuvwxyz" , "c3fcd3d76192e4007dfb496cca67e13b" ,
72+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ,
73+ "d174ab98d277d9f5a5611c2c9f419d9f" ,
74+ "12345678901234567890123456789012345678901234567890123456789012345678901234567890" , "57edf4a22be3c955ac49da2e2107b67a"
7575 };
7676 int i ;
7777
7878 for (i = 0 ; i < 7 * 2 ; i += 2 ) {
79- MD5_CTX state ;
80- uint8_t digest [16 ];
81- char hex_output [16 * 2 + 1 ];
82- int di ;
83-
84- MD5_Init (& state );
85- MD5_Update (& state , (const uint8_t * )test [i ], strlen (test [i ]));
86- MD5_Final (digest , & state );
87- printf ("MD5 (\"%s\") = " , test [i ]);
88- for (di = 0 ; di < 16 ; ++ di )
89- sprintf (hex_output + di * 2 , "%02x" , digest [di ]);
90- puts (hex_output );
91- if (strcmp (hex_output , test [i + 1 ]))
92- printf ("**** ERROR, should be: %s\n" , test [i + 1 ]);
79+ MD5_CTX state ;
80+ uint8_t digest [16 ];
81+ char hex_output [16 * 2 + 1 ];
82+ int di ;
83+
84+ MD5_Init (& state );
85+ MD5_Update (& state , (const uint8_t * )test [i ], strlen (test [i ]));
86+ MD5_Final (digest , & state );
87+ printf ("MD5 (\"%s\") = " , test [i ]);
88+ for (di = 0 ; di < 16 ; ++ di )
89+ sprintf (hex_output + di * 2 , "%02x" , digest [di ]);
90+ puts (hex_output );
91+ if (strcmp (hex_output , test [i + 1 ]))
92+ printf ("**** ERROR, should be: %s\n" , test [i + 1 ]);
9393 }
9494 return 0 ;
9595}
@@ -106,18 +106,18 @@ main(void)
106106{
107107 int i ;
108108 for (i = 1 ; i <= 64 ; ++ i ) {
109- unsigned long v = (unsigned long )(4294967296.0 * fabs (sin ((double )i )));
110-
111- /*
112- * The following nonsense is only to avoid compiler warnings about
113- * "integer constant is unsigned in ANSI C, signed with -traditional".
114- */
115- if (v >> 31 ) {
116- printf ("#define T%d /* 0x%08lx */ (T_MASK ^ 0x%08lx)\n" , i ,
117- v , (unsigned long )(unsigned int )(~v ));
118- } else {
119- printf ("#define T%d 0x%08lx\n" , i , v );
120- }
109+ unsigned long v = (unsigned long )(4294967296.0 * fabs (sin ((double )i )));
110+
111+ /*
112+ * The following nonsense is only to avoid compiler warnings about
113+ * "integer constant is unsigned in ANSI C, signed with -traditional".
114+ */
115+ if (v >> 31 ) {
116+ printf ("#define T%d /* 0x%08lx */ (T_MASK ^ 0x%08lx)\n" , i ,
117+ v , (unsigned long )(unsigned int )(~v ));
118+ } else {
119+ printf ("#define T%d 0x%08lx\n" , i , v );
120+ }
121121 }
122122 return 0 ;
123123}
@@ -199,8 +199,8 @@ static void
199199md5_process (MD5_CTX * pms , const uint8_t * data /*[64]*/ )
200200{
201201 uint32_t
202- a = pms -> state [0 ], b = pms -> state [1 ],
203- c = pms -> state [2 ], d = pms -> state [3 ];
202+ a = pms -> state [0 ], b = pms -> state [1 ],
203+ c = pms -> state [2 ], d = pms -> state [3 ];
204204 uint32_t t ;
205205
206206#ifdef WORDS_BIGENDIAN
@@ -214,7 +214,7 @@ md5_process(MD5_CTX *pms, const uint8_t *data /*[64]*/)
214214 int i ;
215215
216216 for (i = 0 ; i < 16 ; ++ i , xp += 4 )
217- X [i ] = xp [0 ] + (xp [1 ] << 8 ) + (xp [2 ] << 16 ) + (xp [3 ] << 24 );
217+ X [i ] = xp [0 ] + (xp [1 ] << 8 ) + (xp [2 ] << 16 ) + (xp [3 ] << 24 );
218218
219219#else
220220
@@ -226,12 +226,12 @@ md5_process(MD5_CTX *pms, const uint8_t *data /*[64]*/)
226226 const uint32_t * X ;
227227
228228 if (!(((uintptr_t )data ) & 3 )) {
229- /* data are properly aligned */
230- X = (const uint32_t * )data ;
229+ /* data are properly aligned */
230+ X = (const uint32_t * )data ;
231231 } else {
232- /* not aligned */
233- memcpy (xbuf , data , 64 );
234- X = xbuf ;
232+ /* not aligned */
233+ memcpy (xbuf , data , 64 );
234+ X = xbuf ;
235235 }
236236#endif
237237
@@ -370,55 +370,55 @@ MD5_Update(MD5_CTX *pms, const uint8_t *data, size_t nbytes)
370370 uint32_t nbits = (uint32_t )(nbytes << 3 );
371371
372372 if (nbytes == 0 )
373- return ;
373+ return ;
374374
375375 /* Update the message length. */
376376 pms -> count [1 ] += nbytes >> 29 ;
377377 pms -> count [0 ] += nbits ;
378378 if (pms -> count [0 ] < nbits )
379- pms -> count [1 ]++ ;
379+ pms -> count [1 ]++ ;
380380
381381 /* Process an initial partial block. */
382382 if (offset ) {
383- size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes );
384-
385- memcpy (pms -> buffer + offset , p , copy );
386- if (offset + copy < 64 )
387- return ;
388- p += copy ;
389- left -= copy ;
390- md5_process (pms , pms -> buffer );
383+ size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes );
384+
385+ memcpy (pms -> buffer + offset , p , copy );
386+ if (offset + copy < 64 )
387+ return ;
388+ p += copy ;
389+ left -= copy ;
390+ md5_process (pms , pms -> buffer );
391391 }
392392
393393 /* Process full blocks. */
394394 for (; left >= 64 ; p += 64 , left -= 64 )
395- md5_process (pms , p );
395+ md5_process (pms , p );
396396
397397 /* Process a final partial block. */
398398 if (left )
399- memcpy (pms -> buffer , p , left );
399+ memcpy (pms -> buffer , p , left );
400400}
401401
402402int
403403MD5_Finish (MD5_CTX * pms , uint8_t * digest )
404404{
405405 static const uint8_t pad [64 ] = {
406- 0x80 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
407- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
408- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
409- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
406+ 0x80 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
407+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
408+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
409+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
410410 };
411411 uint8_t data [8 ];
412412 size_t i ;
413413
414414 /* Save the length before padding. */
415415 for (i = 0 ; i < 8 ; ++ i )
416- data [i ] = (uint8_t )(pms -> count [i >> 2 ] >> ((i & 3 ) << 3 ));
416+ data [i ] = (uint8_t )(pms -> count [i >> 2 ] >> ((i & 3 ) << 3 ));
417417 /* Pad to 56 bytes mod 64. */
418418 MD5_Update (pms , pad , ((55 - (pms -> count [0 ] >> 3 )) & 63 ) + 1 );
419419 /* Append the length. */
420420 MD5_Update (pms , data , 8 );
421421 for (i = 0 ; i < 16 ; ++ i )
422- digest [i ] = (uint8_t )(pms -> state [i >> 2 ] >> ((i & 3 ) << 3 ));
422+ digest [i ] = (uint8_t )(pms -> state [i >> 2 ] >> ((i & 3 ) << 3 ));
423423 return 1 ;
424424}
0 commit comments