2.8 MD5

md5.h

  1. /*
  2. **********************************************************************
  3. ** md5.h -- Header file for implementation of MD5 **
  4. ** RSA Data Security, Inc. MD5 Message Digest Algorithm **
  5. ** Created: 2/17/90 RLR **
  6. ** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version **
  7. ** Revised (for MD5): RLR 4/27/91 **
  8. ** -- G modified to have y&~z instead of y&z **
  9. ** -- FF, GG, HH modified to add in last register done **
  10. ** -- Access pattern: round 2 works mod 5, round 3 works mod 3 **
  11. ** -- distinct additive constant for each step **
  12. ** -- round 4 added, working mod 7 **
  13. **********************************************************************
  14. */
  15. /*
  16. **********************************************************************
  17. ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
  18. ** **
  19. ** License to copy and use this software is granted provided that **
  20. ** it is identified as the "RSA Data Security, Inc. MD5 Message **
  21. ** Digest Algorithm" in all material mentioning or referencing this **
  22. ** software or this function. **
  23. ** **
  24. ** License is also granted to make and use derivative works **
  25. ** provided that such works are identified as "derived from the RSA **
  26. ** Data Security, Inc. MD5 Message Digest Algorithm" in all **
  27. ** material mentioning or referencing the derived work. **
  28. ** **
  29. ** RSA Data Security, Inc. makes no representations concerning **
  30. ** either the merchantability of this software or the suitability **
  31. ** of this software for any particular purpose. It is provided "as **
  32. ** is" without express or implied warranty of any kind. **
  33. ** **
  34. ** These notices must be retained in any copies of any part of this **
  35. ** documentation and/or software. **
  36. **********************************************************************
  37. */
  38. #ifndef __MD5_H_
  39. #define __MD5_H_
  40. #include "Type.h"
  41. #define _MD5_MAX 32
  42. /* Data structure for MD5 (Message Digest) computation */
  43. typedef struct {
  44. DWORD i[2]; /* number of _bits_ handled mod 2^64 */
  45. DWORD buf[4]; /* scratch buffer */
  46. BYTE in[64]; /* input buffer */
  47. BYTE digest[16]; /* actual digest after MD5Final call */
  48. } _MD5_CTX;
  49. /**
  50. * 计算指定字符串的md5值
  51. *@data 待MD5的数据
  52. *@datalen 待md5数据的长度
  53. *@md5Value OUT MD计算结果
  54. *@md5ValueMax md5Value缓冲区大小(
  55. 如果小于16字节,则返回失败;
  56. 如果小区32字节则返回16字节的编码;
  57. 如果大于等于32字节,则返回32字节的bcd编码)
  58. *@return 成功返回md5Value的使用字节数
  59. * 失败返回 -1
  60. *@notes 一般情况下,MD5的结果为16或32字节,32字节为16字节的bcd编码。
  61. */
  62. int Md5(BYTE* data,int datalen,BYTE* md5Value,int md5ValueMax);
  63. #define _md5 Md5
  64. /**
  65. * 计算指定文件的md5值
  66. *@pcFilePath 待计算MD5的文件路径
  67. *@md5Value OUT MD计算结果
  68. *@md5ValueMax md5Value缓冲区大小(
  69. 如果小于16字节,则返回失败;
  70. 如果小区32字节则返回16字节的编码;
  71. 如果大于等于32字节,则返回32字节的bcd编码)
  72. *@return 成功返回md5Value的使用字节数
  73. * 失败返回 -1
  74. *@notes 一般情况下,MD5的结果为16或32字节,32字节为16字节的bcd编码。
  75. */
  76. int CalcFileMd5(char* pcFilePath,BYTE* md5Value,int md5ValueMax);
  77. /**
  78. * Md5初始化
  79. *@mdContext md5上下文
  80. *@return 无
  81. */
  82. void Md5Init (_MD5_CTX *mdContext);
  83. /**
  84. * 执行MD计算
  85. *@mdContext MD5上下文,Md5Init初始化过。
  86. *@inBuf 待MD5计算的内容
  87. *@inLen 内容长度
  88. */
  89. void Md5Update (_MD5_CTX *mdContext, BYTE *inBuf, DWORD inLen);
  90. /**
  91. * 完成MD计算
  92. */
  93. void Md5Final (_MD5_CTX *mdContext);
  94. /**
  95. * 随机数Md5加密
  96. *@plaintext 加密前的明文
  97. *@plaintextLen 明文长度
  98. *@randtext 随机内容
  99. *@randtextLen 随机内容长度
  100. *@encryption 存放密文的空间
  101. *@encryptionMax 密文空间最大容量
  102. *@return 成功返回加密后的内容长度
  103. * 失败返回 -1
  104. */
  105. int RandMd5(char* plaintext,int plaintextLen,char* randtext,int randtextLen,char* encryption,int encryptionMax);
  106. #endif

