VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 22724|回复: 146

[转载] C++计算文件的MD5值

  [复制链接]
51_avatar_middle
online_admins 发表于 2011-11-6 14:14:31 | 显示全部楼层 |阅读模式
用C++计算文件的MD5值,相关的代码如下:

md5.h
  1. #ifndef MD5_H
  2. #define MD5_H

  3. #include <string>
  4. #include <fstream>

  5. /* Type define */
  6. typedef unsigned char byte;
  7. typedef unsigned int uint32;

  8. using std::string;
  9. using std::ifstream;

  10. /* MD5 declaration. */
  11. class MD5 {
  12. public:
  13. MD5();
  14. MD5(const void* input, size_t length);
  15. MD5(const string& str);
  16. MD5(ifstream& in);
  17. void update(const void* input, size_t length);
  18. void update(const string& str);
  19. void update(ifstream& in);
  20. const byte* digest();
  21. string toString();
  22. void reset();

  23. private:
  24. void update(const byte* input, size_t length);
  25. void final();
  26. void transform(const byte block[64]);
  27. void encode(const uint32* input, byte* output, size_t length);
  28. void decode(const byte* input, uint32* output, size_t length);
  29. string bytesToHexString(const byte* input, size_t length);

  30. /* class uncopyable */
  31. MD5(const MD5&);
  32. MD5& operator=(const MD5&);

  33. private:
  34. uint32 _state[4]; /* state (ABCD) */
  35. uint32 _count[2]; /* number of bits, modulo 2^64 (low-order word first) */
  36. byte _buffer[64]; /* input buffer */
  37. byte _digest[16]; /* message digest */
  38. bool _finished;   /* calculate finished ? */

  39. static const byte PADDING[64]; /* padding for calculate */
  40. static const char HEX[16];
  41. enum { BUFFER_SIZE = 1024 };
  42. };

  43. #endif /*MD5_H*/
