php md5 file算法原理,MD5算法原理与实现

//MessageDigestAlgorithm5.cpp

#include "stdafx.h"

#include "MessageDigestAlgorithm5.h"

constByte CMessageDigestAlgorithm5::g_Padding[64] = { 0x80 };//第一位补1,其他补0

CMessageDigestAlgorithm5::CMessageDigestAlgorithm5(void)

{

}

CMessageDigestAlgorithm5::~CMessageDigestAlgorithm5(void)

{

}

unsigned intCMessageDigestAlgorithm5::F(unsignedintx, unsignedinty,unsignedintz)

{

return(x & y) | ((~ x) & z);

}

unsigned intCMessageDigestAlgorithm5::G(unsignedintx, unsignedinty,unsignedintz)

{

return(x & z) | (y & (~ z));

}

unsigned intCMessageDigestAlgorithm5::H(unsignedintx, unsignedinty,unsignedintz)

{

returnx ^ y ^ z;

}

unsigned intCMessageDigestAlgorithm5::I(unsignedintx, unsignedinty,unsignedintz)

{

returny ^ (x | (~ z));

}

/***************************************************

*参数:空

*功能:初始化链接变量

*返回值:空

****************************************************/

voidCMessageDigestAlgorithm5::Initialize()

{

m_Count[0] = m_Count[1] = 0;

m_ChainingVariable[0] = 0x67452301;

m_ChainingVariable[1] = 0xefcdab89;

m_ChainingVariable[2] = 0x98badcfe;

m_ChainingVariable[3] = 0x10325476;

}

/***************************************************

*参数:opNumber表示待左移的数

opBit表示左移的位数

*功能:完成循环左移操作

*返回值:循环左移后值

****************************************************/

unsigned intCMessageDigestAlgorithm5::LeftRotate(unsignedintopNumber,unsignedintopBit)

{

unsigned intleft = opNumber;

unsigned intright = opNumber;

return(left <> (32 - opBit));

}

voidCMessageDigestAlgorithm5::FF(unsignedint&a, unsignedintb,unsignedintc,unsignedintd, unsignedintMj,unsignedints,unsignedintTi)

{

unsigned inttemp = a + F(b,c,d) + Mj + Ti;

a = b + LeftRotate(temp,s);

}

voidCMessageDigestAlgorithm5::GG(unsignedint&a, unsignedintb,unsignedintc,unsignedintd,unsignedintMj,unsignedints,unsignedintTi)

{

unsigned inttemp = a + G(b,c,d) + Mj + Ti;

a = b + LeftRotate(temp,s);

}

voidCMessageDigestAlgorithm5::HH(unsignedint&a, unsignedintb,unsignedintc,unsignedintd, unsignedintMj,unsignedints,unsignedintTi)

{

unsigned inttemp = a + H(b,c,d) + Mj + Ti;

a = b + LeftRotate(temp,s);

}

voidCMessageDigestAlgorithm5::II(unsignedint&a, unsignedintb,unsignedintc,unsignedintd, unsignedintMj,unsignedints,unsignedintTi)

{

unsigned inttemp = a + I(b,c,d) + Mj + Ti;

a = b + LeftRotate(temp,s);

}

/***************************************************

*参数:input表示输入字节数组

output表示输出unsigned int数组

length表示输入字节长度

*功能:byte转unsigned int(左低右高)

*返回值:空

****************************************************/

voidCMessageDigestAlgorithm5::ByteToUnsignedInt(constByte* input, unsignedint* output,size_tlength)

