/* Convert byte array to hex string. */
string MD5::bytesToHexString(const byte *input, size_t length) {
string str;
str.reserve(length << 1);
for(size_t i = 0; i < length; i++) {
int t = input[i];
int a = t / 16;
int b = t % 16;
str.append(1, HEX[a]);
str.append(1, HEX[b]);
}
return str;
}
/* Convert digest to string value */
string MD5::toString() {
return bytesToHexString(digest(), 16);
}
----------------------------------
md5头文件代码如下:
#ifndef MD5_H
#define MD5_H
#include <string>
#include <fstream>
/* Type define */
typedef unsigned char byte;
typedef unsigned long ulong;