MD5.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include "MD5.h"
  5. #include <string.h>
  6. /* Constants for MD5Transform routine. */
  7. #define S11 7
  8. #define S12 12
  9. #define S13 17
  10. #define S14 22
  11. #define S21 5
  12. #define S22 9
  13. #define S23 14
  14. #define S24 20
  15. #define S31 4
  16. #define S32 11
  17. #define S33 16
  18. #define S34 23
  19. #define S41 6
  20. #define S42 10
  21. #define S43 15
  22. #define S44 21
  23. /* F, G, H and I are basic MD5 functions.
  24. */
  25. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  26. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  27. #define H(x, y, z) ((x) ^ (y) ^ (z))
  28. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  29. /* ROTATE_LEFT rotates x left n bits.
  30. */
  31. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  32. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  33. Rotation is separate from addition to prevent recomputation.
  34. */
  35. #define FF(a, b, c, d, x, s, ac) { \
  36. (a) += F ((b), (c), (d)) + (x) + ac; \
  37. (a) = ROTATE_LEFT ((a), (s)); \
  38. (a) += (b); \
  39. }
  40. #define GG(a, b, c, d, x, s, ac) { \
  41. (a) += G ((b), (c), (d)) + (x) + ac; \
  42. (a) = ROTATE_LEFT ((a), (s)); \
  43. (a) += (b); \
  44. }
  45. #define HH(a, b, c, d, x, s, ac) { \
  46. (a) += H ((b), (c), (d)) + (x) + ac; \
  47. (a) = ROTATE_LEFT ((a), (s)); \
  48. (a) += (b); \
  49. }
  50. #define II(a, b, c, d, x, s, ac) { \
  51. (a) += I ((b), (c), (d)) + (x) + ac; \
  52. (a) = ROTATE_LEFT ((a), (s)); \
  53. (a) += (b); \
  54. }
  55. const byte MD5::PADDING[64] = { 0x80 };
  56. const char MD5::HEX[16] = {
  57. '0', '1', '2', '3',
  58. '4', '5', '6', '7',
  59. '8', '9', 'a', 'b',
  60. 'c', 'd', 'e', 'f'
  61. };
  62. /* Default construct. */
  63. MD5::MD5() {
  64. reset();
  65. }
  66. /* Construct a MD5 object with a input buffer. */
  67. MD5::MD5(const void* input, size_t length) {
  68. reset();
  69. update1(input, length);
  70. }
  71. /* Construct a MD5 object with a string. */
  72. MD5::MD5(const std::string& str) {
  73. reset();
  74. update(str);
  75. }
  76. /* Construct a MD5 object with a file. */
  77. MD5::MD5(std::ifstream& in) {
  78. reset();
  79. update3(in);
  80. }
  81. /* Return the message-digest */
  82. const byte* MD5::digest() {
  83. if (!_finished) {
  84. _finished = true;
  85. final();
  86. }
  87. return _digest;
  88. }
  89. /* Reset the calculate state */
  90. void MD5::reset() {
  91. _finished = false;
  92. /* reset number of bits. */
  93. _count[0] = _count[1] = 0;
  94. /* Load magic initialization constants. */
  95. _state[0] = 0x67452301;
  96. _state[1] = 0xefcdab89;
  97. _state[2] = 0x98badcfe;
  98. _state[3] = 0x10325476;
  99. }
  100. /* Updating the context with a input buffer. */
  101. void MD5::update1(const void* input, size_t length) {
  102. update((const byte*)input, length);
  103. }
  104. /* Updating the context with a string. */
  105. void MD5::update(const std::string& str) {
  106. update((const byte*)str.c_str(), str.length());
  107. }
  108. /* Updating the context with a file. */
  109. void MD5::update3(std::ifstream& in) {
  110. if (!in) {
  111. return;
  112. }
  113. std::streamsize length;
  114. char buffer[BUFFER_SIZE];
  115. while (!in.eof()) {
  116. in.read(buffer, BUFFER_SIZE);
  117. length = in.gcount();
  118. if (length > 0) {
  119. update1(buffer, length);
  120. }
  121. }
  122. in.close();
  123. }
  124. /* MD5 block update operation. Continues an MD5 message-digest
  125. operation, processing another message block, and updating the
  126. context.
  127. */
  128. void MD5::update(const byte* input, size_t length) {
  129. uint32 i, index, partLen;
  130. _finished = false;
  131. /* Compute number of bytes mod 64 */
  132. index = (uint32)((_count[0] >> 3) & 0x3f);
  133. /* update number of bits */
  134. if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3)) {
  135. ++_count[1];
  136. }
  137. _count[1] += ((uint32)length >> 29);
  138. partLen = 64 - index;
  139. /* transform as many times as possible. */
  140. if (length >= partLen) {
  141. memcpy(&_buffer[index], input, partLen);
  142. transform(_buffer);
  143. for (i = partLen; i + 63 < length; i += 64) {
  144. transform(&input[i]);
  145. }
  146. index = 0;
  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. std::string MD5::bytesToHexString(const byte* input, size_t length) {
  277. std::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. std::string MD5::toString() {
  290. return bytesToHexString(digest(), 16);
  291. }