MD5 源码(一次自私的发帖, 还有帖子里的代码非本人原创,请作者看到后跟下贴,那大伙儿都知道)

hongzao 2009-05-28 10:05:03

#ifndef MD5_H
#define MD5_H

#include <string>
#include <fstream>

/* Type define */
typedef unsigned char Byte;
typedef unsigned int UInt32;
typedef size_t SizeType;

using std::string;
using std::ifstream;

/* MD5 declaration. */
class MD5 {
public:
MD5();
MD5(const void* input, SizeType length);
MD5(const string& str);
MD5(ifstream& in);
void update(const void* input, SizeType length);
void update(const string& str);
void update(ifstream& in);
const Byte* digest();
string toString();
void reset();

private:
void update(const Byte* input, SizeType length);
void final();
void transform(const Byte block[64]);
void encode(const UInt32* input, Byte* output, SizeType length);
void decode(const Byte* input, UInt32* output, SizeType length);
string bytesToHexString(const Byte* input, SizeType length);

/* class uncopyable */
MD5(const MD5&);
MD5& operator=(const MD5&);

private:
UInt32 state_[4]; /* state (ABCD) */
UInt32 count_[2]; /* number of bits, modulo 2^64 (low-order word first) */
Byte buffer_[64]; /* input buffer */
Byte digest_[16]; /* message digest */
bool isFinished_; /* calculate finished ? */

static const Byte Padding[64]; /* padding for calculate */
static const char Hex[16];
static const int BufferSize = 1024;
};

#endif /*MD5_H*/

...全文
118 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
eatsweetpotato 2009-05-28
  • 打赏
  • 举报
回复
mark
hongzao 2009-05-28
  • 打赏
  • 举报
回复

#include "md5.h"
#include <iostream>

using namespace std;

void PrintMD5(const string& str, MD5& md5) {
cout << "MD5(\"" << str << "\") = " << md5.toString() << endl;
}

string FileDigest(const string& file) {

ifstream in(file.c_str(), ios::binary);
if (!in) {
return "";
}

MD5 md5;
std::streamsize length;
char buffer[1024];
while (!in.eof()) {
in.read(buffer, 1024);
length = in.gcount();
if (length > 0) {
md5.update(buffer, length);
}
}
in.close();
return md5.toString();
}

int main() {

//cout << MD5("abc").toString() << endl;
//cout << MD5(ifstream("D:\\test.txt")).toString() << endl;
//cout << MD5(ifstream("D:\\test.exe", ios::binary)).toString() << endl;
//cout << FileDigest("D:\\test.exe") << endl;

MD5 md5;
md5.update("");
PrintMD5("", md5);

md5.update("a");
PrintMD5("a", md5);

md5.update("bc");
PrintMD5("abc", md5);

md5.update("defghijklmnopqrstuvwxyz");
PrintMD5("abcdefghijklmnopqrstuvwxyz", md5);

md5.reset();
md5.update("message digest");
PrintMD5("message digest", md5);

md5.reset();
md5.update("sdfasdfadfasdfasdf");
PrintMD5("sdfasdfadfasdfasdf", md5);

//md5.reset();
//md5.update(ifstream("D:\\test.txt"));
//PrintMD5("D:\\test.txt", md5);
return 0;
}


"" = d41d8cd98f00b204e9800998ecf8427e
"a" = 0cc175b9c0f1b6a831c399e269772661
"abc" = 900150983cd24fb0d6963f7d28e17f72
"abcdefghijklmnopqrstuvwxyz" = c3fcd3d76192e4007dfb496cca67e13b
"message digest" = f96b697d7cb7938d525a2f31aaf161d0
"sdfasdfadfasdfasdf" = 6dc48f52b4d534a46923d4d354937433
hongzao 2009-05-28
  • 打赏
  • 举报
回复
现在在家找到的一个源码,准备到公司用,但公司是不能上博客和下东西,所以只好放到这里来了
yefei679 2009-05-28
  • 打赏
  • 举报
回复
LZ这是做什么?
hongzao 2009-05-28
  • 打赏
  • 举报
回复

