📄 md5信息-摘要加算法(翻译).html
字号:
#define S42 10<br>
#define S43 15<br>
#define S44 21<br>
<br>
static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));<br>
static void Encode PROTO_LIST<br>
((unsigned char *, UINT4 *, unsigned int));<br>
static void Decode PROTO_LIST<br>
((UINT4 *, unsigned char *, unsigned int));<br>
static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));<br>
static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));<br>
<br>
static unsigned char PADDING[64] = {<br>
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<br>
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<br>
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0<br>
};<br>
<br>
/* F, G, H 和 I 为MD5的基本函数<br>
*/<br>
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))<br>
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))<br>
#define H(x, y, z) ((x) ^ (y) ^ (z))<br>
#define I(x, y, z) ((y) ^ ((x) | (~z)))<br>
<br>
/* ROTATE_LEFT 定义对x进行循环左移,使x只剩下n位bits值.<br>
*/<br>
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))<br>
<br>
/* FF, GG, HH 和 II 的分别代表第 1, 2, 3 及 4次循环<br>
位移与加法运算相分离,以防止再运算<br>
*/<br>
#define FF(a, b, c, d, x, s, ac) { \<br>
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \<br>
(a) = ROTATE_LEFT ((a), (s)); \<br>
(a) += (b); \<br>
}<br>
#define GG(a, b, c, d, x, s, ac) { \<br>
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \<br>
(a) = ROTATE_LEFT ((a), (s)); \<br>
(a) += (b); \<br>
}<br>
#define HH(a, b, c, d, x, s, ac) { \<br>
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \<br>
(a) = ROTATE_LEFT ((a), (s)); \<br>
(a) += (b); \<br>
}<br>
#define II(a, b, c, d, x, s, ac) { \<br>
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \<br>
(a) = ROTATE_LEFT ((a), (s)); \<br>
(a) += (b); \<br>
}<br>
<br>
/* MD5 初始化. 开始 MD5 操作, 并写入一个新的内容(context) (<font color="#00FF00">newlaos</font>:内容的最终结果就是MD5的信息摘要了)<br>
*/<br>
void MD5Init (context)<br>
MD5_CTX *context; /* context */<br>
{<br>
context->count[0] = context->count[1] = 0;<br>
/* 加载初始化常数。<br>
*/<br>
context->state[0] = 0x67452301;<br>
context->state[1] = 0xefcdab89;<br>
context->state[2] = 0x98badcfe;<br>
context->state[3] = 0x10325476;<br>
}<br>
<br>
/* MD5 数据块更新操作。继续MD5 信息摘要操作Continues an MD5 message-digest<br>
operation, processing another message block, and updating the<br>
context.<br>
*/<br>
void MD5Update (context, input, inputLen)<br>
MD5_CTX *context; /* MD5内容 */<br>
unsigned char *input; /* 输入的数据块 */<br>
unsigned int inputLen; /* 输入的数据块长度 */<br>
{<br>
unsigned int i, index, partLen;<br>
<br>
/* 计算字节数除以64的余数(字节数 mod 64) */<br>
index = (unsigned int)((context->count[0] >> 3) & 0x3F);<br>
<br>
/* 更新bit位数 */<br>
if ((context->count[0] += ((UINT4)inputLen << 3))<br>
<br>
< ((UINT4)inputLen << 3))<br>
context->count[1]++;<br>
context->count[1] += ((UINT4)inputLen >> 29);<br>
<br>
partLen = 64 - index;<br>
<br>
/* 尽可能多地变换<br>
*/<br>
if (inputLen >= partLen) {<br>
MD5_memcpy<br>
((POINTER)&context->buffer[index], (POINTER)input, partLen);<br>
MD5Transform (context->state, context->buffer);<br>
<br>
for (i = partLen; i + 63 < inputLen; i += 64)<br>
MD5Transform (context->state, &input[i]);<br>
<br>
index = 0;<br>
}<br>
else<br>
i = 0;<br>
<br>
/* 对剩下的数据进行缓存 */<br>
MD5_memcpy<br>
((POINTER)&context->buffer[index], (POINTER)&input[i],<br>
inputLen-i);<br>
}<br>
<br>
/* MD5 结束. 结束一个生成 MD5 信息摘要过程的操作, 将信息摘要写入内容Context.<br>
*/<br>
void MD5Final (digest, context)<br>
unsigned char digest[16]; /* 信息摘要 */<br>
MD5_CTX *context; /* 内容context */<br>
{<br>
unsigned char bits[8];<br>
unsigned int index, padLen;<br>
<br>
/* 保存bit位数 */<br>
Encode (bits, context->count, 8);<br>
<br>
/* 将数据补长至对64求模(余数)为56<br>
*/<br>
index = (unsigned int)((context->count[0] >> 3) & 0x3f);<br>
padLen = (index < 56) ? (56 - index) : (120 - index);<br>
MD5Update (context, PADDING, padLen);<br>
<br>
/* 增加长度 (补长前) */<br>
MD5Update (context, bits, 8);<br>
<br>
/* 保存摘要状态 */<br>
Encode (digest, context->state, 16);<br>
<br>
/* 将敏感信息至为最初状态<br>
*/<br>
MD5_memset ((POINTER)context, 0, sizeof (*context));<br>
}<br>
<br>
/* MD5 基本变换. 基于块(512位)的变换<br>
*/<br>
static void MD5Transform (state, block)<br>
UINT4 state[4];<br>
unsigned char block[64];<br>
{<br>
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];<br>
<br>
Decode (x, block, 64);<br>
<br>
/* 第 1 轮循环*/<br>
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */<br>
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */<br>
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */<br>
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */<br>
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */<br>
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */<br>
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */<br>
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */<br>
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */<br>
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */<br>
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */<br>
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */<br>
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */<br>
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */<br>
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */<br>
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */<br>
<br>
/* 第 2 轮循环*/<br>
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */<br>
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */<br>
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */<br>
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */<br>
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */<br>
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */<br>
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */<br>
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */<br>
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */<br>
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */<br>
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */<br>
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */<br>
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */<br>
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */<br>
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */<br>
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */<br>
<br>
/* 第 3 轮循环*/<br>
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */<br>
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */<br>
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */<br>
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */<br>
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */<br>
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */<br>
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */<br>
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */<br>
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */<br>
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */<br>
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */<br>
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */<br>
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */<br>
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */<br>
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */<br>
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */<br>
<br>
/* 第 4 轮循环*/<br>
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */<br>
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */<br>
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */<br>
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */<br>
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */<br>
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */<br>
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */<br>
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */<br>
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */<br>
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */<br>
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */<br>
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */<br>
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */<br>
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */<br>
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */<br>
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */<br>
<br>
state[0] += a;<br>
state[1] += b;<br>
state[2] += c;<br>
state[3] += d;<br>
<br>
/* Zeroize sensitive information.<br>
*/<br>
MD5_memset ((POINTER)x, 0, sizeof (x));<br>
}<br>
<br>
/* 将(UINT4)输入加密为(unsigned char)输出。假定长度为4的整数倍<br>
*/<br>
static void Encode (output, input, len)<br>
unsigned char *output;<br>
UINT4 *input;<br>
unsigned int len;<br>
{<br>
unsigned int i, j;<br>
<br>
for (i = 0, j = 0; j < len; i++, j += 4) {<br>
output[j] = (unsigned char)(input[i] & 0xff);<br>
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);<br>
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);<br>
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);<br>
}<br>
}<br>
<br>
/*将 (unsigned char)输入解密为(UINT4)输出。假定长度为4的整数倍。<br>
*/<br>
static void Decode (output, input, len)<br>
UINT4 *output;<br>
unsigned char *input;<br>
unsigned int len;<br>
{<br>
unsigned int i, j;<br>
<br>
for (i = 0, j = 0; j < len; i++, j += 4)<br>
output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |<br>
(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);<br>
}<br>
<br>
/* Note: Replace "for loop" with standard memcpy if possible.<br>
*/<br>
<br>
static void MD5_memcpy (output, input, len)<br>
POINTER output;<br>
POINTER input;<br>
unsigned int len;<br>
{<br>
unsigned int i;<br>
<br>
for (i = 0; i < len; i++)<br>
<br>
output[i] = input[i];<br>
}<br>
<br>
/* Note: Replace "for loop" with standard memset if possible.<br>
*/<br>
static void MD5_memset (output, value, len)<br>
POINTER output;<br>
int value;<br>
unsigned int len;<br>
{<br>
unsigned int i;<br>
<br>
for (i = 0; i < len; i++)<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -