You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
9.2 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /////////////////////////////////////////////////////////////////////////
  2. // MD5.cpp
  3. // Implementation file for MD5 class
  4. //
  5. // This C++ Class implementation of the original RSA Data Security, Inc.
  6. // MD5 Message-Digest Algorithm is copyright (c) 2002, Gary McNickle.
  7. // All rights reserved. This software is a derivative of the "RSA Data
  8. // Security, Inc. MD5 Message-Digest Algorithm"
  9. //
  10. // You may use this software free of any charge, but without any
  11. // warranty or implied warranty, provided that you follow the terms
  12. // of the original RSA copyright, listed below.
  13. //
  14. // Original RSA Data Security, Inc. Copyright notice
  15. /////////////////////////////////////////////////////////////////////////
  16. //
  17. // Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  18. // rights reserved.
  19. //
  20. // License to copy and use this software is granted provided that it
  21. // is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  22. // Algorithm" in all material mentioning or referencing this software
  23. // or this function.
  24. // License is also granted to make and use derivative works provided
  25. // that such works are identified as "derived from the RSA Data
  26. // Security, Inc. MD5 Message-Digest Algorithm" in all material
  27. // mentioning or referencing the derived work.
  28. // RSA Data Security, Inc. makes no representations concerning either
  29. // the merchantability of this software or the suitability of this
  30. // software for any particular purpose. It is provided "as is"
  31. // without express or implied warranty of any kind.
  32. // These notices must be retained in any copies of any part of this
  33. // documentation and/or software.
  34. /////////////////////////////////////////////////////////////////////////
  35. #include <assert.h>
  36. #include <memory.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include "md5.h"
  40. static unsigned char PADDING[64] =
  41. {
  42. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  45. };
  46. #define S11 7
  47. #define S12 12
  48. #define S13 17
  49. #define S14 22
  50. #define S21 5
  51. #define S22 9
  52. #define S23 14
  53. #define S24 20
  54. #define S31 4
  55. #define S32 11
  56. #define S33 16
  57. #define S34 23
  58. #define S41 6
  59. #define S42 10
  60. #define S43 15
  61. #define S44 21
  62. // PrintMD5: Converts a completed md5 digest into a char* string.
  63. char* PrintMD5(u_char md5Digest[16])
  64. {
  65. char chBuffer[256];
  66. char chEach[10];
  67. int nCount;
  68. memset(chBuffer,0,256);
  69. memset(chEach, 0, 10);
  70. for (nCount = 0; nCount < 16; nCount++)
  71. {
  72. sprintf(chEach, "%02x", md5Digest[nCount]);
  73. strncat(chBuffer, chEach, sizeof(chEach));
  74. }
  75. return strdup(chBuffer);
  76. }
  77. // MD5String: Performs the MD5 algorithm on a char* string, returning
  78. // the results as a char*.
  79. char* MD5String(char* szString)
  80. {
  81. int nLen = strlen(szString);
  82. md5 alg;
  83. alg.Update((unsigned char*)szString, (unsigned int)nLen);
  84. alg.Finalize();
  85. return PrintMD5(alg.Digest());
  86. }
  87. // MD5File: Performs the MD5 algorithm on a file (binar or text),
  88. // returning the results as a char*. Returns NULL if it fails.
  89. char* MD5File(char* szFilename)
  90. {
  91. FILE* file;
  92. md5 alg;
  93. int nLen;
  94. unsigned char chBuffer[1024];
  95. try
  96. {
  97. memset(chBuffer, 0, 1024);
  98. if ((file = fopen (szFilename, "rb")) != NULL)
  99. {
  100. while (nLen = fread (chBuffer, 1, 1024, file))
  101. alg.Update(chBuffer, nLen);
  102. alg.Finalize();
  103. fclose (file);
  104. return PrintMD5(alg.Digest());
  105. }
  106. }
  107. catch(...)
  108. {
  109. }
  110. return NULL; // failed
  111. }
  112. // md5::Init
  113. // Initializes a new context.
  114. void md5::Init()
  115. {
  116. memset(m_Count, 0, 2 * sizeof(u_int4));
  117. m_State[0] = 0x67452301;
  118. m_State[1] = 0xefcdab89;
  119. m_State[2] = 0x98badcfe;
  120. m_State[3] = 0x10325476;
  121. }
  122. // md5::Update
  123. // MD5 block update operation. Continues an MD5 message-digest
  124. // operation, processing another message block, and updating the
  125. // context.
  126. void md5::Update(u_char* chInput, u_int4 nInputLen)
  127. {
  128. u_int4 i, index, partLen;
  129. // Compute number of bytes mod 64
  130. index = (unsigned int)((m_Count[0] >> 3) & 0x3F);
  131. // Update number of bits
  132. if ((m_Count[0] += (nInputLen << 3)) < (nInputLen << 3))
  133. m_Count[1]++;
  134. m_Count[1] += (nInputLen >> 29);
  135. partLen = 64 - index;
  136. // Transform as many times as possible.
  137. if (nInputLen >= partLen)
  138. {
  139. memcpy( &m_Buffer[index], chInput, partLen );
  140. Transform(m_Buffer);
  141. for (i = partLen; i + 63 < nInputLen; i += 64)
  142. Transform(&chInput[i]);
  143. index = 0;
  144. }
  145. else
  146. i = 0;
  147. // Buffer remaining input
  148. memcpy( &m_Buffer[index], &chInput[i], nInputLen-i );
  149. }
  150. // md5::Finalize
  151. // MD5 finalization. Ends an MD5 message-digest operation, writing
  152. // the message digest and zeroizing the context.
  153. void md5::Finalize()
  154. {
  155. u_char bits[8];
  156. u_int4 index, padLen;
  157. // Save number of bits
  158. Encode (bits, m_Count, 8);
  159. // Pad out to 56 mod 64
  160. index = (unsigned int)((m_Count[0] >> 3) & 0x3f);
  161. padLen = (index < 56) ? (56 - index) : (120 - index);
  162. Update(PADDING, padLen);
  163. // Append length (before padding)
  164. Update (bits, 8);
  165. // Store state in digest
  166. Encode (m_Digest, m_State, 16);
  167. memset(m_Count, 0, 2 * sizeof(u_int4));
  168. memset(m_State, 0, 4 * sizeof(u_int4));
  169. memset(m_Buffer,0, 64 * sizeof(u_char));
  170. }
  171. // md5::Transform
  172. // MD5 basic transformation. Transforms state based on block.
  173. void md5::Transform (u_char* block)
  174. {
  175. u_int4 a = m_State[0], b = m_State[1], c = m_State[2], d = m_State[3], x[16];
  176. Decode (x, block, 64);
  177. // Round 1
  178. FF (a, b, c, d, x[ 0], S11, 0xd76aa478);
  179. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756);
  180. FF (c, d, a, b, x[ 2], S13, 0x242070db);
  181. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee);
  182. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf);
  183. FF (d, a, b, c, x[ 5], S12, 0x4787c62a);
  184. FF (c, d, a, b, x[ 6], S13, 0xa8304613);
  185. FF (b, c, d, a, x[ 7], S14, 0xfd469501);
  186. FF (a, b, c, d, x[ 8], S11, 0x698098d8);
  187. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af);
  188. FF (c, d, a, b, x[10], S13, 0xffff5bb1);
  189. FF (b, c, d, a, x[11], S14, 0x895cd7be);
  190. FF (a, b, c, d, x[12], S11, 0x6b901122);
  191. FF (d, a, b, c, x[13], S12, 0xfd987193);
  192. FF (c, d, a, b, x[14], S13, 0xa679438e);
  193. FF (b, c, d, a, x[15], S14, 0x49b40821);
  194. // Round 2
  195. GG (a, b, c, d, x[ 1], S21, 0xf61e2562);
  196. GG (d, a, b, c, x[ 6], S22, 0xc040b340);
  197. GG (c, d, a, b, x[11], S23, 0x265e5a51);
  198. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
  199. GG (a, b, c, d, x[ 5], S21, 0xd62f105d);
  200. GG (d, a, b, c, x[10], S22, 0x2441453);
  201. GG (c, d, a, b, x[15], S23, 0xd8a1e681);
  202. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
  203. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6);
  204. GG (d, a, b, c, x[14], S22, 0xc33707d6);
  205. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87);
  206. GG (b, c, d, a, x[ 8], S24, 0x455a14ed);
  207. GG (a, b, c, d, x[13], S21, 0xa9e3e905);
  208. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
  209. GG (c, d, a, b, x[ 7], S23, 0x676f02d9);
  210. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);
  211. // Round 3
  212. HH (a, b, c, d, x[ 5], S31, 0xfffa3942);
  213. HH (d, a, b, c, x[ 8], S32, 0x8771f681);
  214. HH (c, d, a, b, x[11], S33, 0x6d9d6122);
  215. HH (b, c, d, a, x[14], S34, 0xfde5380c);
  216. HH (a, b, c, d, x[ 1], S31, 0xa4beea44);
  217. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
  218. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60);
  219. HH (b, c, d, a, x[10], S34, 0xbebfbc70);
  220. HH (a, b, c, d, x[13], S31, 0x289b7ec6);
  221. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa);
  222. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085);
  223. HH (b, c, d, a, x[ 6], S34, 0x4881d05);
  224. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039);
  225. HH (d, a, b, c, x[12], S32, 0xe6db99e5);
  226. HH (c, d, a, b, x[15], S33, 0x1fa27cf8);
  227. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665);
  228. // Round 4
  229. II (a, b, c, d, x[ 0], S41, 0xf4292244);
  230. II (d, a, b, c, x[ 7], S42, 0x432aff97);
  231. II (c, d, a, b, x[14], S43, 0xab9423a7);
  232. II (b, c, d, a, x[ 5], S44, 0xfc93a039);
  233. II (a, b, c, d, x[12], S41, 0x655b59c3);
  234. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
  235. II (c, d, a, b, x[10], S43, 0xffeff47d);
  236. II (b, c, d, a, x[ 1], S44, 0x85845dd1);
  237. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f);
  238. II (d, a, b, c, x[15], S42, 0xfe2ce6e0);
  239. II (c, d, a, b, x[ 6], S43, 0xa3014314);
  240. II (b, c, d, a, x[13], S44, 0x4e0811a1);
  241. II (a, b, c, d, x[ 4], S41, 0xf7537e82);
  242. II (d, a, b, c, x[11], S42, 0xbd3af235);
  243. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
  244. II (b, c, d, a, x[ 9], S44, 0xeb86d391);
  245. m_State[0] += a;
  246. m_State[1] += b;
  247. m_State[2] += c;
  248. m_State[3] += d;
  249. memset(x, 0, sizeof(x));
  250. }
  251. // md5::Encode
  252. // Encodes input (uint4) into output (uchar). Assumes nLength is
  253. // a multiple of 4.
  254. void md5::Encode(u_char* dest, u_int4* src, u_int4 nLength)
  255. {
  256. u_int4 i, j;
  257. assert(nLength % 4 == 0);
  258. for (i = 0, j = 0; j < nLength; i++, j += 4)
  259. {
  260. dest[j] = (u_char)(src[i] & 0xff);
  261. dest[j+1] = (u_char)((src[i] >> 8) & 0xff);
  262. dest[j+2] = (u_char)((src[i] >> 16) & 0xff);
  263. dest[j+3] = (u_char)((src[i] >> 24) & 0xff);
  264. }
  265. }
  266. // md5::Decode
  267. // Decodes input (uchar) into output (uint4). Assumes nLength is
  268. // a multiple of 4.
  269. void md5::Decode(u_int4* dest, u_char* src, u_int4 nLength)
  270. {
  271. u_int4 i, j;
  272. assert(nLength % 4 == 0);
  273. for (i = 0, j = 0; j < nLength; i++, j += 4)
  274. {
  275. dest[i] = ((u_int4)src[j]) | (((u_int4)src[j+1])<<8) |
  276. (((u_int4)src[j+2])<<16) | (((u_int4)src[j+3])<<24);
  277. }
  278. }