复制代码
md5.cpp
  1. #include "md5.h"

  2. using namespace std;

  3. /* Constants for MD5Transform routine. */
  4. #define S11 7
  5. #define S12 12
  6. #define S13 17
  7. #define S14 22
  8. #define S21 5
  9. #define S22 9
  10. #define S23 14
  11. #define S24 20
  12. #define S31 4
  13. #define S32 11
  14. #define S33 16
  15. #define S34 23
  16. #define S41 6
  17. #define S42 10
  18. #define S43 15
  19. #define S44 21


  20. /* F, G, H and I are basic MD5 functions.
  21. */
  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. /* ROTATE_LEFT rotates x left n bits.
  27. */
  28. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))

  29. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  30. Rotation is separate from addition to prevent recomputation.
  31. */
  32. #define FF(a, b, c, d, x, s, ac) { \
  33. (a) += F ((b), (c), (d)) + (x) + ac; \
  34. (a) = ROTATE_LEFT ((a), (s)); \
  35. (a) += (b); \
  36. }
  37. #define GG(a, b, c, d, x, s, ac) { \
  38. (a) += G ((b), (c), (d)) + (x) + ac; \
  39. (a) = ROTATE_LEFT ((a), (s)); \
  40. (a) += (b); \
  41. }
  42. #define HH(a, b, c, d, x, s, ac) { \
  43. (a) += H ((b), (c), (d)) + (x) + ac; \
  44. (a) = ROTATE_LEFT ((a), (s)); \
  45. (a) += (b); \
  46. }
  47. #define II(a, b, c, d, x, s, ac) { \
  48. (a) += I ((b), (c), (d)) + (x) + ac; \
  49. (a) = ROTATE_LEFT ((a), (s)); \
  50. (a) += (b); \
  51. }


  52. const byte MD5::PADDING[64] = { 0x80 };
  53. const char MD5::HEX[16] = {
  54. '0', '1', '2', '3',
  55. '4', '5', '6', '7',
  56. '8', '9', 'a', 'b',
  57. 'c', 'd', 'e', 'f'
  58. };


  59. /* Default construct. */
  60. MD5::MD5() {
  61. reset();
  62. }

  63. /* Construct a MD5 object with a input buffer. */
  64. MD5::MD5(const void* input, size_t length) {
  65. reset();
  66. update(input, length);
  67. }

  68. /* Construct a MD5 object with a string. */
  69. MD5::MD5(const string& str) {
  70. reset();
  71. update(str);
  72. }

  73. /* Construct a MD5 object with a file. */
  74. MD5::MD5(ifstream& in) {
  75. reset();
  76. update(in);
  77. }

  78. /* Return the message-digest */
  79. const byte* MD5::digest() {

  80. if (!_finished) {
  81.    _finished = true;
  82.    final();
  83. }
  84. return _digest;
  85. }

  86. /* Reset the calculate state */
  87. void MD5::reset() {

  88. _finished = false;
  89. /* reset number of bits. */
  90. _count[0] = _count[1] = 0;
  91. /* Load magic initialization constants. */
  92. _state[0] = 0x67452301;
  93. _state[1] = 0xefcdab89;
  94. _state[2] = 0x98badcfe;
  95. _state[3] = 0x10325476;
  96. }

  97. /* Updating the context with a input buffer. */
  98. void MD5::update(const void* input, size_t length) {
  99. update((const byte*)input, length);
  100. }

  101. /* Updating the context with a string. */
  102. void MD5::update(const string& str) {
  103. update((const byte*)str.c_str(), str.length());
  104. }

  105. /* Updating the context with a file. */
  106. void MD5::update(ifstream& in) {

  107. if (!in) {
  108.    return;
  109. }

  110. std::streamsize length;
  111. char buffer[BUFFER_SIZE];
  112. while (!in.eof()) {
  113.    in.read(buffer, BUFFER_SIZE);
  114.    length = in.gcount();
  115.    if (length > 0) {
  116.     update(buffer, length);
  117.    }
  118. }
  119. in.close();
  120. }

  121. /* MD5 block update operation. Continues an MD5 message-digest
  122. operation, processing another message block, and updating the
  123. context.
  124. */
  125. void MD5::update(const byte* input, size_t length) {

  126. uint32 i, index, partLen;

  127. _finished = false;

  128. /* Compute number of bytes mod 64 */
  129. index = (uint32)((_count[0] >> 3) & 0x3f);

  130. /* update number of bits */
  131. if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3)) {
  132.    ++_count[1];
  133. }
  134. _count[1] += ((uint32)length >> 29);

  135. partLen = 64 - index;

  136. /* transform as many times as possible. */
  137. if (length >= partLen) {

  138.    memcpy(&_buffer[index], input, partLen);
  139.    transform(_buffer);

  140.    for (i = partLen; i + 63 < length; i += 64) {
  141.     transform(&input[i]);
  142.    }
  143.    index = 0;

  144. } else {
  145.    i = 0;
  146. }

  147. /* Buffer remaining input */
  148. memcpy(&_buffer[index], &input[i], length - i);
  149. }

  150. /* MD5 finalization. Ends an MD5 message-_digest operation, writing the
  151. the message _digest and zeroizing the context.
  152. */
  153. void MD5::final() {

  154. byte bits[8];
  155. uint32 oldState[4];
  156. uint32 oldCount[2];
  157. uint32 index, padLen;

  158. /* Save current state and count. */
  159. memcpy(oldState, _state, 16);
  160. memcpy(oldCount, _count, 8);

  161. /* Save number of bits */
  162. encode(_count, bits, 8);

  163. /* Pad out to 56 mod 64. */
  164. index = (uint32)((_count[0] >> 3) & 0x3f);
  165. padLen = (index < 56) ? (56 - index) : (120 - index);
  166. update(PADDING, padLen);

  167. /* Append length (before padding) */
  168. update(bits, 8);

  169. /* Store state in digest */
  170. encode(_state, _digest, 16);

  171. /* Restore current state and count. */
  172. memcpy(_state, oldState, 16);
  173. memcpy(_count, oldCount, 8);
  174. }

  175. /* MD5 basic transformation. Transforms _state based on block. */
  176. void MD5::transform(const byte block[64]) {

  177. uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16];

  178. decode(block, x, 64);

  179. /* Round 1 */
  180. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  181. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  182. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  183. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  184. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  185. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  186. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  187. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  188. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  189. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  190. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  191. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  192. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  193. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  194. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  195. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */

  196. /* Round 2 */
  197. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  198. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  199. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  200. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  201. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  202. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  203. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  204. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  205. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  206. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  207. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  208. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  209. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  210. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  211. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  212. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */

  213. /* Round 3 */
  214. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  215. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  216. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  217. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  218. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  219. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  220. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  221. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  222. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  223. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  224. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  225. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  226. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  227. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  228. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  229. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */

  230. /* Round 4 */
  231. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  232. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  233. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  234. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  235. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  236. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  237. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  238. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  239. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  240. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  241. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  242. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  243. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  244. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  245. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  246. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */

  247. _state[0] += a;
  248. _state[1] += b;
  249. _state[2] += c;
  250. _state[3] += d;
  251. }

  252. /* Encodes input (ulong) into output (byte). Assumes length is
  253. a multiple of 4.
  254. */
  255. void MD5::encode(const uint32* input, byte* output, size_t length) {

  256. for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
  257.    output[j]= (byte)(input[i] & 0xff);
  258.    output[j + 1] = (byte)((input[i] >> 8) & 0xff);
  259.    output[j + 2] = (byte)((input[i] >> 16) & 0xff);
  260.    output[j + 3] = (byte)((input[i] >> 24) & 0xff);
  261. }
  262. }

  263. /* Decodes input (byte) into output (ulong). Assumes length is
  264. a multiple of 4.
  265. */
  266. void MD5::decode(const byte* input, uint32* output, size_t length) {

  267. for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
  268.    output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) |
  269.    (((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24);
  270. }
  271. }

  272. /* Convert byte array to hex string. */
  273. string MD5::bytesToHexString(const byte* input, size_t length) {

  274. string str;
  275. str.reserve(length << 1);
  276. for (size_t i = 0; i < length; ++i) {
  277.    int t = input[i];
  278.    int a = t / 16;
  279.    int b = t % 16;
  280.    str.append(1, HEX[a]);
  281.    str.append(1, HEX[b]);
  282. }
  283. return str;
  284. }

  285. /* Convert digest to string value */
  286. string MD5::toString() {
  287. return bytesToHexString(digest(), 16);
  288. }
