VC++取MD5算法记录下以后用得到(转)
这个是网上扒下来的 作者已经无法知道是谁了
MD5.h
1 #ifndef MD5_H 2 #define MD5_H 3 4 #include <string> 5 #include <fstream> 6 7 /* Type define */ 8 typedef unsigned char byte; 9 typedef unsigned int uint32; 10 typedef unsigned int uint4; 11 12 using std::string; 13 using std::ifstream; 14 15 /* MD5 declaration. */ 16 class MD5 { 17 public: 18 MD5(); 19 MD5(const void *input, size_t length); 20 MD5(const string &str); 21 MD5(ifstream &in); 22 void update(const void *input, size_t length); 23 void update(const string &str); 24 void update(ifstream &in); 25 const byte* digest(); 26 string toString(); 27 void reset(); 28 29 inline uint4 rotate_left(uint4 x, int n); 30 inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 31 inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 32 inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 33 inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac); 34 35 private: 36 void update(const byte *input, size_t length); 37 void final(); 38 void transform(const byte block[64]); 39 void encode(const uint32 *input, byte *output, size_t length); 40 void decode(const byte *input, uint32 *output, size_t length); 41 string bytesToHexString(const byte *input, size_t length); 42 43 /* class uncopyable */ 44 MD5(const MD5&); 45 MD5& operator=(const MD5&); 46 private: 47 uint32 _state[4]; /* state (ABCD) */ 48 uint32 _count[2]; /* number of bits, modulo 2^64 (low-order word first) */ 49 byte _buffer[64]; /* input buffer */ 50 byte _digest[16]; /* message digest */ 51 bool _finished; /* calculate finished ? */ 52 53 static const byte PADDING[64]; /* padding for calculate */ 54 static const char HEX[16]; 55 static const size_t BUFFER_SIZE = 1024; 56 }; 57 58 string FileDigest(const string &file); 59 60 #endif/*MD5_H*/
MD5.cpp
1 #include "md5.h" 2 3 using namespace std; 4 5 /* Constants for MD5Transform routine. */ 6 #define S11 7 7 #define S12 12 8 #define S13 17 9 #define S14 22 10 #define S21 5 11 #define S22 9 12 #define S23 14 13 #define S24 20 14 #define S31 4 15 #define S32 11 16 #define S33 16 17 #define S34 23 18 #define S41 6 19 #define S42 10 20 #define S43 15 21 #define S44 21 22 23 24 /* F, G, H and I are basic MD5 functions. 25 */ 26 #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 27 #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) 28 #define H(x, y, z) ((x) ^ (y) ^ (z)) 29 #define I(x, y, z) ((y) ^ ((x) | (~z))) 30 31 /* ROTATE_LEFT rotates x left n bits. 32 */ 33 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 34 35 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 36 Rotation is separate from addition to prevent recomputation. 37 */ 38 //#define FF(a, b, c, d, x, s, ac) { \ 39 //(a) += F((b), (c), (d)) + (x)+ac; \ 40 //(a) = ROTATE_LEFT((a), (s)); \ 41 //(a) += (b); \ 42 //} 43 //#define GG(a, b, c, d, x, s, ac) { \ 44 //(a) += G((b), (c), (d)) + (x)+ac; \ 45 //(a) = ROTATE_LEFT((a), (s)); \ 46 //(a) += (b); \ 47 //} 48 //#define HH(a, b, c, d, x, s, ac) { \ 49 //(a) += H((b), (c), (d)) + (x)+ac; \ 50 //(a) = ROTATE_LEFT((a), (s)); \ 51 //(a) += (b); \ 52 //} 53 //#define II(a, b, c, d, x, s, ac) { \ 54 //(a) += I((b), (c), (d)) + (x)+ac; \ 55 //(a) = ROTATE_LEFT((a), (s)); \ 56 //(a) += (b); \ 57 //} 58 59 inline uint4 MD5::rotate_left(uint4 x, int n) { 60 return (x << n) | (x >> (32 - n)); 61 } 62 63 inline void MD5::FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) { 64 a = rotate_left(a + F(b, c, d) + x + ac, s) + b; 65 } 66 67 inline void MD5::GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) { 68 a = rotate_left(a + G(b, c, d) + x + ac, s) + b; 69 } 70 71 inline void MD5::HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) { 72 a = rotate_left(a + H(b, c, d) + x + ac, s) + b; 73 } 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 const byte MD5::PADDING[64] = { 0x80 }; 80 const char MD5::HEX[16] = { 81 '0', '1', '2', '3', 82 '4', '5', '6', '7', 83 '8', '9', 'a', 'b', 84 'c', 'd', 'e', 'f' 85 }; 86 87 /* Default construct. */ 88 MD5::MD5() { 89 reset(); 90 } 91 92 /* Construct a MD5 object with a input buffer. */ 93 MD5::MD5(const void *input, size_t length) { 94 reset(); 95 update(input, length); 96 } 97 98 /* Construct a MD5 object with a string. */ 99 MD5::MD5(const string &str) { 100 reset(); 101 update(str); 102 } 103 104 /* Construct a MD5 object with a file. */ 105 MD5::MD5(ifstream &in) { 106 reset(); 107 update(in); 108 } 109 110 /* Return the message-digest */ 111 const byte* MD5::digest() { 112 if (!_finished) { 113 _finished = true; 114 final(); 115 } 116 return _digest; 117 } 118 119 /* Reset the calculate state */ 120 void MD5::reset() { 121 122 _finished = false; 123 /* reset number of bits. */ 124 _count[0] = _count[1] = 0; 125 /* Load magic initialization constants. */ 126 _state[0] = 0x67452301; 127 _state[1] = 0xefcdab89; 128 _state[2] = 0x98badcfe; 129 _state[3] = 0x10325476; 130 } 131 132 /* Updating the context with a input buffer. */ 133 void MD5::update(const void *input, size_t length) { 134 update((const byte*)input, length); 135 } 136 137 /* Updating the context with a string. */ 138 void MD5::update(const string &str) { 139 update((const byte*)str.c_str(), str.length()); 140 } 141 142 /* Updating the context with a file. */ 143 void MD5::update(ifstream &in) { 144 145 if (!in) 146 return; 147 148 std::streamsize length; 149 char buffer[BUFFER_SIZE]; 150 while (!in.eof()) { 151 in.read(buffer, BUFFER_SIZE); 152 length = in.gcount(); 153 if (length > 0) 154 update(buffer, length); 155 } 156 in.close(); 157 } 158 159 /* MD5 block update operation. Continues an MD5 message-digest 160 operation, processing another message block, and updating the 161 context. 162 */ 163 void MD5::update(const byte *input, size_t length) { 164 165 uint32 i, index, partLen; 166 167 _finished = false; 168 169 /* Compute number of bytes mod 64 */ 170 index = (uint32)((_count[0] >> 3) & 0x3f); 171 172 /* update number of bits */ 173 if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3)) 174 _count[1]++; 175 _count[1] += ((uint32)length >> 29); 176 177 partLen = 64 - index; 178 179 /* transform as many times as possible. */ 180 if (length >= partLen) { 181 182 memcpy(&_buffer[index], input, partLen); 183 transform(_buffer); 184 185 for (i = partLen; i + 63 < length; i += 64) 186 transform(&input[i]); 187 index = 0; 188 189 } 190 else { 191 i = 0; 192 } 193 194 /* Buffer remaining input */ 195 memcpy(&_buffer[index], &input[i], length - i); 196 } 197 198 /* MD5 finalization. Ends an MD5 message-_digest operation, writing the 199 the message _digest and zeroizing the context. 200 */ 201 void MD5::final() { 202 203 byte bits[8]; 204 uint32 oldState[4]; 205 uint32 oldCount[2]; 206 uint32 index, padLen; 207 208 /* Save current state and count. */ 209 memcpy(oldState, _state, 16); 210 memcpy(oldCount, _count, 8); 211 212 /* Save number of bits */ 213 encode(_count, bits, 8); 214 215 /* Pad out to 56 mod 64. */ 216 index = (uint32)((_count[0] >> 3) & 0x3f); 217 padLen = (index < 56) ? (56 - index) : (120 - index); 218 update(PADDING, padLen); 219 220 /* Append length (before padding) */ 221 update(bits, 8); 222 223 /* Store state in digest */ 224 encode(_state, _digest, 16); 225 226 /* Restore current state and count. */ 227 memcpy(_state, oldState, 16); 228 memcpy(_count, oldCount, 8); 229 } 230 231 /* MD5 basic transformation. Transforms _state based on block. */ 232 void MD5::transform(const byte block[64]) { 233 234 uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16]; 235 236 decode(block, x, 64); 237 238 /* Round 1 */ 239 FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */ 240 FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */ 241 FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */ 242 FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */ 243 FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */ 244 FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */ 245 FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */ 246 FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */ 247 FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */ 248 FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */ 249 FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ 250 FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ 251 FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ 252 FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ 253 FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ 254 FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ 255 256 /* Round 2 */ 257 GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */ 258 GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */ 259 GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ 260 GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */ 261 GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */ 262 GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */ 263 GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ 264 GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */ 265 GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */ 266 GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ 267 GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */ 268 GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */ 269 GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ 270 GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */ 271 GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */ 272 GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ 273 274 /* Round 3 */ 275 HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */ 276 HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */ 277 HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ 278 HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ 279 HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */ 280 HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */ 281 HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */ 282 HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ 283 HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ 284 HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */ 285 HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */ 286 HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */ 287 HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */ 288 HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ 289 HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ 290 HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */ 291 292 /* Round 4 */ 293 II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */ 294 II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */ 295 II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ 296 II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */ 297 II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ 298 II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */ 299 II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ 300 II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */ 301 II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */ 302 II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ 303 II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */ 304 II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ 305 II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */ 306 II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ 307 II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */ 308 II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */ 309 310 _state[0] += a; 311 _state[1] += b; 312 _state[2] += c; 313 _state[3] += d; 314 } 315 316 /* Encodes input (ulong) into output (byte). Assumes length is 317 a multiple of 4. 318 */ 319 void MD5::encode(const uint32 *input, byte *output, size_t length) { 320 321 for (size_t i = 0, j = 0; j<length; i++, j += 4) { 322 output[j] = (byte)(input[i] & 0xff); 323 output[j + 1] = (byte)((input[i] >> 8) & 0xff); 324 output[j + 2] = (byte)((input[i] >> 16) & 0xff); 325 output[j + 3] = (byte)((input[i] >> 24) & 0xff); 326 } 327 } 328 329 /* Decodes input (byte) into output (ulong). Assumes length is 330 a multiple of 4. 331 */ 332 void MD5::decode(const byte *input, uint32 *output, size_t length) { 333 334 for (size_t i = 0, j = 0; j<length; i++, j += 4) { 335 output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) | 336 (((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24); 337 } 338 } 339 340 /* Convert byte array to hex string. */ 341 string MD5::bytesToHexString(const byte *input, size_t length) { 342 string str; 343 str.reserve(length << 1); 344 for (size_t i = 0; i < length; i++) { 345 int t = input[i]; 346 int a = t / 16; 347 int b = t % 16; 348 str.append(1, HEX[a]); 349 str.append(1, HEX[b]); 350 } 351 return str; 352 } 353 354 /* Convert digest to string value */ 355 string MD5::toString() { 356 return bytesToHexString(digest(), 16); 357 } 358 359 360 //得到二进制文件的MD5码 361 string FileDigest(const string &file) { 362 ifstream in(file.c_str(), ios::binary); 363 if (!in) 364 return ""; 365 366 MD5 md5; 367 md5.reset(); 368 std::streamsize length; 369 char buffer[1024]; 370 while (!in.eof()) { 371 in.read(buffer, 1024); 372 length = in.gcount(); 373 if (length > 0) 374 md5.update(buffer, length); 375 } 376 in.close(); 377 return md5.toString(); 378 }
调用main.cpp
1 // Md5Test.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "MD5.h" 6 #include <fstream> 7 #include <process.h> 8 int main() 9 { 10 std::ifstream of("ReadMe.txt", std::ios::in | std::ios::binary); 11 if (!of.is_open())return 0; 12 MD5 fileMd5(of); 13 printf("%s\r\n", fileMd5.toString().c_str()); 14 MD5 strMd5("123456789"); 15 printf("%s\r\n", strMd5.toString().c_str()); 16 system("pause"); 17 return 0; 18 }