MD5.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "stdafx.h"
  2. #include "Md5.h"
  3. #include <strsafe.h>
  4. // MD5Transform函数使用的常量;
  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. // 基础的MD5函数(四个非线性函数(每轮一个))
  22. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  23. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  24. #define H(x, y, z) ((x) ^ (y) ^ (z))
  25. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  26. // 将x左移n个bit位;
  27. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  28. // FF, GG, HH, 和 II 是用来转换 Rounds1,2,3,4的 ;
  29. // Rotation is separate from addition to prevent recomputation;
  30. #define FF(a, b, c, d, x, s, ac) { \
  31. (a) += F ((b), (c), (d)) + (x) + (UINT32)(ac); \
  32. (a) = ROTATE_LEFT ((a), (s)); \
  33. (a) += (b); \
  34. }
  35. #define GG(a, b, c, d, x, s, ac) { \
  36. (a) += G ((b), (c), (d)) + (x) + (UINT32)(ac); \
  37. (a) = ROTATE_LEFT ((a), (s)); \
  38. (a) += (b); \
  39. }
  40. #define HH(a, b, c, d, x, s, ac) { \
  41. (a) += H ((b), (c), (d)) + (x) + (UINT32)(ac); \
  42. (a) = ROTATE_LEFT ((a), (s)); \
  43. (a) += (b); \
  44. }
  45. #define II(a, b, c, d, x, s, ac) { \
  46. (a) += I ((b), (c), (d)) + (x) + (UINT32)(ac); \
  47. (a) = ROTATE_LEFT ((a), (s)); \
  48. (a) += (b); \
  49. }
  50. static unsigned char PADDING[64] = {
  51. 0x80, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0,
  56. 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0
  59. };
  60. CMD5::CMD5(void)
  61. {
  62. m_nInputLen = 0;
  63. m_pInputText = NULL;
  64. m_bValidDigest = FALSE;
  65. memset(m_digestStr,0,33);
  66. }
  67. CMD5::CMD5(IN CONST BYTE* szInput,IN CONST size_t& nInputLen)
  68. {
  69. m_pInputText = const_cast<BYTE*>(szInput);
  70. m_nInputLen = nInputLen;
  71. memset(m_digestStr, 0, 33);
  72. m_bValidDigest = CalcDigest();
  73. }
  74. CMD5::~CMD5(void)
  75. {
  76. }
  77. void CMD5::Decode(IN UINT32 *output, IN UINT8 *input, IN CONST size_t& len)
  78. {
  79. UINT32 i, j;
  80. for (i = 0, j = 0; j < len; i++, j += 4)
  81. output[i] = (input[j]) | ((input[j+1]) << 8) | ((input[j+2]) << 16) | ((input[j+3]) << 24);
  82. }
  83. void CMD5::Encode(IN UINT8 *output, IN UINT32 *input, IN CONST size_t& len)
  84. {
  85. UINT32 i, j;
  86. for (i = 0, j = 0; j < len; i++, j += 4)
  87. {
  88. output[j] = input[i] & 0xFF;
  89. output[j+1] = (input[i] >> 8) & 0xFF;
  90. output[j+2] = (input[i] >> 16) & 0xFF;
  91. output[j+3] = (input[i] >> 24) & 0xFF;
  92. }
  93. }
  94. void CMD5::MD5Init(IN MD5_CTX* md5ctx)
  95. {
  96. md5ctx->count[0] = md5ctx->count[1] = 0;
  97. // 用魔数常量来初始化;
  98. md5ctx->state[0] = 0x67452301;
  99. md5ctx->state[1] = 0xefcdab89;
  100. md5ctx->state[2] = 0x98badcfe;
  101. md5ctx->state[3] = 0x10325476;
  102. }
  103. void CMD5::MD5Update(IN MD5_CTX* md5ctx, IN UINT8* Input, IN CONST size_t& InputLen)
  104. {
  105. UINT32 i, nIndex, nPartLen;
  106. // 计算出模64(0x3F)后的值;
  107. nIndex = (md5ctx->count[0] >> 3) & 0x3F;
  108. // 更新bit位数;
  109. if ( (md5ctx->count[0] += (InputLen << 3))< (InputLen << 3) )
  110. md5ctx->count[1]++;
  111. md5ctx->count[1] += (InputLen >> 29);
  112. // 设置缓冲区的字节数;
  113. nPartLen = 64 - nIndex;
  114. // 尽可能多的转换;
  115. if (InputLen >= nPartLen)
  116. {
  117. memcpy(&md5ctx->buffer[nIndex], Input, nPartLen);
  118. MD5Transform(md5ctx->state, md5ctx->buffer);
  119. for (i = nPartLen; i + 63 < InputLen; i += 64)
  120. MD5Transform(md5ctx->state, &Input[i]);
  121. nIndex = 0;
  122. }
  123. else
  124. i = 0;
  125. // 复制剩余的缓冲内容;
  126. memcpy(&md5ctx->buffer[nIndex], &Input[i], InputLen-i);
  127. }
  128. void CMD5::MD5Transform(IN UINT32 state[4], IN UINT8 block[64] )
  129. {
  130. UINT32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  131. Decode(x, block, 64);
  132. // 第一轮分组运算;
  133. FF(a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  134. FF(d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  135. FF(c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  136. FF(b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  137. FF(a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  138. FF(d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  139. FF(c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  140. FF(b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  141. FF(a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  142. FF(d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  143. FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  144. FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  145. FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  146. FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  147. FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  148. FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  149. // 第二轮分组运算;
  150. GG(a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  151. GG(d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  152. GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  153. GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  154. GG(a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  155. GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  156. GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  157. GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  158. GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  159. GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  160. GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  161. GG(b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  162. GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  163. GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  164. GG(c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  165. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  166. // 第三轮分组运算;
  167. HH(a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  168. HH(d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  169. HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  170. HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  171. HH(a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  172. HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  173. HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  174. HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  175. HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  176. HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  177. HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  178. HH(b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  179. HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  180. HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  181. HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  182. HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  183. // 第四轮分组运算;
  184. II(a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  185. II(d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  186. II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  187. II(b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  188. II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  189. II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  190. II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  191. II(b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  192. II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  193. II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  194. II(c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  195. II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  196. II(a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  197. II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  198. II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  199. II(b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  200. state[0] += a;
  201. state[1] += b;
  202. state[2] += c;
  203. state[3] += d;
  204. memset(x, 0, sizeof(x));
  205. }
  206. void CMD5::MD5Final(IN UINT8 digest[16], IN MD5_CTX* md5ctx)
  207. {
  208. UINT8 bits[8];
  209. UINT32 index, padLen;
  210. // 保存2进制位数;
  211. Encode(bits, md5ctx->count, 8);
  212. /* Pad out to 56 mod 64.*/
  213. index = (unsigned int)((md5ctx->count[0] >> 3) & 0x3f);
  214. padLen = (index < 56) ? (56 - index) : (120 - index);
  215. MD5Update(md5ctx, PADDING, padLen);
  216. /* Append length (before padding) */
  217. MD5Update(md5ctx, bits, 8);
  218. /* Store state in digest */
  219. Encode(digest, md5ctx->state, 16);
  220. memset(md5ctx, 0, sizeof(*md5ctx));
  221. }
  222. void CMD5::SetBYTEText(IN CONST BYTE* szInput, IN CONST size_t& nInputLen)
  223. {
  224. m_pInputText = const_cast<BYTE*>(szInput);
  225. m_nInputLen = nInputLen;
  226. m_bValidDigest = CalcDigest();
  227. }
  228. BOOL CMD5::CalcDigest()
  229. {
  230. MD5_CTX md5ctx;
  231. // 初始化md5;
  232. MD5Init(&md5ctx);
  233. MD5Update(&md5ctx, m_pInputText, m_nInputLen);
  234. MD5Final(m_digest,&md5ctx);
  235. // 转化为32位的16进制字符;
  236. int p = 0;
  237. for (int i = 0; i < 16; i++)
  238. {
  239. #ifdef UNICODE
  240. StringCchPrintfW(&m_digestStr[p], 3, L"%02x", m_digest[i]);
  241. #else
  242. StringCchPrintfA(&m_digestStr[p], 3, "%02x", m_digest[i]);
  243. #endif
  244. p += 2;
  245. }
  246. return TRUE;
  247. }
  248. CONST TCHAR* CMD5::GetMD5Digest()
  249. {
  250. if( m_bValidDigest )
  251. return m_digestStr;
  252. return NULL;
  253. }
  254. std::string GetTextMD5(std::string text)
  255. {
  256. CMD5 md5;
  257. md5.SetBYTEText((const byte*)text.c_str(), text.size());
  258. return std::string(md5.GetMD5Digest());
  259. }
  260. std::string GetFileMD5(std::string file)
  261. {
  262. CMD5 md5;
  263. FILE *pf = NULL;
  264. if ( _tfopen_s(&pf, file.c_str(), "rb") == 0 )
  265. {
  266. fseek(pf, 0, SEEK_END);
  267. size_t fsize = ftell(pf);
  268. fseek(pf, 0, SEEK_SET);
  269. byte* pdata = new byte[fsize];
  270. fread(pdata, 1, fsize, pf);
  271. md5.SetBYTEText(pdata, fsize);
  272. delete []pdata;
  273. pdata = NULL;
  274. fclose(pf);
  275. return std::string(md5.GetMD5Digest());
  276. }
  277. return std::string();
  278. }