md5.cpp 10 KB

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