|
【更新】
2012-08-15,添加MD5应用描述。
【说明】读本文之前,建议先阅读MD5算法的C++实现 一文。本文是对MD5算法的C++实现一文所述代码的Qt版本改写。主要改动是transform()函数,参考了http://zh.wikipedia.org/zh/MD5(建议阅读)提供的方法。本文主要对改动部分进行描述,感谢MD5算法的C++实现博主的辛勤付出。
【原理】
*下述摘自MD5算法的C++实现 一文。
MD5算法的基本步骤:
(1) Append Padding Bits 信息计算前先要进行位补位,设补位后信息的长度为LEN(bit),则LEN%512 = 448(bit),即数据扩展至 K*512+448(bit)。即K*64+56(byte),K为整数。补位操作始终要执行,即使补位前信息的长度对512求余的结果是448。具体补位操作:补一个1,然后补0至满足上述要求。总共最少要补1bit,最多补512bit。
(2) Append Length 将输入信息的原始长度b(bit)表示成一个64-bit的数字,把它添加到上一步的结果后面(在32位的机器上,这64位将用2个字来表示并且低位在前)。当遇到b大于2^64这种极少的情况时,b的高位被截去,仅使用b的低64位。经过上面两步,数据就被填补成长度为512(bit)的倍数。也就是说,此时的数据长度是16个字(32byte)的整数倍。此时的数据表示为: M[0 ... N-1] 其中的N是16的倍数。 (3) Initialize MD Buffer 用一个四个字的缓冲器(A,B,C,D)来计算报文摘要,A,B,C,D分别是32位的寄存器,初始化使用的是十六进制表示的数字,注意低字节在前: word A: 01 23 45 67 word B: 89 ab cd ef word C: fe dc ba 98 word D: 76 54 32 10 (4) Process Message in 16-Word Blocks 首先定义4个辅助函数,每个函数的输入是三个32位的字,输出是一个32位的字: F(X,Y,Z) = XY v not(X) Z G(X,Y,Z) = XZ v Y not(Z) H(X,Y,Z) = X xor Y xor Z I(X,Y,Z) = Y xor (X v not(Z))
(5) Output 报文摘要的产生后的形式为:A,B,C,D。也就是低位字节A开始,高位字节D结束。
*下述摘自维基百科http://zh.wikipedia.org/zh/MD5
伪代码
//Note: All variables are unsigned 32 bits and wrap modulo 2^32 when calculating
var int[64] r, k
//r specifies the per-round shift amounts
r[ 0..15]:= {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22}
r[16..31]:= {5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20}
r[32..47]:= {4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23}
r[48..63]:= {6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}
//Use binary integer part of the sines of integers as constants:
for i from 0 to 63
k[i] := floor(abs(sin(i + 1)) × 2^32)
//Initialize variables:
var int h0 := 0x67452301
var int h1 := 0xEFCDAB89
var int h2 := 0x98BADCFE
var int h3 := 0x10325476
//Pre-processing:
append "1" bit to message
append "0" bits until message length in bits ≡ 448 (mod 512)
append bit length of message as 64-bit little-endian integer to message
//Process the message in successive 512-bit chunks:
for each 512-bit chunk of message
break chunk into sixteen 32-bit little-endian words w[i], 0 ≤ i ≤ 15
//Initialize hash value for this chunk:
var int a := h0
var int b := h1
var int c := h2
var int d := h3
//Main loop:
for i from 0 to 63
if 0 ≤ i ≤ 15 then
f := (b and c) or ((not b) and d)
g := i
else if 16 ≤ i ≤ 31
f := (d and b) or ((not d) and c)
g := (5×i + 1) mod 16
else if 32 ≤ i ≤ 47
f := b xor c xor d
g := (3×i + 5) mod 16
else if 48 ≤ i ≤ 63
f := c xor (b or (not d))
g := (7×i) mod 16
temp := d
d := c
c := b
b := leftrotate((a + f + k[i] + w[g]),r[i]) + b
a := temp
Next i
//Add this chunk's hash to result so far:
h0 := h0 + a
h1 := h1 + b
h2 := h2 + c
h3 := h3 + d
End ForEach
var int digest := h0 append h1 append h2 append h3 //(expressed as little-endian)
【代码解释】
1 步骤3的实现
_state[0] = 0x67452301;
_state[1] = 0xefcdab89;
_state[2] = 0x98badcfe;
_state[3] = 0x10325476;
2 步骤4的实现
/* Operate define */
#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)))
3 key值的产生
(1) 方法1:利用静态矩阵初始化
k数组的定义
static const uint32 k[ 64 ];
具体实现
const uint32 MD5::k[64] = {
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
};
(2) 方法2:利用伪代码定义,对应伪代码中下述代码:
//Use binary integer part of the sines of integers as constants:
for i from 0 to 63
k[i] := floor(abs(sin(i + 1)) × 2^32)
Qt代码如下:
k数组的声明和定义
static uint32 k[64];//md5.h
uint32 MD5::k[64] = {0};//md5.cpp
具体实现
for(int i=0; i<64; i++)
k[i] = floor(fabs(sin(i+1))*pow(2,32));
*注意:两种方法k数组的定义
4 伪代码的实现
void MD5::transform(const byte block[64])
{
/*
//Initialize hash value for this chunk:
var int a := _state[0]
var int b := _state[1]
var int c := _state[2]
var int d := _state[3]
*/
uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3];
uint32 temp;
uint32 w[16];
uint32 f;
int i;
int g;
/*
break chunk into sixteen 32-bit little-endian words w[i], 0 ≤ i ≤ 64
*/
decode(block, w, 64);
/*
//Main loop:
for i from 0 to 63
if 0 ≤ i ≤ 15 then
f := (b and c) or ((not b) and d)
g := i
else if 16 ≤ i ≤ 31
f := (d and b) or ((not d) and c)
g := (5×i + 1) mod 16
else if 32 ≤ i ≤ 47
f := b xor c xor d
g := (3×i + 5) mod 16
else if 48 ≤ i ≤ 63
f := c xor (b or (not d))
g := (7×i) mod 16
temp := d
d := c
c := b
b := leftrotate((a + f + k[i] + w[g]),r[i]) + b
a := temp
Next i
*/
for(i=0; i<64; i++)
{
if(i>=0 && i<16)
{
f = F(b, c, d);
g = i;
}
else if(i>=16 && i<32)
{
f = G(b, c, d);
g = (5*i + 1)%16;
}
else if(i>=32 && i<48)
{
f = H(b, c, d);
g = (3*i + 5)%16;
}
else if(i>=48 && i<64)
{
f = I(b, c, d);
g = (7*i)%16;
}
temp = d;
d = c;
c = b;
b = ROTATE_LEFT((a + f + k[i] + w[g]), r[i]) + b;
a = temp;
}
/*
//Add this chunk's hash to result so far:
_state[0] := _state[0] + a
_state[1] := _state[1] + b
_state[2] := _state[2] + c
_state[3] := _state[3] + d
*/
_state[0] += a;
_state[1] += b;
_state[2] += c;
_state[3] += d;
}
5 测试代码
main.cpp
#include <QDebug>
#include "md5.h"
using namespace std;
void PrintMD5(const QString& str, MD5& md5)
{
qDebug() <<"MD5(" << str << ") = " << md5.toString() ;
}
QString FileDigest(const QString& file)
{
ifstream in(file.toLatin1().data(), ios::binary);
if (!in)
{
return "";
}
MD5 md5;
std::streamsize length;
char buffer[1024];
while (!in.eof())
{
in.read(buffer, 1024);
length = in.gcount();
if (length > 0)
{
md5.update(buffer, length);
}
}
in.close();
return md5.toString();
}
int main(void)
{
MD5 md5;
md5.update("");
PrintMD5("", md5);
md5.update("a");
PrintMD5("a", md5);
md5.update("bc");// Note : without reset()
PrintMD5("abc", md5);
md5.update("defghijklmnopqrstuvwxyz");// Note : without reset()
PrintMD5("abcdefghijklmnopqrstuvwxyz", md5);
md5.reset();
md5.update("message digest");
PrintMD5("message digest", md5);
md5.reset();
ifstream in("./test.txt", ios::binary);
md5.update(in);
PrintMD5("./test.txt", md5);
return 0;
}
【运行结果】
MD5( "" ) = "d41d8cd98f00b204e9800998ecf8427e"
MD5( "a" ) = "0cc175b9c0f1b6a831c399e269772661"
MD5( "abc" ) = "900150983cd24fb0d6963f7d28e17f72"
MD5( "abcdefghijklmnopqrstuvwxyz" ) = "c3fcd3d76192e4007dfb496cca67e13b"
MD5( "message digest" ) = "f96b697d7cb7938d525a2f31aaf161d0"
MD5( "./test.txt" ) = "098f6bcd4621d373cade4e832627b4f6"
【应用 2012-08-15】
将MD5称为加解密算法,是不准确的。因为,MD5是不可逆的算法。MD5标准文档只给出了MD5值的求解方法,而并未给出如何通过MD5值求得原文的方法。MD5常被用于不参与运算的数据库字段的加密,例如用户名、密码等信息加密。以用户密码为例,使用时,求取密码的MD5值,然后将其存入数据库中。在登录认证中,服务器获取用户密码后,求取密码的MD5值。然后,利用类似select语句进行查询数据中是否存在该值。若查询到相应的MD5值,则认为通过验证。
MD5的关键之处,在于算法的散列性能。期望的MD5值是唯一的。试想,如果"a”和"b"对应的MD5值相同,那么如果正确的用户密码是"a",则可以同时通过"a","b"“正确的”进行验证,这显然是不被允许的。而散列的关键在于密钥的产生(见代码解释3),因此改进MD5算法可以由此着手。
【资源下载】
http://download.csdn.net/detail/tandesir/4490759
*对于MD5算法的C++实现一文所述代码中,步骤1和步骤2所述代码,感觉理解还不够透彻(update()和final()函数)。有谁理解了的,希望做出总结,留给地址啊!呵呵
转载请标明出处,仅供学习交流,勿用于商业目的
Copyright @ http://blog.csdn.net/tandesir
|