/* MD5 basic transformation. Transforms state_ based on block. */
void MD5::transform(const Byte block[64]) {

UInt32 a = state_[0], b = state_[1], c = state_[2], d = state_[3], x[16];

decode(block, x, 64);

/* Round 1 */
FF (a, b, c, d, x[ 0], S[0][0], 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S[0][1], 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S[0][2], 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S[0][3], 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S[0][0], 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S[0][1], 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S[0][2], 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S[0][3], 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S[0][0], 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S[0][1], 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S[0][2], 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S[0][3], 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S[0][0], 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S[0][1], 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S[0][2], 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S[0][3], 0x49b40821); /* 16 */

/* Round 2 */
GG (a, b, c, d, x[ 1], S[1][0], 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S[1][1], 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S[1][2], 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S[1][3], 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S[1][0], 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S[1][1], 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S[1][2], 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S[1][3], 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S[1][0], 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S[1][1], 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S[1][2], 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S[1][3], 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S[1][0], 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S[1][1], 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S[1][2], 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S[1][3], 0x8d2a4c8a); /* 32 */

/* Round 3 */
HH (a, b, c, d, x[ 5], S[2][0], 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S[2][1], 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S[2][2], 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S[2][3], 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S[2][0], 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S[2][1], 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S[2][2], 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S[2][3], 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S[2][0], 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S[2][1], 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S[2][2], 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S[2][3], 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S[2][0], 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S[2][1], 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S[2][2], 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S[2][3], 0xc4ac5665); /* 48 */

/* Round 4 */
II (a, b, c, d, x[ 0], S[3][0], 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S[3][1], 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S[3][2], 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S[3][3], 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S[3][0], 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S[3][1], 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S[3][2], 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S[3][3], 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S[3][0], 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S[3][1], 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S[3][2], 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S[3][3], 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S[3][0], 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S[3][1], 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S[3][2], 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S[3][3], 0xeb86d391); /* 64 */

state_[0] += a;
state_[1] += b;
state_[2] += c;
state_[3] += d;
}

/* Encodes input (ulong) into output (Byte). Assumes length is
a multiple of 4.
*/
void MD5::encode(const UInt32* input, Byte* output, SizeType length) {

for (SizeType i = 0, j = 0; j < length; ++i, j += 4) {
output[j]= (Byte)(input[i] & 0xff);
output[j + 1] = (Byte)((input[i] >> 8) & 0xff);
output[j + 2] = (Byte)((input[i] >> 16) & 0xff);
output[j + 3] = (Byte)((input[i] >> 24) & 0xff);
}
}

/* Decodes input (Byte) into output (ulong). Assumes length is
a multiple of 4.
*/
void MD5::decode(const Byte* input, UInt32* output, SizeType length) {

for (SizeType i = 0, j = 0; j < length; ++i, j += 4) {
output[i] = ((UInt32)input[j]) | (((UInt32)input[j + 1]) << 8) |
(((UInt32)input[j + 2]) << 16) | (((UInt32)input[j + 3]) << 24);
}
}

/* Convert Byte array to hex string. */
string MD5::bytesToHexString(const Byte* input, SizeType length) {

string str;
str.reserve(length << 1);
for (SizeType 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);
}
hongzao 2009-05-28
  • 打赏
  • 举报
回复

/* Default construct. */
MD5::MD5() {
reset();
}

/* Construct a MD5 object with a input buffer. */
MD5::MD5(const void* input, SizeType length) {
reset();
update(input, length);
}

/* Construct a MD5 object with a string. */
MD5::MD5(const string& str) {
reset();
update(str);
}

/* Construct a MD5 object with a file. */
MD5::MD5(ifstream& in) {
reset();
update(in);
}

/* Return the message-digest */
const Byte* MD5::digest() {

if (!isFinished_) {
isFinished_ = true;
final();
}
return digest_;
}

/* Reset the calculate state */
void MD5::reset() {

isFinished_ = false;
/* reset number of bits. */
count_[0] = count_[1] = 0;
/* Load magic initialization constants. */
state_[0] = 0x67452301;
state_[1] = 0xefcdab89;
state_[2] = 0x98badcfe;
state_[3] = 0x10325476;
}

/* Updating the context with a input buffer. */
void MD5::update(const void* input, SizeType length) {
update((const Byte*)input, length);
}

