md5.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // The original file was copied from sqlite, and was in the public domain.
  5. /*
  6. * This code implements the MD5 message-digest algorithm.
  7. * The algorithm is due to Ron Rivest. This code was
  8. * written by Colin Plumb in 1993, no copyright is claimed.
  9. * This code is in the public domain; do with it what you wish.
  10. *
  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. *
  16. * To compute the message digest of a chunk of bytes, declare an
  17. * MD5Context structure, pass it to MD5Init, call MD5Update as
  18. * needed on buffers full of bytes, and then call MD5Final, which
  19. * will fill a supplied 16-byte array with the digest.
  20. */
  21. #include "butil/md5.h"
  22. #include "butil/basictypes.h"
  23. namespace {
  24. struct Context {
  25. uint32_t buf[4];
  26. uint32_t bits[2];
  27. unsigned char in[64];
  28. };
  29. /*
  30. * Note: this code is harmless on little-endian machines.
  31. */
  32. void byteReverse(unsigned char *buf, unsigned longs) {
  33. uint32_t t;
  34. do {
  35. t = (uint32_t)((unsigned)buf[3]<<8 | buf[2]) << 16 |
  36. ((unsigned)buf[1]<<8 | buf[0]);
  37. *(uint32_t *)buf = t;
  38. buf += 4;
  39. } while (--longs);
  40. }
  41. /* The four core functions - F1 is optimized somewhat */
  42. /* #define F1(x, y, z) (x & y | ~x & z) */
  43. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  44. #define F2(x, y, z) F1(z, x, y)
  45. #define F3(x, y, z) (x ^ y ^ z)
  46. #define F4(x, y, z) (y ^ (x | ~z))
  47. /* This is the central step in the MD5 algorithm. */
  48. #define MD5STEP(f, w, x, y, z, data, s) \
  49. ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
  50. /*
  51. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  52. * reflect the addition of 16 longwords of new data. MD5Update blocks
  53. * the data and converts bytes into longwords for this routine.
  54. */
  55. void MD5Transform(uint32_t buf[4], const uint32_t in[16]) {
  56. uint32_t a, b, c, d;
  57. a = buf[0];
  58. b = buf[1];
  59. c = buf[2];
  60. d = buf[3];
  61. MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
  62. MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
  63. MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
  64. MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
  65. MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
  66. MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
  67. MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
  68. MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
  69. MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
  70. MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
  71. MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
  72. MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
  73. MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
  74. MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
  75. MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
  76. MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
  77. MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
  78. MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
  79. MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
  80. MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
  81. MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
  82. MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
  83. MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
  84. MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
  85. MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
  86. MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
  87. MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
  88. MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
  89. MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
  90. MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
  91. MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
  92. MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
  93. MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
  94. MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
  95. MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
  96. MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
  97. MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
  98. MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
  99. MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
  100. MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
  101. MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
  102. MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
  103. MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
  104. MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
  105. MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
  106. MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
  107. MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
  108. MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
  109. MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
  110. MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
  111. MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
  112. MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
  113. MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
  114. MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
  115. MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
  116. MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
  117. MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
  118. MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
  119. MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
  120. MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
  121. MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
  122. MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
  123. MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
  124. MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
  125. buf[0] += a;
  126. buf[1] += b;
  127. buf[2] += c;
  128. buf[3] += d;
  129. }
  130. } // namespace
  131. namespace butil {
  132. /*
  133. * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
  134. * initialization constants.
  135. */
  136. void MD5Init(MD5Context* context) {
  137. struct Context *ctx = (struct Context *)context;
  138. ctx->buf[0] = 0x67452301;
  139. ctx->buf[1] = 0xefcdab89;
  140. ctx->buf[2] = 0x98badcfe;
  141. ctx->buf[3] = 0x10325476;
  142. ctx->bits[0] = 0;
  143. ctx->bits[1] = 0;
  144. }
  145. /*
  146. * Update context to reflect the concatenation of another buffer full
  147. * of bytes.
  148. */
  149. void MD5Update(MD5Context* context, const StringPiece& data) {
  150. const unsigned char* inbuf = (const unsigned char*)data.data();
  151. size_t len = data.size();
  152. struct Context *ctx = (struct Context *)context;
  153. const unsigned char* buf = (const unsigned char*)inbuf;
  154. uint32_t t;
  155. /* Update bitcount */
  156. t = ctx->bits[0];
  157. if ((ctx->bits[0] = t + ((uint32_t)len << 3)) < t)
  158. ctx->bits[1]++; /* Carry from low to high */
  159. ctx->bits[1] += static_cast<uint32_t>(len >> 29);
  160. t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
  161. /* Handle any leading odd-sized chunks */
  162. if (t) {
  163. unsigned char *p = (unsigned char *)ctx->in + t;
  164. t = 64-t;
  165. if (len < t) {
  166. memcpy(p, buf, len);
  167. return;
  168. }
  169. memcpy(p, buf, t);
  170. byteReverse(ctx->in, 16);
  171. MD5Transform(ctx->buf, (uint32_t *)ctx->in);
  172. buf += t;
  173. len -= t;
  174. }
  175. /* Process data in 64-byte chunks */
  176. while (len >= 64) {
  177. memcpy(ctx->in, buf, 64);
  178. byteReverse(ctx->in, 16);
  179. MD5Transform(ctx->buf, (uint32_t *)ctx->in);
  180. buf += 64;
  181. len -= 64;
  182. }
  183. /* Handle any remaining bytes of data. */
  184. memcpy(ctx->in, buf, len);
  185. }
  186. /*
  187. * Final wrapup - pad to 64-byte boundary with the bit pattern
  188. * 1 0* (64-bit count of bits processed, MSB-first)
  189. */
  190. void MD5Final(MD5Digest* digest, MD5Context* context) {
  191. struct Context *ctx = (struct Context *)context;
  192. unsigned count;
  193. unsigned char *p;
  194. /* Compute number of bytes mod 64 */
  195. count = (ctx->bits[0] >> 3) & 0x3F;
  196. /* Set the first char of padding to 0x80. This is safe since there is
  197. always at least one byte free */
  198. p = ctx->in + count;
  199. *p++ = 0x80;
  200. /* Bytes of padding needed to make 64 bytes */
  201. count = 64 - 1 - count;
  202. /* Pad out to 56 mod 64 */
  203. if (count < 8) {
  204. /* Two lots of padding: Pad the first block to 64 bytes */
  205. memset(p, 0, count);
  206. byteReverse(ctx->in, 16);
  207. MD5Transform(ctx->buf, (uint32_t *)ctx->in);
  208. /* Now fill the next block with 56 bytes */
  209. memset(ctx->in, 0, 56);
  210. } else {
  211. /* Pad block to 56 bytes */
  212. memset(p, 0, count-8);
  213. }
  214. byteReverse(ctx->in, 14);
  215. /* Append length in bits and transform */
  216. memcpy(&ctx->in[14 * sizeof(ctx->bits[0])],
  217. &ctx->bits[0],
  218. sizeof(ctx->bits[0]));
  219. memcpy(&ctx->in[15 * sizeof(ctx->bits[1])],
  220. &ctx->bits[1],
  221. sizeof(ctx->bits[1]));
  222. MD5Transform(ctx->buf, (uint32_t *)ctx->in);
  223. byteReverse((unsigned char *)ctx->buf, 4);
  224. memcpy(digest->a, ctx->buf, 16);
  225. memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
  226. }
  227. void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) {
  228. /* MD5Final mutates the MD5Context*. Make a copy for generating the
  229. intermediate value. */
  230. MD5Context context_copy;
  231. memcpy(&context_copy, context, sizeof(context_copy));
  232. MD5Final(digest, &context_copy);
  233. }
  234. std::string MD5DigestToBase16(const MD5Digest& digest) {
  235. static char const zEncode[] = "0123456789abcdef";
  236. std::string ret;
  237. ret.resize(32);
  238. int j = 0;
  239. for (int i = 0; i < 16; i ++) {
  240. int a = digest.a[i];
  241. ret[j++] = zEncode[(a>>4)&0xf];
  242. ret[j++] = zEncode[a & 0xf];
  243. }
  244. return ret;
  245. }
  246. void MD5Sum(const void* data, size_t length, MD5Digest* digest) {
  247. MD5Context ctx;
  248. MD5Init(&ctx);
  249. MD5Update(&ctx,
  250. StringPiece(reinterpret_cast<const char*>(data), length));
  251. MD5Final(digest, &ctx);
  252. }
  253. std::string MD5String(const StringPiece& str) {
  254. MD5Digest digest;
  255. MD5Sum(str.data(), str.length(), &digest);
  256. return MD5DigestToBase16(digest);
  257. }
  258. } // namespace butil