MD5算法及源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MD5算法及源代码
分类:计算机密码
//获得MD5的二个数组和一个buffer并初始化
MD5 *GetMD5();
//初始化MD5的二个数据和一个buffer
void MD5Init (MD5 *context);
//用于计算MD5值的函数
void MD5Update (MD5 *context, unsigned char *input, unsigned int inputLen);
//输出结果
void MD5Final (MD5 *context, unsigned char digest[16]);
//对input数据做一次完整的MD5运算
void MD5Out (MD5 *md5, unsigned char *input, unsigned int inputLen, unsigned char out[16]);
//计算一个文件的MD5值
int 计算一个文件的MD5值(TCHAR* 文件路径, unsigned char md5值[16])
{
MD5 context;
int 缓冲区长度 = 1024,
读取到的字节数;
unsigned char *缓冲区 = new unsigned char[缓冲区长度];
FILE *文件指针 = fopen(文件路径, "rb");
if(文件指针 == NULL)
return 1;
MD5Init(&context);
while ( (读取到的字节数 = fread (
缓冲区, 1, 缓冲区长度, 文件指针 )) ! =EOF)
{
MD5Update (&context, 缓冲区, 读取到的字节数);
//判断是否为已经读到文件尾
if ( 读取到的字节数 < 缓冲区长度 )
break;
}
MD5Final (&context, md5值);
free ( 缓冲区 );
return 0;
}
/**
**MD5.h
**/
typedef struct {
unsigned long state[4]; /* state (ABCD) */
unsigned long count[2]; /* number of bits, modulo 2^64 */
unsigned char buffer[64]; /* input buffer */
} MD5;
MD5 *GetMD5();
void MD5Init (MD5 *context);
void MD5Update (MD5 *context, unsigned char *input, unsigned int inputLen);
void MD5Final (MD5 *context, unsigned char digest[16]);
void MD5Out (MD5 *md5, unsigned char *input, unsigned int inputLen, unsigned char out[16]); /**
*MD5.cpp
*/
#include "stdafx.h"
#include
#include "md5.h"
#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
static void MD5Transform (unsigned long int state[4], unsigned char block[64]);
static void Encode (unsigned char *output, unsigned long int *input,unsigned int len); static void Decode (unsigned long int *output, unsigned char *input, unsigned int len);
//static void MD5_memcpy (unsigned char* output, unsigned char* input,unsigned int len);
//static void MD5_memset (unsigned char* output,int value,unsigned int len);
static unsigned char PADDING[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/* 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)))