md5.c

  1. /*
  2. **********************************************************************
  3. ** md5.c **
  4. ** RSA Data Security, Inc. MD5 Message Digest Algorithm **
  5. ** Created: 2/17/90 RLR **
  6. ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
  7. **********************************************************************
  8. */
  9. /*
  10. **********************************************************************
  11. ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
  12. ** **
  13. ** License to copy and use this software is granted provided that **
  14. ** it is identified as the "RSA Data Security, Inc. MD5 Message **
  15. ** Digest Algorithm" in all material mentioning or referencing this **
  16. ** software or this function. **
  17. ** **
  18. ** License is also granted to make and use derivative works **
  19. ** provided that such works are identified as "derived from the RSA **
  20. ** Data Security, Inc. MD5 Message Digest Algorithm" in all **
  21. ** material mentioning or referencing the derived work. **
  22. ** **
  23. ** RSA Data Security, Inc. makes no representations concerning **
  24. ** either the merchantability of this software or the suitability **
  25. ** of this software for any particular purpose. It is provided "as **
  26. ** is" without express or implied warranty of any kind. **
  27. ** **
  28. ** These notices must be retained in any copies of any part of this **
  29. ** documentation and/or software. **
  30. **********************************************************************
  31. */
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include "Md5.h"
  36. /* forward declaration */
  37. static void Transform (DWORD *buf, DWORD *in) ;
  38. static unsigned char PADDING[64] = {
  39. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  47. };
  48. /* F, G and H are basic MD5 functions: selection, majority, parity */
  49. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  50. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  51. #define H(x, y, z) ((x) ^ (y) ^ (z))
  52. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  53. /* ROTATE_LEFT rotates x left n bits */
  54. #if 1// LITTLE_ENDIAN
  55. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  56. #else
  57. #define ROTATE_LEFT(x, n) (((x) << (32 - (n))) | ((x) >> (n)))
  58. #endif
  59. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
  60. /* Rotation is separate from addition to prevent recomputation */
  61. #define FF(a, b, c, d, x, s, ac) \
  62. {(a) += F ((b), (c), (d)) + (x) + (DWORD)(ac); \
  63. (a) = ROTATE_LEFT ((a), (s)); \
  64. (a) += (b); \
  65. }
  66. #define GG(a, b, c, d, x, s, ac) \
  67. {(a) += G ((b), (c), (d)) + (x) + (DWORD)(ac); \
  68. (a) = ROTATE_LEFT ((a), (s)); \
  69. (a) += (b); \
  70. }
  71. #define HH(a, b, c, d, x, s, ac) \
  72. {(a) += H ((b), (c), (d)) + (x) + (DWORD)(ac); \
  73. (a) = ROTATE_LEFT ((a), (s)); \
  74. (a) += (b); \
  75. }
  76. #define II(a, b, c, d, x, s, ac) \
  77. {(a) += I ((b), (c), (d)) + (x) + (DWORD)(ac); \
  78. (a) = ROTATE_LEFT ((a), (s)); \
  79. (a) += (b); \
  80. }
  81. void Md5Init (_MD5_CTX *mdContext)
  82. {
  83. mdContext->i[0] = mdContext->i[1] = (DWORD)0;
  84. /* Load magic initialization constants.
  85. */
  86. mdContext->buf[0] = (DWORD)0x67452301;
  87. mdContext->buf[1] = (DWORD)0xefcdab89;
  88. mdContext->buf[2] = (DWORD)0x98badcfe;
  89. mdContext->buf[3] = (DWORD)0x10325476;
  90. }
  91. void Md5Update (_MD5_CTX *mdContext, BYTE *inBuf, DWORD inLen)
  92. {
  93. DWORD in[16];
  94. int mdi;
  95. unsigned int i, ii;
  96. /* compute number of bytes mod 64 */
  97. mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  98. /* update number of bits */
  99. if ((mdContext->i[0] + ((DWORD)inLen << 3)) < mdContext->i[0])
  100. mdContext->i[1]++;
  101. mdContext->i[0] += ((DWORD)inLen << 3);
  102. mdContext->i[1] += ((DWORD)inLen >> 29);
  103. while (inLen--) {
  104. /* add new character to buffer, increment mdi */
  105. mdContext->in[mdi++] = *inBuf++;
  106. /* transform if necessary */
  107. if (mdi == 0x40) {
  108. for (i = 0, ii = 0; i < 16; i++, ii += 4)
  109. in[i] = (((DWORD)mdContext->in[ii+3]) << 24) |
  110. (((DWORD)mdContext->in[ii+2]) << 16) |
  111. (((DWORD)mdContext->in[ii+1]) << 8) |
  112. ((DWORD)mdContext->in[ii]);
  113. Transform (mdContext->buf, in);
  114. mdi = 0;
  115. }
  116. }
  117. }
  118. void Md5Final (_MD5_CTX *mdContext)
  119. {
  120. DWORD in[16];
  121. int mdi;
  122. unsigned int i, ii;
  123. unsigned int padLen;
  124. /* save number of bits */
  125. in[14] = mdContext->i[0];
  126. in[15] = mdContext->i[1];
  127. /* compute number of bytes mod 64 */
  128. mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  129. /* pad out to 56 mod 64 */
  130. padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
  131. Md5Update (mdContext, PADDING, padLen);
  132. /* append length in bits and transform */
  133. for (i = 0, ii = 0; i < 14; i++, ii += 4)
  134. in[i] = (((DWORD)mdContext->in[ii+3]) << 24) |
  135. (((DWORD)mdContext->in[ii+2]) << 16) |
  136. (((DWORD)mdContext->in[ii+1]) << 8) |
  137. ((DWORD)mdContext->in[ii]);
  138. Transform (mdContext->buf, in);
  139. /* store buffer in digest */
  140. for (i = 0, ii = 0; i < 4; i++, ii += 4) {
  141. mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
  142. mdContext->digest[ii+1] =
  143. (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
  144. mdContext->digest[ii+2] =
  145. (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
  146. mdContext->digest[ii+3] =
  147. (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
  148. }
  149. }
  150. /* Basic MD5 step. Transform buf based on in.
  151. */
  152. static void Transform (DWORD *buf, DWORD *in)
  153. {
  154. DWORD a = buf[0], b = buf[1], c = buf[2], d = buf[3];
  155. /* Round 1 */
  156. #define S11 7
  157. #define S12 12
  158. #define S13 17
  159. #define S14 22
  160. FF ( a, b, c, d, in[ 0], S11, 3614090360UL); /* 1 */
  161. FF ( d, a, b, c, in[ 1], S12, 3905402710UL); /* 2 */
  162. FF ( c, d, a, b, in[ 2], S13, 606105819UL); /* 3 */
  163. FF ( b, c, d, a, in[ 3], S14, 3250441966UL); /* 4 */
  164. FF ( a, b, c, d, in[ 4], S11, 4118548399UL); /* 5 */
  165. FF ( d, a, b, c, in[ 5], S12, 1200080426UL); /* 6 */
  166. FF ( c, d, a, b, in[ 6], S13, 2821735955UL); /* 7 */
  167. FF ( b, c, d, a, in[ 7], S14, 4249261313UL); /* 8 */
  168. FF ( a, b, c, d, in[ 8], S11, 1770035416UL); /* 9 */
  169. FF ( d, a, b, c, in[ 9], S12, 2336552879UL); /* 10 */
  170. FF ( c, d, a, b, in[10], S13, 4294925233UL); /* 11 */
  171. FF ( b, c, d, a, in[11], S14, 2304563134UL); /* 12 */
  172. FF ( a, b, c, d, in[12], S11, 1804603682UL); /* 13 */
  173. FF ( d, a, b, c, in[13], S12, 4254626195UL); /* 14 */
  174. FF ( c, d, a, b, in[14], S13, 2792965006UL); /* 15 */
  175. FF ( b, c, d, a, in[15], S14, 1236535329UL); /* 16 */
  176. /* Round 2 */
  177. #define S21 5
  178. #define S22 9
  179. #define S23 14
  180. #define S24 20
  181. GG ( a, b, c, d, in[ 1], S21, 4129170786UL); /* 17 */
  182. GG ( d, a, b, c, in[ 6], S22, 3225465664UL); /* 18 */
  183. GG ( c, d, a, b, in[11], S23, 643717713UL); /* 19 */
  184. GG ( b, c, d, a, in[ 0], S24, 3921069994UL); /* 20 */
  185. GG ( a, b, c, d, in[ 5], S21, 3593408605UL); /* 21 */
  186. GG ( d, a, b, c, in[10], S22, 38016083UL); /* 22 */
  187. GG ( c, d, a, b, in[15], S23, 3634488961UL); /* 23 */
  188. GG ( b, c, d, a, in[ 4], S24, 3889429448UL); /* 24 */
  189. GG ( a, b, c, d, in[ 9], S21, 568446438UL); /* 25 */
  190. GG ( d, a, b, c, in[14], S22, 3275163606UL); /* 26 */
  191. GG ( c, d, a, b, in[ 3], S23, 4107603335UL); /* 27 */
  192. GG ( b, c, d, a, in[ 8], S24, 1163531501UL); /* 28 */
  193. GG ( a, b, c, d, in[13], S21, 2850285829UL); /* 29 */
  194. GG ( d, a, b, c, in[ 2], S22, 4243563512UL); /* 30 */
  195. GG ( c, d, a, b, in[ 7], S23, 1735328473UL); /* 31 */
  196. GG ( b, c, d, a, in[12], S24, 2368359562UL); /* 32 */
  197. /* Round 3 */
  198. #define S31 4
  199. #define S32 11
  200. #define S33 16
  201. #define S34 23
  202. HH ( a, b, c, d, in[ 5], S31, 4294588738UL); /* 33 */
  203. HH ( d, a, b, c, in[ 8], S32, 2272392833UL); /* 34 */
  204. HH ( c, d, a, b, in[11], S33, 1839030562UL); /* 35 */
  205. HH ( b, c, d, a, in[14], S34, 4259657740UL); /* 36 */
  206. HH ( a, b, c, d, in[ 1], S31, 2763975236UL); /* 37 */
  207. HH ( d, a, b, c, in[ 4], S32, 1272893353UL); /* 38 */
  208. HH ( c, d, a, b, in[ 7], S33, 4139469664UL); /* 39 */
  209. HH ( b, c, d, a, in[10], S34, 3200236656UL); /* 40 */
  210. HH ( a, b, c, d, in[13], S31, 681279174UL); /* 41 */
  211. HH ( d, a, b, c, in[ 0], S32, 3936430074UL); /* 42 */
  212. HH ( c, d, a, b, in[ 3], S33, 3572445317UL); /* 43 */
  213. HH ( b, c, d, a, in[ 6], S34, 76029189UL); /* 44 */
  214. HH ( a, b, c, d, in[ 9], S31, 3654602809UL); /* 45 */
  215. HH ( d, a, b, c, in[12], S32, 3873151461UL); /* 46 */
  216. HH ( c, d, a, b, in[15], S33, 530742520UL); /* 47 */
  217. HH ( b, c, d, a, in[ 2], S34, 3299628645UL); /* 48 */
  218. /* Round 4 */
  219. #define S41 6
  220. #define S42 10
  221. #define S43 15
  222. #define S44 21
  223. II ( a, b, c, d, in[ 0], S41, 4096336452UL); /* 49 */
  224. II ( d, a, b, c, in[ 7], S42, 1126891415UL); /* 50 */
  225. II ( c, d, a, b, in[14], S43, 2878612391UL); /* 51 */
  226. II ( b, c, d, a, in[ 5], S44, 4237533241UL); /* 52 */
  227. II ( a, b, c, d, in[12], S41, 1700485571UL); /* 53 */
  228. II ( d, a, b, c, in[ 3], S42, 2399980690UL); /* 54 */
  229. II ( c, d, a, b, in[10], S43, 4293915773UL); /* 55 */
  230. II ( b, c, d, a, in[ 1], S44, 2240044497UL); /* 56 */
  231. II ( a, b, c, d, in[ 8], S41, 1873313359UL); /* 57 */
  232. II ( d, a, b, c, in[15], S42, 4264355552UL); /* 58 */
  233. II ( c, d, a, b, in[ 6], S43, 2734768916UL); /* 59 */
  234. II ( b, c, d, a, in[13], S44, 1309151649UL); /* 60 */
  235. II ( a, b, c, d, in[ 4], S41, 4149444226UL); /* 61 */
  236. II ( d, a, b, c, in[11], S42, 3174756917UL); /* 62 */
  237. II ( c, d, a, b, in[ 2], S43, 718787259UL); /* 63 */
  238. II ( b, c, d, a, in[ 9], S44, 3951481745UL); /* 64 */
  239. buf[0] += a;
  240. buf[1] += b;
  241. buf[2] += c;
  242. buf[3] += d;
  243. }
  244. /**
  245. * 计算指定字符串的md5值
  246. *@data 待MD5的数据
  247. *@datalen 待md5数据的长度
  248. *@md5Value OUT MD计算结果
  249. *@md5ValueMax md5Value缓冲区大小(
  250. 如果小于16字节,则返回失败;
  251. 如果小区32字节则返回16字节的编码;
  252. 如果大于等于32字节,则返回32字节的bcd编码)
  253. *@return 成功返回md5Value的使用字节数
  254. * 失败返回 -1
  255. *@notes 一般情况下,MD5的结果为16或32字节,32字节为16字节的bcd编码。
  256. */
  257. int Md5(BYTE* data,int datalen,BYTE* md5Value,int md5ValueMax)
  258. {
  259. _MD5_CTX mdContext;
  260. Md5Init(&mdContext);
  261. Md5Update(&mdContext, data, datalen);
  262. Md5Final(&mdContext);
  263. if(md5ValueMax < 16)//容量不够
  264. {
  265. return -1;
  266. }
  267. else if(md5ValueMax < 32) //如果小于32
  268. {
  269. for (int i=0;i<16;i++)
  270. {
  271. md5Value[i] = mdContext.digest[i];
  272. }
  273. return 16;
  274. }
  275. else
  276. {
  277. for(int i=0; i<16; i++)
  278. {
  279. sprintf((char*)&md5Value[i*2], "%02x", mdContext.digest[i]);
  280. }
  281. return 32;
  282. }
  283. return -1;
  284. }
  285. /*
  286. **********************************************************************
  287. ** End of md5.c **
  288. ******************************* (cut) ********************************
  289. */
  290. /**
  291. * 计算指定文件的md5值
  292. *@pcFilePath 待计算MD5的文件路径
  293. *@md5Value OUT MD计算结果
  294. *@md5ValueMax md5Value缓冲区大小(
  295. 如果小于16字节,则返回失败;
  296. 如果小区32字节则返回16字节的编码;
  297. 如果大于等于32字节,则返回32字节的bcd编码)
  298. *@return 成功返回md5Value的使用字节数
  299. * 失败返回 -1
  300. *@notes 一般情况下,MD5的结果为16或32字节,32字节为16字节的bcd编码。
  301. */
  302. int CalcFileMd5(char* pcFilePath,BYTE* md5Value,int md5ValueMax)
  303. {
  304. FILE *fp = fopen (pcFilePath, "rb");
  305. _MD5_CTX mdContext;
  306. int bytes;
  307. BYTE data[1024];
  308. if (fp == NULL)
  309. {
  310. return -1;
  311. }
  312. Md5Init (&mdContext);
  313. while ((bytes = fread (data, 1, 1024, fp)) != 0)
  314. {
  315. Md5Update (&mdContext, data, bytes);
  316. }
  317. Md5Final (&mdContext);
  318. fclose(fp);
  319. if(md5ValueMax < 16)//容量不够
  320. {
  321. return -1;
  322. }
  323. else if(md5ValueMax < 32) //如果小于32
  324. {
  325. for (int i=0;i<16;i++)
  326. {
  327. md5Value[i] = mdContext.digest[i];
  328. }
  329. return 16;
  330. }
  331. else
  332. {
  333. for(int i=0; i<16; i++)
  334. {
  335. sprintf((char*)&md5Value[i*2], "%02x", mdContext.digest[i]);
  336. }
  337. return 32;
  338. }
  339. return -1;
  340. }
  341. /**
  342. * 随机数Md5加密
  343. *@plaintext 加密前的明文
  344. *@plaintextLen 明文长度
  345. *@randtext 随机内容
  346. *@randtextLen 随机内容长度
  347. *@encryption 存放密文的空间
  348. *@encryptionMax 密文空间最大容量
  349. *@return 成功返回加密后的内容长度
  350. * 失败返回 -1
  351. */
  352. int RandMd5(char* plaintext,int plaintextLen,char* randtext,int randtextLen,char* encryption,int encryptionMax)
  353. {
  354. for (int i=0;i<plaintextLen;i++)
  355. {
  356. plaintext[i] = plaintext[i] ^ randtext[i%randtextLen];
  357. }
  358. return Md5((BYTE*)plaintext,plaintextLen,(BYTE*)encryption,encryptionMax);
  359. }

md5Test.cpp

  1. // Md5Test.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <Md5.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. int main(int argc, char* argv[])
  8. {
  9. char *data = "12345678";
  10. char R[] ={'1','2','3','4'};
  11. char md5[_MD5_MAX + 1];
  12. char md5_2[_MD5_MAX + 1];
  13. memset(md5,0,sizeof(md5));
  14. Md5((BYTE*)data.GetBuffer(0),data.GetLength(),(BYTE*)md5,_MD5_MAX);
  15. printf("md5:%s\n",md5);
  16. memset(md5_2,0,sizeof(md5_2));
  17. for (int i=0;i<_MD5_MAX;i++)
  18. {
  19. md5[i] = md5[i]^(R[i%4]);
  20. }
  21. Md5((BYTE*)md5,_MD5_MAX,(BYTE*)md5_2,_MD5_MAX);
  22. printf("md5_2:%s\n",md5_2);
  23. getchar();
  24. return 0;
  25. }