读书人

C++实现的MD5的开源代码下载?该如何解

发布时间: 2012-04-11 17:42:33 作者: rapoo

C++实现的MD5的开源代码下载?
哪里有比较好的MD5数据校验代码下载;
既可以检查1Kb网络数据的有效性,
又可以获取一个任意大小的磁盘文件的MD5值。
我下载的这个MD5校验代码,获取的MD5值不准确!
《我获取同样文件名字的磁盘文件,MD5值是一样的,无论我是否改变文件大小,或者文件内容》
得到的MD5值是一样的,不知道是啥原因。
因为要在项目中使用,所以
求准确的MD5开源代码,谢谢!

#ifndef MD5_H
#define MD5_H

#include <string>
#include <fstream>

/* Type define */
typedef unsigned char byte;
typedef unsigned int uint32;

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

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

private:
void update(const byte* input, size_t length);
void final();
void transform(const byte block[64]);
void encode(const uint32* input, byte* output, size_t length);
void decode(const byte* input, uint32* output, size_t length);
string bytesToHexString(const byte* input, size_t 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 _finished;/* calculate finished ? */

static const byte PADDING[64];/* padding for calculate */
static const char HEX[16];
enum { BUFFER_SIZE = 1024 };
};

#endif /*MD5_H*/

[解决办法]

探讨

http://www.cppblog.com/ant/archive/2007/09/11/31886.html
源码在这里,这个获取本地磁盘文件的MD5值,只要名字一样,无论文件大小,和内容是否改变,都一样,o()o 唉

[解决办法]
是呀,根据文件名,获取文件内容,然后得出,这个文件对应的hash值
[解决办法]
根据文件名,获取文件内容,然后得出,这个文件对应的hash值,这样才正确
[解决办法]
C/C++ code
#include <stdio.h>#include <math.h>#include <string.h>#include <memory.h>const unsigned int s[4][4]={{7,12,17,22},{5,9,14,20},{4,11,16,23},{6,10,15,21}};const unsigned long t[64]={//t[i]=4294967296*fabs(sin(i+1)); 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501, 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,0xa679438e,0x49b40821, 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8, 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a, 0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70, 0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665, 0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1, 0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391};const int serial[64]={ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12, 5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2, 0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9};void func(unsigned long& a,          unsigned long b,          unsigned long c,          unsigned long d,          unsigned long M,          unsigned long t,          int s,          int turn) {    unsigned long temp;    switch(turn) {    case 0:        temp=(b&c)|((~b)&d);        break;    case 1:        temp=(d&b)|((~d)&c);        break;    case 2:        temp=b^c^d;        break;    case 3:        temp=c^(b|(~d));        break;    }    temp+=M+t+a;    _asm {        mov ecx,s        rol temp,cl    }    a=b+temp;}void MD512(const unsigned long M[16],unsigned long hash[4]) {    int i,j,index=0;    for (i=0;i<4;i++)        for (j=0;j<4;j++) {            func(hash[0],hash[1],hash[2],hash[3],M[serial[index]],t[index],s[i][0],i);            index++;            func(hash[3],hash[0],hash[1],hash[2],M[serial[index]],t[index],s[i][1],i);            index++;            func(hash[2],hash[3],hash[0],hash[1],M[serial[index]],t[index],s[i][2],i);            index++;            func(hash[1],hash[2],hash[3],hash[0],M[serial[index]],t[index],s[i][3],i);            index++;        }}void MD5(char* M,int nLen,unsigned long output[4]) {    int i,j;    unsigned long Hash[4]={0x67452301,0xefcdab89,0x98badcfe,0x10325476};    unsigned long hash[4];    //填充    __int64 BitsLen=nLen*8;    int oldlen=nLen;    while(nLen%64!=56) {        M[nLen++]=0;    }    M[oldlen]=0x80u;    *(__int64*)(M+nLen)=BitsLen;    nLen+=8;    //开始处理分组    for (i=0;i<nLen;i+=64) {        memcpy(hash,Hash,sizeof(long)*4);        MD512((const unsigned long*)&M[i],hash);//处理512bits分组        for (j=0;j<4;j++)            Hash[j]+=hash[j];    }    //处理输出。    for (i=0;i<4;i++)        for (j=3;j>=0;j--) {            *((char*)(output+i)+j)=*((char*)(Hash+i)+3-j);        }}int main() {    int i;    char szMessage[1000]={0};    printf("input a string:\n>>>");    scanf("%s",szMessage);    unsigned long output[4];    MD5(szMessage,strlen(szMessage),output);    for (i=0;i<4;i++)        printf("%08lx",output[i]);    printf("\n");    return 0;} 


[解决办法]
头文件
#ifndef MD5_H
#define MD5_H

#include <string>
#include <fstream>

using namespace std;

/* Type define */
typedef unsigned char byte;
typedef unsigned int uint32;


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

private:
void update(const byte* input, size_t length);
void final();
void transform(const byte block[64]);
void encode(const uint32* input, byte* output, size_t length);
void decode(const byte* input, uint32* output, size_t length);
string bytesToHexString(const byte* input, size_t 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 _finished;/* calculate finished ? */

static const byte PADDING[64];/* padding for calculate */
static const char HEX[16];
enum { BUFFER_SIZE = 1024 };
};

#endif /*MD5_H*/

[解决办法]
cpp第一部分
#include "md5.h"

using namespace std;

/* Constants for MD5Transform routine. */
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21


/* F, G, H and I are basic MD5 functions.
*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))

/* ROTATE_LEFT rotates x left n bits.
*/
#define ROTATE_LEFT(x, n) (((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.
*/
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + ac; \
(a) = ROTATE_LEFT ((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'
};


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

/* Construct a MD5 object with a input buffer. */
MD5::MD5(const void* input, size_t 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 (!_finished) {
_finished = true;
final();
}
return _digest;
}

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

_finished = 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, size_t 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());
//}

void MD5::update(const string& path)
{
//char *file;
ifstream in;
in.open(path.c_str(),ifstream::binary);

if (!in.is_open())
{
//trace<<"torrent file open failed \n";
return;
}

std::streamsize length;
char buffer[BUFFER_SIZE];
while (!in.eof()) {
in.read(buffer, BUFFER_SIZE);
length = in.gcount();
if (length > 0) {
update(buffer, length);
}
}
[解决办法]
第二部分

in.close();

// delete [] file;
// file = NULL;
}

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

if (!in) {
return;
}

std::streamsize length;
char buffer[BUFFER_SIZE];
while (!in.eof()) {
in.read(buffer, BUFFER_SIZE);
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, size_t length) {

uint32 i, index, partLen;

_finished = 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);
}

------解决方案--------------------


。。。。。不传了 老是传不上来 郁闷

读书人网 >C++

热点推荐