MD5.cpp 9.0 KB

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