md5.cpp 8.9 KB

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