MD5.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "../precompiled.h"
  2. #pragma hdrstop
  3. /*
  4. MD5 Message Digest Algorithm. (RFC1321)
  5. */
  6. /*
  7. This code implements the MD5 message-digest algorithm.
  8. The algorithm is due to Ron Rivest. This code was
  9. written by Colin Plumb in 1993, no copyright is claimed.
  10. This code is in the public domain; do with it what you wish.
  11. Equivalent code is available from RSA Data Security, Inc.
  12. This code has been tested against that, and is equivalent,
  13. except that you don't need to include two pages of legalese
  14. with every copy.
  15. To compute the message digest of a chunk of bytes, declare an
  16. MD5Context structure, pass it to MD5Init, call MD5Update as
  17. needed on buffers full of bytes, and then call MD5Final, which
  18. will fill a supplied 16-byte array with the digest.
  19. */
  20. /* MD5 context. */
  21. typedef struct
  22. {
  23. unsigned int state[4];
  24. unsigned int bits[2];
  25. unsigned char in[64];
  26. } MD5_CTX;
  27. /* The four core functions - F1 is optimized somewhat */
  28. /* #define F1(x, y, z) (x & y | ~x & z) */
  29. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  30. #define F2(x, y, z) F1(z, x, y)
  31. #define F3(x, y, z) (x ^ y ^ z)
  32. #define F4(x, y, z) (y ^ (x | ~z))
  33. /* This is the central step in the MD5 algorithm. */
  34. #define MD5STEP(f, w, x, y, z, data, s) ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
  35. /*
  36. =================
  37. MD5_Transform
  38. The core of the MD5 algorithm, this alters an existing MD5 hash to
  39. reflect the addition of 16 longwords of new data. MD5Update blocks
  40. the data and converts bytes into longwords for this routine.
  41. =================
  42. */
  43. void MD5_Transform( unsigned int state[4], unsigned int in[16] ) {
  44. register unsigned int a, b, c, d;
  45. a = state[0];
  46. b = state[1];
  47. c = state[2];
  48. d = state[3];
  49. LittleRevBytes( in, sizeof(unsigned int), 16 );
  50. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  51. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  52. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  53. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  54. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  55. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  56. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  57. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  58. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  59. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  60. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  61. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  62. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  63. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  64. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  65. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  66. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  67. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  68. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  69. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  70. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  71. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  72. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  73. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  74. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  75. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  76. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  77. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  78. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  79. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  80. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  81. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  82. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  83. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  84. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  85. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  86. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  87. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  88. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  89. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  90. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  91. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  92. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  93. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  94. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  95. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  96. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  97. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  98. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  99. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  100. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  101. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  102. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  103. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  104. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  105. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  106. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  107. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  108. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  109. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  110. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  111. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  112. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  113. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  114. LittleRevBytes( in, sizeof(unsigned int), 16 );
  115. state[0] += a;
  116. state[1] += b;
  117. state[2] += c;
  118. state[3] += d;
  119. }
  120. /*
  121. ==================
  122. MD5_Init
  123. MD5 initialization. Begins an MD5 operation, writing a new context.
  124. ==================
  125. */
  126. void MD5_Init( MD5_CTX *ctx ) {
  127. ctx->state[0] = 0x67452301;
  128. ctx->state[1] = 0xefcdab89;
  129. ctx->state[2] = 0x98badcfe;
  130. ctx->state[3] = 0x10325476;
  131. ctx->bits[0] = 0;
  132. ctx->bits[1] = 0;
  133. }
  134. /*
  135. ===================
  136. MD5_Update
  137. MD5 block update operation. Continues an MD5 message-digest operation,
  138. processing another message block, and updating the context.
  139. ===================
  140. */
  141. void MD5_Update( MD5_CTX *ctx, unsigned char const *buf, unsigned int len ) {
  142. unsigned int t;
  143. /* Update bitcount */
  144. t = ctx->bits[0];
  145. if ( ( ctx->bits[0] = t + ( (unsigned int) len << 3 ) ) < t ) {
  146. ctx->bits[1]++; /* Carry from low to high */
  147. }
  148. ctx->bits[1] += len >> 29;
  149. t = ( t >> 3 ) & 0x3f; /* Bytes already in shsInfo->data */
  150. /* Handle any leading odd-sized chunks */
  151. if ( t ) {
  152. unsigned char *p = (unsigned char *) ctx->in + t;
  153. t = 64 - t;
  154. if ( len < t ) {
  155. memcpy( p, buf, len );
  156. return;
  157. }
  158. memcpy( p, buf, t );
  159. MD5_Transform( ctx->state, (unsigned int *) ctx->in );
  160. buf += t;
  161. len -= t;
  162. }
  163. /* Process data in 64-byte chunks */
  164. while( len >= 64 ) {
  165. memcpy( ctx->in, buf, 64 );
  166. MD5_Transform( ctx->state, (unsigned int *) ctx->in );
  167. buf += 64;
  168. len -= 64;
  169. }
  170. /* Handle any remaining bytes of data. */
  171. memcpy( ctx->in, buf, len );
  172. }
  173. /*
  174. ===============
  175. MD5_Final
  176. MD5 finalization. Ends an MD5 message-digest operation,
  177. writing the message digest and zeroizing the context.
  178. ===============
  179. */
  180. void MD5_Final( MD5_CTX *ctx, unsigned char digest[16] ) {
  181. unsigned count;
  182. unsigned char *p;
  183. /* Compute number of bytes mod 64 */
  184. count = ( ctx->bits[0] >> 3 ) & 0x3F;
  185. /* Set the first char of padding to 0x80. This is safe since there is
  186. always at least one byte free */
  187. p = ctx->in + count;
  188. *p++ = 0x80;
  189. /* Bytes of padding needed to make 64 bytes */
  190. count = 64 - 1 - count;
  191. /* Pad out to 56 mod 64 */
  192. if ( count < 8 ) {
  193. /* Two lots of padding: Pad the first block to 64 bytes */
  194. memset( p, 0, count );
  195. MD5_Transform( ctx->state, (unsigned int *) ctx->in );
  196. /* Now fill the next block with 56 bytes */
  197. memset( ctx->in, 0, 56 );
  198. } else {
  199. /* Pad block to 56 bytes */
  200. memset( p, 0, count - 8 );
  201. }
  202. /* Append length in bits and transform */
  203. unsigned int val0 = ctx->bits[0];
  204. unsigned int val1 = ctx->bits[1];
  205. ((unsigned int *) ctx->in)[14] = LittleLong( val0 );
  206. ((unsigned int *) ctx->in)[15] = LittleLong( val1 );
  207. MD5_Transform( ctx->state, (unsigned int *) ctx->in );
  208. memcpy( digest, ctx->state, 16 );
  209. memset( ctx, 0, sizeof( ctx ) ); /* In case it's sensitive */
  210. }
  211. /*
  212. ===============
  213. MD5_BlockChecksum
  214. ===============
  215. */
  216. unsigned long MD5_BlockChecksum( const void *data, int length ) {
  217. unsigned long digest[4];
  218. unsigned long val;
  219. MD5_CTX ctx;
  220. MD5_Init( &ctx );
  221. MD5_Update( &ctx, (unsigned char *)data, length );
  222. MD5_Final( &ctx, (unsigned char *)digest );
  223. val = digest[0] ^ digest[1] ^ digest[2] ^ digest[3];
  224. return val;
  225. }