1、MD5概念
MD5信息摘要算法(MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。输入任意长度的数据经过处理,输出都是128位的信息(数字指纹)。
不同的输入得到的不同的结果,在有限范围内是唯一的,毕竟字符的组合是无限的,大于2^128种。MD5加密过程是一个有损的加密过程,几乎不能还原出原始数据。
2、MD5算法(C语言版)
md5.h
#ifndef MD5_H
#define MD5_H
typedef struct
{
unsigned int count[2];
unsigned int state[4];
unsigned char buffer[64];
}MD5_CTX;
#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))
#define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))
#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; \
}
extern void MD5Init(MD5_CTX *context);
extern void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen);
extern void MD5Final(MD5_CTX *context,unsigned char digest[16]);
#endif // MD5_H
md5.c
#include "md5.h"
static void MD5Transform(unsigned int state[4],unsigned char block[64]);
static void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len);
static void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len);
unsigned char PADDING[]={
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};
static void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)
{
unsigned int i = 0,j = 0;
while(j < len)
{
output[j] = input[i] & 0xFF;
output[j+1] = (input[i] >> 8) & 0xFF;
output[j+2] = (input[i] >> 16) & 0xFF;
output[j+3] = (input[i] >> 24) & 0xFF;
i++;
j+=4;
}
}
static void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len)

本文介绍了MD5信息摘要算法的概念,提供了一个C语言版本的MD5算法实现,并探讨了其在嵌入式系统开发中的应用场景,例如文件完整性的校验及密码的安全存储。
最低0.47元/天 解锁文章
582

被折叠的 条评论
为什么被折叠?



