当前位置:文档之家› MD5加密算法-c源代码

MD5加密算法-c源代码

}
else
i = 0;
/* Buffer remaining input */
/*将输入缓冲区中的不足填充满512bits的剩余内容填充到context->buffer中,留待以后再作处理*/
R_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
/* Update number of bits *//*更新已有信息的bits长度*/
if((context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3))
转换结果保存到context->state中
*/
for(i = partLen; i + 63 < inputLen; i += 64)/*把i+63<inputlen改为i+64<=inputlen更容易理解*/
MD5Transform(context->state, &input[i]);
index = 0;
}
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context. */
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
static void MD5Transform(UINT4 [4], unsigned char [64]);
static void Encode(unsigned char *, UINT4 *, unsigned int);
typedef unsigned char *POINTER;
/* MD5 context. */
typedef struct {
/* state (ABCD) */
/*四个32bits数,用于存放最终计算得到的消息摘要。当消息长度〉512bits时,也用于存放每个512bits的中间结果*/
UINT4 state[4];
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
unsigned char buffer[64];
} MD5_CTX;
void MD5Init(MD5_CTX *);
void MD5Update(MD5_CTX *, unsigned char *, unsigned int);
void MD5Final(unsigned char [16], MD5_CTX *);
#endif /* _MD5_H_ */
///////////////////////////////////////////////////////////////////////////
/* md5.cpp */
#include "stdafx.h"
/* Constants for MD5Transform routine. */
*/
#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.
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
md5的介绍的文章网上很多,关于md5的来历,用途什么的这里就不再介绍了。这里主要介绍代码。代码明白了就什么都明白了。
////////////////////////////////////////////////////////////////////
/* md5.h */
#ifndef _MD5_H_
(a) += (b); \
}
/* MD5 initialization. Begins an MD5 operation, writing a new context. */
/*初始化md5的结构*/
void MD5Init (MD5_CTX *context)
{
/*将当前的有效信息的长度设成0,这个很简单,还没有有效信息,长度当然是0了*/
{
unsigned int i, index, partLen;
/* Compute number of bytes mod 64 */
/*计算已有信息的bits长度的字节数的模64, 64bytes=512bits。
用于判断已有信息加上当前传过来的信息的总长度能不能达到512bits,
如果能够达到则对凑够的512bits进行一次处理*/
/*用基本函数对填充满的512bits(已经保存到context->buffer中)做一次转换,转换结果保存到context->state中*/
MD5Transform(context->state, context->buffer);
/*
对当前输入的剩余字节做转换(如果剩余的字节<在输入的input缓冲区中>大于512bits的话),
*/
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
context->count[1]++;
context->count[1] += ((UINT4)inputLen >> 29);
/*计算已有的字节数长度还差多少字节可以凑成64的整倍数*/
partLen = 64 - index;
/* Transform as many times as possible.
md5加密算法c实现
七分注释收藏
经常到csdn来是查资料,每次都会有所收获。总是看别人的感觉很不好意思,于是决定自己也写一点东西贡献出来。于是就有了这篇md5七分注释。希望对用到的朋友有所帮助。
记得当初自己刚开始学习md5的时候,从网上搜了很多关于算法的原理和文字性的描述的东西,但是看了很久一直没有搞懂,搜c的源代码又很少。直到后来学习rsa算法的时候,从网上下了1991年的欧洲的什么组织写的关于rsa、des、md5算法的c源代码(各部分代码混在一块的,比如rsa用到的随机大素数就是用机器的随机时间的md5哈希值获得的)。我才彻底把md5弄明白了。这里的代码就是我从那里面分离出来的,代码的效率和可重用性都是很高的。整理了一下希望对需要的朋友能够有帮助。
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
}
/* MD5 block update operation. Continues an MD5 message-digest
operation, prock, and updating the
*/
/*如果当前输入的字节数大于已有字节数长度补足64字节整倍数所差的字节数*/
if(inputLen >= partLen)
{
/*用当前输入的内容把context->buffer的内容补足512bits*/
R_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
};
/*
接下来的这几个宏定义是md5算法规定的,就是对信息进行md5加密都要做的运算。
据说有经验的高手跟踪程序时根据这几个特殊的操作就可以断定是不是用的md5
*/
/* F, G, H and I are basic MD5 functions.
*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
context->count[0] = context->count[1] = 0;
相关主题