/* Updating the context with a string. */
void MD5::update(const string& str) {
update((const Byte*)str.c_str(), str.length());
}

/* Updating the context with a file. */
void MD5::update(ifstream& in) {

if (!in) {
return;
}

std::streamsize length;
char buffer[BufferSize];
while (!in.eof()) {
in.read(buffer, BufferSize);
length = in.gcount();
if (length > 0) {
update(buffer, length);
}
}
in.close();
}

/* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
void MD5::update(const Byte* input, SizeType length) {

UInt32 i, index, partLen;

isFinished_ = false;

/* Compute number of bytes mod 64 */
index = (UInt32)((count_[0] >> 3) & 0x3f);

/* update number of bits */
if ((count_[0] += ((UInt32)length << 3)) < ((UInt32)length << 3)) {
++count_[1];
}
count_[1] += ((UInt32)length >> 29);

partLen = 64 - index;

/* transform as many times as possible. */
if (length >= partLen) {

memcpy(&buffer_[index], input, partLen);
transform(buffer_);

for (i = partLen; i + 63 < length; i += 64) {
transform(&input[i]);
}
index = 0;

} else {
i = 0;
}

/* Buffer remaining input */
memcpy(&buffer_[index], &input[i], length - i);
}

/* MD5 finalization. Ends an MD5 message-digest_ operation, writing the
the message digest_ and zeroizing the context.
*/
void MD5::final() {

Byte bits[8];
UInt32 oldState[4];
UInt32 oldCount[2];
UInt32 index, padLen;

/* Save current state and count. */
memcpy(oldState, state_, 16);
memcpy(oldCount, count_, 8);

/* Save number of bits */
encode(count_, bits, 8);

/* Pad out to 56 mod 64. */
index = (UInt32)((count_[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
update(Padding, padLen);

/* Append length (before padding) */
update(bits, 8);

/* Store state in digest */
encode(state_, digest_, 16);

/* Restore current state and count. */
memcpy(state_, oldState, 16);
memcpy(count_, oldCount, 8);
}
hongzao 2009-05-28
  • 打赏
  • 举报
回复

#include "md5.h"

using namespace std;

/* Constants for MD5Transform routine. */
const int S[4][4] = {
7, 12, 17, 22,
5, 9, 14, 20,
4, 11, 16, 23,
6, 10, 15, 21
};

/* F, G, H and I are basic MD5 functions.
*/
inline UInt32 F(UInt32 x, UInt32 y, UInt32 z)
{
return (x & y) | ((~x) & z);
}

inline UInt32 G(UInt32 x, UInt32 y, UInt32 z)
{
return (x & z) | (y & (~z));
}

inline UInt32 H(UInt32 x, UInt32 y, UInt32 z)
{
return x ^ y ^ z;
}

inline UInt32 I(UInt32 x, UInt32 y, UInt32 z)
{
return y ^ (x | (~z));
}

/* ROTATE_LEFT rotates x left n bits.
*/
inline UInt32 RotateLeft(UInt32 x, int n)
{
return (x << n) | (x >> (32 - n));
}

/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation.
*/
inline void FF(UInt32& a, UInt32 b, UInt32 c, UInt32 d, UInt32 x, int s, UInt32 ac)
{
a += F(b, c, d) + x + ac;
a = RotateLeft(a, s);
a += b;
}

inline void GG(UInt32& a, UInt32 b, UInt32 c, UInt32 d, UInt32 x, int s, UInt32 ac)
{
a += G(b, c, d) + x + ac;
a = RotateLeft(a, s);
a += b;
}

inline void HH(UInt32& a, UInt32 b, UInt32 c, UInt32 d, UInt32 x, int s, UInt32 ac)
{
a += H(b, c, d) + x + ac;
a = RotateLeft(a, s);
a += b;
}

inline void II(UInt32& a, UInt32 b, UInt32 c, UInt32 d, UInt32 x, int s, UInt32 ac)
{
a += I(b, c, d) + x + ac;
a = RotateLeft(a, s);
a += b;
}

const Byte MD5::Padding[64] = { 0x80 };
const char MD5::Hex[16] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