测试结果和百度百科测试例子一致。
实现过程中需要注意事项:最后把四个变量A B C D 链接成结果时 ,注意变量高低位的先后顺序,具体参考 linkResult()方法。
md5.h
#ifndef _MD5_H_ #define _MD5_H_ #include#include using namespace std; class MD5 { public: typedef unsigned char uchar8; //make sure it is 8bit typedef char char8; //make sure it is 8bit MD5(); void init(); void UpdateMd5(const uchar8 input[], const int length); void UpdateMd5(const char8 input[], const int length); void Finalize(); void ComputMd5(const uchar8 input[], const int length); void ComputMd5(const char8 input[], const int length); string GetMd5(); void printMd5(); private: typedef unsigned int uint32; //make sure it is 32 bit; typedef unsigned long long uint64; //make sure it is 64 bit; uint32 A, B, C, D; const static int blockLen_ = 64; // 512/8 //the remain after last updata (because md5 may be computed segment by segment) uchar8 remain_[blockLen_]; int remainNum_ ; // the number of remain_, < 64 uint64 totalInputBits_; uchar8 md5Result_[16]; //bit style md5 result,totally 128 bit char md5Result_hex_[33]; //hexadecimal style result; md5Result_hex_[32]=' ' bool isDone_; // indicate the comput is finished; inline uint32 RotateLeft(const uint32 x, int n); inline uint32 F(const uint32 x, const uint32 y, const uint32 z); inline uint32 G(const uint32 x, const uint32 y, const uint32 z); inline uint32 H(const uint32 x, const uint32 y, const uint32 z); inline uint32 I(const uint32 x, const uint32 y, const uint32 z); inline void FF(uint32 &a, const uint32 b, const uint32 c, const uint32 d, const uint32 Mj, const int s, const uint32 ti); inline void GG(uint32 &a, const uint32 b, const uint32 c, const uint32 d, const uint32 Mj, const int s, const uint32 ti); inline void HH(uint32 &a, const uint32 b, const uint32 c, const uint32 d, const uint32 Mj, const int s, const uint32 ti); inline void II(uint32 &a, const uint32 b, const uint32 c, const uint32 d, const uint32 Mj, const int s, const uint32 ti); void UcharToUint(uint32 output[], const uchar8 input[], const unsigned int transLength); void FourRound(const uchar8 block[]); void linkResult(); }; #endif
md5.cpp
#include"md5.h" #includeusing namespace std; const int S[4][4] = {7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21}; void MD5::init() { A = 0x67452301; B = 0xefcdab89; C = 0x98badcfe; D = 0x10325476; remainNum_ = 0; remain_[0] = ' '; md5Result_hex_[0] = ' '; md5Result_[0] = ' '; totalInputBits_ = 0; isDone_ = false; } MD5::MD5() { init(); } inline MD5::uint32 MD5::RotateLeft(const uint32 x, int n) { return (x << n) | (x >> (32-n)); // if x is signed, use: (x << n) | ((x & 0xFFFFFFFF) >> (32-n)) } inline MD5::uint32 MD5::F(const uint32 x, const uint32 y, const uint32 z) { return (x & y) | ((~x) & z); } inline MD5::uint32 MD5::G(const uint32 x, const uint32 y, const uint32 z) { return (x & z) | (y & (~z)); } inline MD5::uint32 MD5::H(const uint32 x, const uint32 y, const uint32 z) { return x ^ y ^ z; } inline MD5::uint32 MD5::I(const uint32 x, const uint32 y, const uint32 z) { return y ^ (x | (~z)); } inline void MD5::FF(uint32 &a, const uint32 b, const uint32 c, const uint32 d, const uint32 Mj, const int s, const uint32 ti) { a = b + RotateLeft(a + F(b, c, d) + Mj + ti, s); } inline void MD5::GG(uint32 &a, const uint32 b, const uint32 c, const uint32 d, const uint32 Mj, const int s, const uint32 ti) { a = b + RotateLeft(a + G(b, c, d) + Mj + ti, s); } inline void MD5::HH(uint32 &a, const uint32 b, const uint32 c, const uint32 d, const uint32 Mj, const int s, const uint32 ti) { a = b + RotateLeft(a + H(b, c, d) + Mj + ti, s); } inline void MD5::II(uint32 &a, const uint32 b, const uint32 c, const uint32 d, const uint32 Mj, const int s, const uint32 ti) { a = b + RotateLeft(a + I(b, c, d) + Mj + ti, s); } // link A B C D to result(bit style result and hexadecimal style result) void MD5::linkResult() { //bit style result for(int i = 0; i < 4; i++) //link A: low to high { md5Result_[i] = (A >> 8*i) & 0xff; } for(int i = 4; i<8; i++) //link B: low to high { md5Result_[i] = (B >> 8*(i - 4)) & 0xff; } for(int i = 8; i<12; i++) //link C: low to high { md5Result_[i] = (C >> 8*(i - 8)) & 0xff; } for(int i = 12; i<16; i++) //link D: low to high { md5Result_[i] = (D >> 8*(i - 12)) & 0xff; } //change to hexadecimal style result // note: it is not the same as simply link hex(A) hex(B) hex(C) hex(D) for(int i = 0; i < 16; i++) sprintf(& md5Result_hex_[i*2], "%02x", md5Result_[i]); md5Result_hex_[32] = ' '; } //print the md5 by hex void MD5::printMd5() { if(!isDone_) { cout<< "Error: computation is not finished" < 0) { UpdateMd5(padding, temp); totalInputBits_ -= (temp << 3); } else if(temp < 0) { UpdateMd5(padding, 64 + temp); totalInputBits_ -= ((64 + temp) << 3); } // trans totalInputBits_ to uchar8 (64bits) uchar8 Bits[8]; for(int i = 0; i < 8; i++) { Bits[i] = (totalInputBits_ >> 8*i) & 0xff; } UpdateMd5(Bits, 8); // add the number of original input (the last 64bits) linkResult(); isDone_ = true; } // comput the md5 based on input, (just this one input) void MD5::ComputMd5(const uchar8 input[], const int length) { init(); UpdateMd5(input, length); Finalize(); } void MD5::ComputMd5(const char8 input[], const int length) { ComputMd5((const uchar8 *)input, length); }



