MD5.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "../pch/pch.h"
  2. #include "MD5.h"
  3. #include <stdio.h>
  4. // Constants for MD5Transform routine.
  5. #define S11 7
  6. #define S12 12
  7. #define S13 17
  8. #define S14 22
  9. #define S21 5
  10. #define S22 9
  11. #define S23 14
  12. #define S24 20
  13. #define S31 4
  14. #define S32 11
  15. #define S33 16
  16. #define S34 23
  17. #define S41 6
  18. #define S42 10
  19. #define S43 15
  20. #define S44 21
  21. ///////////////////////////////////////////////
  22. // F, G, H and I are basic MD5 functions.
  23. inline MD5::uint4 MD5::F(uint4 x, uint4 y, uint4 z)
  24. {
  25. return x & y | ~x & z;
  26. }
  27. inline MD5::uint4 MD5::G(uint4 x, uint4 y, uint4 z)
  28. {
  29. return x & z | y & ~z;
  30. }
  31. inline MD5::uint4 MD5::H(uint4 x, uint4 y, uint4 z)
  32. {
  33. return x ^ y ^ z;
  34. }
  35. inline MD5::uint4 MD5::I(uint4 x, uint4 y, uint4 z)
  36. {
  37. return y ^ (x | ~z);
  38. }
  39. // rotate_left rotates x left n bits.
  40. inline MD5::uint4 MD5::rotate_left(uint4 x, int n)
  41. {
  42. return (x << n) | (x >> (32 - n));
  43. }
  44. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  45. // Rotation is separate from addition to prevent recomputation.
  46. inline void MD5::FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
  47. {
  48. a = rotate_left(a + F(b, c, d) + x + ac, s) + b;
  49. }
  50. inline void MD5::GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
  51. {
  52. a = rotate_left(a + G(b, c, d) + x + ac, s) + b;
  53. }
  54. inline void MD5::HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
  55. {
  56. a = rotate_left(a + H(b, c, d) + x + ac, s) + b;
  57. }
  58. inline void MD5::II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac)
  59. {
  60. a = rotate_left(a + I(b, c, d) + x + ac, s) + b;
  61. }
  62. //////////////////////////////////////////////
  63. // default ctor, just initailize
  64. MD5::MD5()
  65. {
  66. init();
  67. }
  68. //////////////////////////////////////////////
  69. // nifty shortcut ctor, compute MD5 for string and finalize it right away
  70. MD5::MD5(const std::string &text)
  71. {
  72. init();
  73. update(text.c_str(), text.length());
  74. finalize();
  75. }
  76. //////////////////////////////
  77. void MD5::init()
  78. {
  79. finalized = false;
  80. count[0] = 0;
  81. count[1] = 0;
  82. // load magic initialization constants.
  83. state[0] = 0x67452301;
  84. state[1] = 0xefcdab89;
  85. state[2] = 0x98badcfe;
  86. state[3] = 0x10325476;
  87. }
  88. //////////////////////////////
  89. // decodes input (unsigned char) into output (uint4). Assumes len is a multiple of 4.
  90. void MD5::decode(uint4 output[], const uint1 input[], size_type len)
  91. {
  92. for(unsigned int i = 0, j = 0; j < len; i++, j += 4)
  93. output[i] = ((uint4)input[j]) | (((uint4)input[j + 1]) << 8) |
  94. (((uint4)input[j + 2]) << 16) | (((uint4)input[j + 3]) << 24);
  95. }
  96. //////////////////////////////
  97. // encodes input (uint4) into output (unsigned char). Assumes len is
  98. // a multiple of 4.
  99. void MD5::encode(uint1 output[], const uint4 input[], size_type len)
  100. {
  101. for(size_type i = 0, j = 0; j < len; i++, j += 4)
  102. {
  103. output[j] = input[i] & 0xff;
  104. output[j + 1] = (input[i] >> 8) & 0xff;
  105. output[j + 2] = (input[i] >> 16) & 0xff;
  106. output[j + 3] = (input[i] >> 24) & 0xff;
  107. }
  108. }
  109. //////////////////////////////
  110. // apply MD5 algo on a block
  111. void MD5::transform(const uint1 block[blocksize])
  112. {
  113. uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  114. decode(x, block, blocksize);
  115. /* Round 1 */
  116. FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
  117. FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
  118. FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
  119. FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
  120. FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
  121. FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
  122. FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
  123. FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
  124. FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
  125. FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
  126. FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  127. FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  128. FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  129. FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  130. FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  131. FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  132. /* Round 2 */
  133. GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
  134. GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
  135. GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  136. GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
  137. GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
  138. GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  139. GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  140. GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
  141. GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
  142. GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  143. GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
  144. GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
  145. GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  146. GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
  147. GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
  148. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  149. /* Round 3 */
  150. HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
  151. HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
  152. HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  153. HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  154. HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
  155. HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
  156. HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
  157. HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  158. HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  159. HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
  160. HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
  161. HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
  162. HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
  163. HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  164. HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  165. HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
  166. /* Round 4 */
  167. II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
  168. II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
  169. II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  170. II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
  171. II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  172. II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
  173. II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  174. II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
  175. II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
  176. II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  177. II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
  178. II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  179. II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
  180. II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  181. II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
  182. II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
  183. state[0] += a;
  184. state[1] += b;
  185. state[2] += c;
  186. state[3] += d;
  187. // Zeroize sensitive information.
  188. memset(x, 0, sizeof x);
  189. }
  190. //////////////////////////////
  191. // MD5 block update operation. Continues an MD5 message-digest
  192. // operation, processing another message block
  193. void MD5::update(const unsigned char input[], size_type length)
  194. {
  195. // compute number of bytes mod 64
  196. size_type index = count[0] / 8 % blocksize;
  197. // Update number of bits
  198. if((count[0] += (length << 3)) < (length << 3))
  199. {
  200. count[1]++;
  201. }
  202. count[1] += (length >> 29);
  203. // number of bytes we need to fill in buffer
  204. size_type firstpart = 64 - index;
  205. size_type i;
  206. // transform as many times as possible.
  207. if(length >= firstpart)
  208. {
  209. // fill buffer first, transform
  210. memcpy(&buffer[index], input, firstpart);
  211. transform(buffer);
  212. // transform chunks of blocksize (64 bytes)
  213. for(i = firstpart; i + blocksize <= length; i += blocksize)
  214. {
  215. transform(&input[i]);
  216. }
  217. index = 0;
  218. }
  219. else
  220. {
  221. i = 0;
  222. }
  223. // buffer remaining input
  224. memcpy(&buffer[index], &input[i], length - i);
  225. }
  226. //////////////////////////////
  227. // for convenience provide a verson with signed char
  228. void MD5::update(const char input[], size_type length)
  229. {
  230. update((const unsigned char*)input, length);
  231. }
  232. //////////////////////////////
  233. // MD5 finalization. Ends an MD5 message-digest operation, writing the
  234. // the message digest and zeroizing the context.
  235. MD5& MD5::finalize()
  236. {
  237. static unsigned char padding[64] =
  238. {
  239. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  240. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  241. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  242. };
  243. if(!finalized)
  244. {
  245. // Save number of bits
  246. unsigned char bits[8];
  247. encode(bits, count, 8);
  248. // pad out to 56 mod 64.
  249. size_type index = count[0] / 8 % 64;
  250. size_type padLen = (index < 56) ? (56 - index) : (120 - index);
  251. update(padding, padLen);
  252. // Append length (before padding)
  253. update(bits, 8);
  254. // Store state in digest
  255. encode(digest, state, 16);
  256. // Zeroize sensitive information.
  257. memset(buffer, 0, sizeof buffer);
  258. memset(count, 0, sizeof count);
  259. finalized = true;
  260. }
  261. return *this;
  262. }
  263. //////////////////////////////
  264. // return hex representation of digest as string
  265. std::string MD5::hexdigest() const
  266. {
  267. if(!finalized)
  268. {
  269. return "";
  270. }
  271. char buf[33];
  272. for(int i = 0; i < 16; i++)
  273. {
  274. sprintf(buf + i * 2, "%02x", digest[i]);
  275. }
  276. buf[32] = 0;
  277. return std::string(buf);
  278. }
  279. //////////////////////////////
  280. std::ostream& operator<<(std::ostream& out, MD5 md5)
  281. {
  282. return out << md5.hexdigest();
  283. }
  284. //////////////////////////////
  285. std::string md5(const std::string str)
  286. {
  287. MD5 md5 = MD5(str);
  288. return md5.hexdigest();
  289. }