【JS】JavaScript版的MD5算法
抄来的。var CharBits=8;//每个字符的位数function StringMD5(SrcStr)
{
return BinArr2Hex(BinMD5(Str2BinArray(SrcStr),SrcStr.length*CharBits));
}
function BinMD5(X,TotalBits)
{
X|=0x80<<((TotalBits)%32);
X[(((TotalBits+64)>>>9)<<4)+14]=TotalBits;
var A=0x67452301;
var B=0xEFCDAB89;
var C=0x98BADCFE;
var D=0x10325476;
for(var i=0;i<X.length;i+=16)
{
var OldA=A;
var OldB=B;
var OldC=C;
var OldD=D;
A=FF(A,B,C,D,X,0x07,0xD76AA478);
D=FF(D,A,B,C,X,0x0C,0xE8C7B756);
C=FF(C,D,A,B,X,0x11,0x242070DB);
B=FF(B,C,D,A,X,0x16,0xC1BDCEEE);
A=FF(A,B,C,D,X,0x07,0xF57C0FAF);
D=FF(D,A,B,C,X,0x0C,0x4787C62A);
C=FF(C,D,A,B,X,0x11,0xA8304613);
B=FF(B,C,D,A,X,0x16,0xFD469501);
A=FF(A,B,C,D,X,0x07,0x698098D8);
D=FF(D,A,B,C,X,0x0C,0x8B44F7AF);
C=FF(C,D,A,B,X,0x11,0xFFFF5BB1);
B=FF(B,C,D,A,X,0x16,0x895CD7BE);
A=FF(A,B,C,D,X,0x07,0x6B901122);
D=FF(D,A,B,C,X,0x0C,0xFD987193);
C=FF(C,D,A,B,X,0x11,0xA679438E);
B=FF(B,C,D,A,X,0x16,0x49B40821);
A=GG(A,B,C,D,X,0x05,0xF61E2562);
D=GG(D,A,B,C,X,0x09,0xC040B340);
C=GG(C,D,A,B,X,0x0E,0x265E5A51);
B=GG(B,C,D,A,X,0x14,0xE9B6C7AA);
A=GG(A,B,C,D,X,0x05,0xD62F105D);
D=GG(D,A,B,C,X,0x09,0x02441453);
C=GG(C,D,A,B,X,0x0E,0xD8A1E681);
B=GG(B,C,D,A,X,0x14,0xE7D3FBC8);
A=GG(A,B,C,D,X,0x05,0x21E1CDE6);
D=GG(D,A,B,C,X,0x09,0xC33707D6);
C=GG(C,D,A,B,X,0x0E,0xF4D50D87);
B=GG(B,C,D,A,X,0x14,0x455A14ED);
A=GG(A,B,C,D,X,0x05,0xA9E3E905);
D=GG(D,A,B,C,X,0x09,0xFCEFA3F8);
C=GG(C,D,A,B,X,0x0E,0x676F02D9);
B=GG(B,C,D,A,X,0x14,0x8D2A4C8A);
A=HH(A,B,C,D,X,0x04,0xFFFA3942);
D=HH(D,A,B,C,X,0x0B,0x8771F681);
C=HH(C,D,A,B,X,0x10,0x6D9D6122);
B=HH(B,C,D,A,X,0x17,0xFDE5380C);
A=HH(A,B,C,D,X,0x04,0xA4BEEA44);
D=HH(D,A,B,C,X,0x0B,0x4BDECFA9);
C=HH(C,D,A,B,X,0x10,0xF6BB4B60);
B=HH(B,C,D,A,X,0x17,0xBEBFBC70);
A=HH(A,B,C,D,X,0x04,0x289B7EC6);
D=HH(D,A,B,C,X,0x0B,0xEAA127FA);
C=HH(C,D,A,B,X,0x10,0xD4EF3085);
B=HH(B,C,D,A,X,0x17,0x04881D05);
A=HH(A,B,C,D,X,0x04,0xD9D4D039);
D=HH(D,A,B,C,X,0x0B,0xE6DB99E5);
C=HH(C,D,A,B,X,0x10,0x1FA27CF8);
B=HH(B,C,D,A,X,0x17,0xC4AC5665);
A=II(A,B,C,D,X,0x06,0xF4292244);
D=II(D,A,B,C,X,0x0A,0x432AFF97);
C=II(C,D,A,B,X,0x0F,0xAB9423A7);
B=II(B,C,D,A,X,0x15,0xFC93A039);
A=II(A,B,C,D,X,0x06,0x655B59C3);
D=II(D,A,B,C,X,0x0A,0x8F0CCC92);
C=II(C,D,A,B,X,0x0F,0xFFEFF47D);
B=II(B,C,D,A,X,0x15,0x85845DD1);
A=II(A,B,C,D,X,0x06,0x6FA87E4F);
D=II(D,A,B,C,X,0x0A,0xFE2CE6E0);
C=II(C,D,A,B,X,0x0F,0xA3014314);
B=II(B,C,D,A,X,0x15,0x4E0811A1);
A=II(A,B,C,D,X,0x06,0xF7537E82);
D=II(D,A,B,C,X,0x0A,0xBD3AF235);
C=II(C,D,A,B,X,0x0F,0x2AD7D2BB);
B=II(B,C,D,A,X,0x15,0xEB86D391);
A=AddUnsigned(A,OldA);
B=AddUnsigned(B,OldB);
C=AddUnsigned(C,OldC);
D=AddUnsigned(D,OldD);
}
return Array(A,B,C,D);
}
function CMN(Q,A,B,X,S,T)
{
return AddUnsigned(BitRol(AddUnsigned(AddUnsigned(A,Q),AddUnsigned(X,T)),S),B);
}
function FF(A,B,C,D,X,S,T)
{
return CMN((B & C)|((~B)& D),A,B,X,S,T);
}
function GG(A,B,C,D,X,S,T)
{
return CMN((B & D)|(C &(~D)),A,B,X,S,T);
}
function HH(A,B,C,D,X,S,T)
{
return CMN(B^C^D,A,B,X,S,T);
}
function II(A,B,C,D,X,S,T)
{
return CMN(C^(B|(~D)),A,B,X,S,T);
}
function AddUnsigned(X,y)
{
var lsw=(X&0xFFFF)+(y&0xFFFF);
var msw=(X>>16)+(y>>16)+(lsw>>16);
return(msw<<16)|(lsw&0xFFFF);
}
function BitRol(Val,Count)
{
return(Val<<Count)|(Val>>>(32-Count));
}
function Str2BinArray(StrToConv)
{
var Bin=Array();
var Mask=(1<<CharBits)-1;
for(var i=0;i<StrToConv.length*CharBits;i+=CharBits)
Bin|=(StrToConv.charCodeAt(i/CharBits)&Mask)<<(i%32);
return Bin;
}
function BinArr2Hex(BinArr)
{
var HexTab="0123456789abcdef";//"0123456789ABCDEF"
var StrRet="";
for(var i=0;i<BinArr.length*4;i++)
StrRet+=HexTab.charAt((BinArr>>((i%4)*8+4))&0xF)+HexTab.charAt((BinArr>>((i%4)*8))&0xF);
return StrRet;
}用法:调用StringMD5取得一个字符串的MD5值。
页:
[1]