用c++实现了md5算法。包含 md5.h 和md5.cpp 两个文件。
主要参考百度百科 “MD5” 原理的描述:http://baike.baidu.cn/view/7636.htm,代码中变量命名也是参考其中的公式,程序的使用说明在md5.h 文件的末尾注释中。
测试结果和百度百科测试例子一致。
实现过程中需要注意事项:最后把四个变量A B C D 链接成结果时 ,注意变量高低位的先后顺序,具体参考 LinkResult()方法。
md5.h
1 #ifndef _MD5_H_ 2 #define _MD5_H_ 3 4 #include <iostream> 5 #include <string> 6 using namespace std; 7 8 class MD5 9 { 10 public: 11 typedef unsigned char uchar8; //make sure it is 8bit 12 typedef char char8; //make sure it is 8bit 13 MD5(); 14 15 void init(); 16 17 void UpdateMd5(const uchar8 input[], const int length); 18 void UpdateMd5(const char8 input[], const int length); 19 20 void Finalize(); 21 22 void ComputMd5(const uchar8 input[], const int length); 23 void ComputMd5(const char8 input[], const int length); 24 25 string GetMd5(); 26 27 void printMd5(); 28 29 30 private: 31 typedef unsigned int uint32; //make sure it is 32 bit; 32 typedef unsigned long long uint64; //make sure it is 64 bit; 33 uint32 A, B, C, D; 34 const static int blockLen_ = 64; // 512/8 35 //the remain after last updata (because md5 may be computed segment by segment) 36 uchar8 remain_[blockLen_]; 37 int remainNum_ ; // the number of remain_, < 64 38 uint64 totalInputBits_; 39 uchar8 md5Result_[16]; //bit style md5 result,totally 128 bit 40 char md5Result_hex_[33]; //hexadecimal style result; md5Result_hex_[32]='\0' 41 bool isDone_; // indicate the comput is finished; 42 43 inline uint32 RotateLeft(const uint32 x, int n); 44 inline uint32 F(const uint32 x, const uint32 y, const uint32 z); 45 inline uint32 G(const uint32 x, const uint32 y, const uint32 z); 46 inline uint32 H(const uint32 x, const uint32 y, const uint32 z); 47 inline uint32 I(const uint32 x, const uint32 y, const uint32 z); 48 inline void FF(uint32 &a, const uint32 b, const uint32 c, const uint32 d, 49 const uint32 Mj, const int s, const uint32 ti); 50 inline void GG(uint32 &a, const uint32 b, const uint32 c, const uint32 d, 51 const uint32 Mj, const int s, const uint32 ti); 52 inline void HH(uint32 &a, const uint32 b, const uint32 c, const uint32 d, 53 const uint32 Mj, const int s, const uint32 ti); 54 inline void II(uint32 &a, const uint32 b, const uint32 c, const uint32 d, 55 const uint32 Mj, const int s, const uint32 ti); 56 57 58 void UcharToUint(uint32 output[], const uchar8 input[], const unsigned int transLength); 59 60 void FourRound(const uchar8 block[]); 61 62 void LinkResult(); 63 64 }; 65 66 /* user guide 67 you can comput the md5 by using the funtion ComputMd5 68 eg: 69 MD5 m; 70 MD5::char8 str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 71 m.ComputMd5(str,sizeof(str) - 1); 72 m.printMd5(); 73 74 if you want to comput segment by segment,you can do as follow, and init() is suggested 75 the begging,and Finalize() must call in the end: 76 77 MD5 M; 78 m.init(); 79 MD5::uchar8 str1[] = "ABCDEFGHIJKLMN"; 80 MD5::uchar8 str2[] = "OPQRSTUVWXYZabcdefghijk"; 81 MD5::uchar8 str3[] = "lmnopqrstuvwxyz"; 82 m.UpdateMd5(str1,sizeof(str1) - 1); 83 m.UpdateMd5(str2,sizeof(str2) - 1); 84 m.UpdateMd5(str3,sizeof(str3) - 1); 85 m.Finalize(); 86 m.printMd5(); 87 88 if you want to comput the md5 of a file, you can use the interface of this program. 89 */ 90 91 #endif