复制代码
test.cpp
  1. #include "md5.h"
  2. #include <iostream>

  3. using namespace std;

  4. void PrintMD5(const string& str, MD5& md5) {
  5.       cout << "MD5("" << str << "") = " << md5.toString() << endl;
  6. }

  7. int main() {

  8.      MD5 md5;
  9.      md5.update("");
  10.      PrintMD5("", md5);

  11.      md5.update("a");
  12.      PrintMD5("a", md5);

  13.      md5.update("bc");
  14.      PrintMD5("abc", md5);

  15.      md5.update("defghijklmnopqrstuvwxyz");
  16.      PrintMD5("abcdefghijklmnopqrstuvwxyz", md5);

  17.      md5.reset();
  18.      md5.update("message digest");
  19.      PrintMD5("message digest", md5);

  20.      md5.reset();
  21.      md5.update(ifstream("D:\\test.txt"));
  22.      PrintMD5("D:\\test.txt", md5);

  23.     return 0;
  24. }
复制代码
测试结果:
MD5("") = d41d8cd98f00b204e9800998ecf8427e
MD5("a") = 0cc175b9c0f1b6a831c399e269772661
MD5("abc") = 900150983cd24fb0d6963f7d28e17f72
MD5("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
MD5("message digest") = f96b697d7cb7938d525a2f31aaf161d0
MD5("D:\test.txt") = 7ac66c0f148de9519b8bd264312c4d64

相关源代码文件:
游客,如果您要查看本帖隐藏内容请回复




上一篇:高仿QQ的IM聊天软件
下一篇:面试时遇到的一题C++上机题,与大家分享一下
85_avatar_middle
在线会员 发表于 2011-11-6 14:52:23 | 显示全部楼层
喜欢这算法,谢谢分享
90_avatar_middle
在线会员 发表于 2011-11-6 15:04:51 | 显示全部楼层
收藏了,谢谢!!!
45_avatar_middle
在线会员 发表于 2011-11-6 18:52:18 | 显示全部楼层
MARKC++计算文件的MD5值
18_avatar_middle
在线会员 发表于 2011-11-6 19:30:25 | 显示全部楼层
谢谢楼主分享 .. 收下研究 ...
45_avatar_middle
在线会员 发表于 2011-11-6 19:55:26 | 显示全部楼层
MD5啊,学习了
68_avatar_middle
在线会员 发表于 2011-11-6 20:52:24 | 显示全部楼层
C++计算文件的MD5值C++计算文件的MD5值
64_avatar_middle
在线会员 发表于 2011-11-6 23:19:34 | 显示全部楼层
C++计算文件的MD5值看看看看····
94_avatar_middle
online_vip 发表于 2011-11-6 23:56:37 | 显示全部楼层
看看。。。。。。。。。。
42_avatar_middle
在线会员 发表于 2011-11-7 00:54:58 | 显示全部楼层
0cc175b9c0f1b6a831c399e269772661
MD5("abc") = 900150983cd24fb0d6963f7d28e17f72
98_avatar_middle
online_vip 发表于 2011-11-7 11:17:08 | 显示全部楼层
感谢楼主!1谢谢了
50_avatar_middle
在线会员 发表于 2011-11-8 22:44:48 | 显示全部楼层
C++计算文件的MD5值C++计算文件的MD5值
90_avatar_middle
在线会员 发表于 2011-11-9 20:14:50 | 显示全部楼层
谢谢分享!
08_avatar_middle
在线会员 发表于 2011-11-9 20:24:03 | 显示全部楼层
md5算法。。。不错。。。哈哈
20_avatar_middle
在线会员 发表于 2011-11-11 21:53:29 | 显示全部楼层
Look 一 Look !!!
41_avatar_middle
在线会员 发表于 2011-11-13 15:53:05 | 显示全部楼层
挺好的,不错,谢谢分享
00_avatar_middle
在线会员 发表于 2011-11-16 22:44:57 | 显示全部楼层
好东西啊!!!
93_avatar_middle
在线会员 发表于 2011-11-21 16:10:41 | 显示全部楼层
很赞。。。。。
11_avatar_middle
在线会员 发表于 2011-11-21 16:33:52 | 显示全部楼层
很不错,谢谢分享
77_avatar_middle
在线会员 发表于 2011-11-23 17:02:01 | 显示全部楼层
C++计算文件的MD5值C++计算文件的MD5值
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号tongdun|网站地图wx_jqr

GMT+8, 2019-3-21 08:39

Powered by Discuz! X3.4

© 2009-2019 cctry.com

快速回复 返回顶部 返回列表