{

for(size_ti = 0,j = 0;j

{

output[i] = ((static_castint>(input[j]))

|((static_castint>(input[j + 1])) <

|((static_castint>(input[j + 2])) <

|((static_castint>(input[j + 3])) <

}

}

/***************************************************

*参数:input表示输入unsigned int数组

output表示输出字节数组

length表示输入字节长度

*功能:unsigned int转byte

*返回值:空

****************************************************/

voidCMessageDigestAlgorithm5::UnsignedIntToByte(constunsignedint* input, Byte* output,size_tlength)

{

for(size_ti = 0, j = 0; j

{

output[j] = static_cast(input[i] & 0xff);

output[j + 1] = static_cast((input[i] >> 8) & 0xff);

output[j + 2] = static_cast((input[i] >> 16) & 0xff);

output[j + 3] = static_cast((input[i] >> 24) & 0xff);

}

}

/***************************************************

*参数:groups[]表示一个512位(64字节)分组

*功能:四轮主要操作

*返回值:空

****************************************************/

voidCMessageDigestAlgorithm5::ProcessOfMDA5(constByte  groups[64])

{

unsigned inta = m_ChainingVariable[0], b = m_ChainingVariable[1], c = m_ChainingVariable[2], d = m_ChainingVariable[3];

unsigned intM[16];

ByteToUnsignedInt( groups, M, 64);

FF(a, b, c, d, M[ 0], S11, 0xd76aa478);

FF(d, a, b, c, M[ 1], S12, 0xe8c7b756);

FF(c, d, a, b, M[ 2], S13, 0x242070db);

FF(b, c, d, a, M[ 3], S14, 0xc1bdceee);

FF(a, b, c, d, M[ 4], S11, 0xf57c0faf);

FF(d, a, b, c, M[ 5], S12, 0x4787c62a);

FF(c, d, a, b, M[ 6], S13, 0xa8304613);

FF(b, c, d, a, M[ 7], S14, 0xfd469501);

FF(a, b, c, d, M[ 8], S11, 0x698098d8);

FF(d, a, b, c, M[ 9], S12, 0x8b44f7af);

FF(c, d, a, b, M[10], S13, 0xffff5bb1);

FF(b, c, d, a, M[11], S14, 0x895cd7be);

FF(a, b, c, d, M[12], S11, 0x6b901122);

FF(d, a, b, c, M[13], S12, 0xfd987193);

FF(c, d, a, b, M[14], S13, 0xa679438e);

FF(b, c, d, a, M[15], S14, 0x49b40821);

GG(a, b, c, d, M[ 1], S21, 0xf61e2562);

GG(d, a, b, c, M[ 6], S22, 0xc040b340);

GG(c, d, a, b, M[11], S23, 0x265e5a51);

GG(b, c, d, a, M[ 0], S24, 0xe9b6c7aa);

GG(a, b, c, d, M[ 5], S21, 0xd62f105d);

GG(d, a, b, c, M[10], S22,  0x2441453);

GG(c, d, a, b, M[15], S23, 0xd8a1e681);

GG(b, c, d, a, M[ 4], S24, 0xe7d3fbc8);

GG(a, b, c, d, M[ 9], S21, 0x21e1cde6);

GG(d, a, b, c, M[14], S22, 0xc33707d6);

GG(c, d, a, b, M[ 3], S23, 0xf4d50d87);

GG(b, c, d, a, M[ 8], S24, 0x455a14ed);

GG(a, b, c, d, M[13], S21, 0xa9e3e905);

GG(d, a, b, c, M[ 2], S22, 0xfcefa3f8);

GG(c, d, a, b, M[ 7], S23, 0x676f02d9);

GG(b, c, d, a, M[12], S24, 0x8d2a4c8a);

HH(a, b, c, d, M[ 5], S31, 0xfffa3942);

HH(d, a, b, c, M[ 8], S32, 0x8771f681);

HH(c, d, a, b, M[11], S33, 0x6d9d6122);

HH(b, c, d, a, M[14], S34, 0xfde5380c);

HH(a, b, c, d, M[ 1], S31, 0xa4beea44);

HH(d, a, b, c, M[ 4], S32, 0x4bdecfa9);

HH(c, d, a, b, M[ 7], S33, 0xf6bb4b60);

HH(b, c, d, a, M[10], S34, 0xbebfbc70);

HH(a, b, c, d, M[13], S31, 0x289b7ec6);

HH(d, a, b, c, M[ 0], S32, 0xeaa127fa);

HH(c, d, a, b, M[ 3], S33, 0xd4ef3085);

HH(b, c, d, a, M[ 6], S34,  0x4881d05);

HH(a, b, c, d, M[ 9], S31, 0xd9d4d039);

HH(d, a, b, c, M[12], S32, 0xe6db99e5);

HH(c, d, a, b, M[15], S33, 0x1fa27cf8);

HH(b, c, d, a, M[ 2], S34, 0xc4ac5665);

II(a, b, c, d, M[ 0], S41, 0xf4292244);

II(d, a, b, c, M[ 7], S42, 0x432aff97);

II(c, d, a, b, M[14], S43, 0xab9423a7);

II(b, c, d, a, M[ 5], S44, 0xfc93a039);

II(a, b, c, d, M[12], S41, 0x655b59c3);

II(d, a, b, c, M[ 3], S42, 0x8f0ccc92);

II(c, d, a, b, M[10], S43, 0xffeff47d);

II(b, c, d, a, M[ 1], S44, 0x85845dd1);

II(a, b, c, d, M[ 8], S41, 0x6fa87e4f);

II(d, a, b, c, M[15], S42, 0xfe2ce6e0);

II(c, d, a, b, M[ 6], S43, 0xa3014314);

II(b, c, d, a, M[13], S44, 0x4e0811a1);

II(a, b, c, d, M[ 4], S41, 0xf7537e82);

II(d, a, b, c, M[11], S42, 0xbd3af235);

II(c, d, a, b, M[ 2], S43, 0x2ad7d2bb);

II(b, c, d, a, M[ 9], S44, 0xeb86d391);

m_ChainingVariable[0] += a;

m_ChainingVariable[1] += b;

m_ChainingVariable[2] += c;

m_ChainingVariable[3] += d;

}

/***************************************************

*参数:input表示输入字节数组

length表示输入字节长度=16(8*16=128位输出)

*功能:byte转16进制

*返回值:16进制字符串

****************************************************/

string CMessageDigestAlgorithm5::ByteToHexString(constByte* input,size_tlength)

{

constcharMapByteToHex[16] =

{

'0','1','2','3',

'4','5','6','7',

'8','9','A','B',

'C','D','E','F'

};

string str;

for(size_ti = 0; i

{

unsigned inttemp =static_castint>(input[i]);

unsigned inta = temp / 16;

unsigned intb = temp % 16;

str.append(1, MapByteToHex[a]);

str.append(1, MapByteToHex[b]);

}

returnstr;

}

/***************************************************

*参数:str表示待加密文本

*功能:MD5算法主函数

*返回值:MD5加密后算列值

****************************************************/

string CMessageDigestAlgorithm5::Encode(string &str)

{

Initialize();

EncodeByte((constByte * )(str.c_str()), str.length());

Final();

string strMD5 = ByteToHexString(m_Result,16);

returnstrMD5;

}

/***************************************************

*参数:infile表示待加密文件

*功能:MD5算法主函数

*返回值:MD5加密后算列值

****************************************************/

string CMessageDigestAlgorithm5::Encode(ifstream & infile)

{

if(!infile)

{

return"";

}

Initialize();

streamsize length;

string str;

charbuffer[1024];

while(! infile.eof())

{

infile.read(buffer, 1024);

length = infile.gcount();

if(length > 0)

{

EncodeByte((constByte* )buffer,length);

Final();

}

}

infile.close();

string strMD5 = ByteToHexString(m_Result,16);

returnstrMD5;

}

voidCMessageDigestAlgorithm5::EncodeByte(constByte* input,size_tlength)

{

unsigned intindex, partLen;

size_ti;

index = static_castint>((m_Count[0] >> 3) & 0x3f);//转换成字节mod64

m_Count[0] += (static_castint>(length) /bit数

if(m_Count[0] static_castint>(length) <

{

++m_Count[1];

}

m_Count[1] += (static_castint>(length) >> 29);//

partLen = 64 - index;

if(length >= partLen)

{

memcpy(&m_Buffer[index], input, partLen);

ProcessOfMDA5(m_Buffer);

for(i = partLen; i + 63

{

ProcessOfMDA5(&input[i]);

}

index = 0;

}

else

{

i = 0;

}

memcpy(&m_Buffer[index], &input[i], length - i);

}

voidCMessageDigestAlgorithm5::Final()

{

Byte bits[8];

unsigned inttempChainingVariable[4],tempCount[2];

unsigned intindex, padLen;

memcpy(tempChainingVariable, m_ChainingVariable, 16);

memcpy(tempCount, m_Count, 8);

UnsignedIntToByte(m_Count, bits, 8);

index = static_castint>((m_Count[0] >> 3) & 0x3f);

padLen = (index

EncodeByte(g_Padding, padLen);

EncodeByte(bits, 8);

UnsignedIntToByte(m_ChainingVariable,m_Result, 16);

memcpy(m_ChainingVariable, tempChainingVariable, 16);

memcpy(m_Count,tempCount, 8);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值