c++实现MD5算法实现代码
本文导语: 测试结果和百度百科测试例子一致。 实现过程中需要注意事项:最后把四个变量A B C D 链接成结果时 ,注意变量高低位的先后顺序,具体参考 LinkResult()方法。 md5.h 代码如下:#ifndef _MD5_H_#define _MD5_H_ #include #include using namespace...
测试结果和百度百科测试例子一致。
实现过程中需要注意事项:最后把四个变量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();
};
/* user guide
you can comput the md5 by using the funtion ComputMd5
eg:
MD5 m;
MD5::char8 str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
m.ComputMd5(str,sizeof(str) - 1);
m.printMd5();
if you want to comput segment by segment,you can do as follow, and init() is suggested
the begging,and Finalize() must call in the end:
MD5 M;
m.init();
MD5::uchar8 str1[] = "ABCDEFGHIJKLMN";
MD5::uchar8 str2[] = "OPQRSTUVWXYZabcdefghijk";
MD5::uchar8 str3[] = "lmnopqrstuvwxyz";
m.UpdateMd5(str1,sizeof(str1) - 1);
m.UpdateMd5(str2,sizeof(str2) - 1);
m.UpdateMd5(str3,sizeof(str3) - 1);
m.Finalize();
m.printMd5();
if you want to comput the md5 of a file, you can use the interface of this program.
*/
#endif
md5.cpp
#include"md5.h"
#include
using 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 > (32-n));
// if x is signed, use: (x > (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 - 4)) & 0xff;
}
for(int i = 8; i> 8*(i - 8)) & 0xff;
}
for(int i = 12; i> 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