JS version MD5 (message-digest algorithm) encryption algorithm

Source: Internet
Author: User
Tags md5

/**
*
* MD5 (message-digest Algorithm)
* http://www.webtoolkit.info/
*
**/

var MD5 = function (string) {

function Rotateleft (lValue, ishiftbits) {
return (lvalue<<ishiftbits) | (lvalue>>> (32-ishiftbits));
}

function addunsigned (lx,ly) {
var lx4,ly4,lx8,ly8,lresult;
lX8 = (lX & 0x80000000);
lY8 = (lY & 0x80000000);
lX4 = (lX & 0x40000000);
lY4 = (lY & 0x40000000);
LResult = (lX & 0x3fffffff) + (lY & 0x3fffffff);
If (lX4 & lY4) {
return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
}
If (lX4 | lY4) {
If (lResult & 0x40000000) {
return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
} else {
return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
}
} else {
return (lResult ^ lX8 ^ lY8);
}
}

function F (x, y, z) {return (&) | ((~x) & z); }
function G (z/y) {return (x & Z) | (y & (~z)); }
function H (z/y) {return (x ^ y ^ z);}
function I (x, y, z) {return (y ^ | (~z))); }

function FF (a,b,c,d,x,s,ac) {
A = addunsigned (a, addunsigned (addunsigned (F (b, c, d), x), ac));
return addunsigned (rotateleft (a, s), b);
};

function GG (a,b,c,d,x,s,ac) {
A = addunsigned (a, addunsigned (addunsigned (G (b, c, d), x), ac));
return addunsigned (rotateleft (a, s), b);
};

function HH (a,b,c,d,x,s,ac) {
A = addunsigned (a, addunsigned (addunsigned (H (b, c, d), x), ac));
return addunsigned (rotateleft (a, s), b);
};

function II (a,b,c,d,x,s,ac) {
A = addunsigned (a, addunsigned (addunsigned (I (b, c, d), x), ac));
return addunsigned (rotateleft (a, s), b);
};

function Converttowordarray (string) {
var lwordcount;
var lmessagelength = string.length;
var lnumberofwords_temp1=lmessagelength + 8;
var lnumberofwords_temp2= (lnumberofwords_temp1-(lnumberofwords_temp1% 64))/64;
var lnumberofwords = (lnumberofwords_temp2+1) *16;
var Lwordarray=array (lNumberOfWords-1);
var lbyteposition = 0;
var lbytecount = 0;
While (lbytecount < Lmessagelength) {
Lwordcount = (lbytecount-(lbytecount% 4))/4;
Lbyteposition = (lbytecount% 4) *8;
lwordarray[lwordcount] = (lwordarray[lwordcount] | (string.charcodeat (lbytecount) <<lbyteposition));
lbytecount++;
}
Lwordcount = (lbytecount-(lbytecount% 4))/4;
Lbyteposition = (lbytecount% 4) *8;
lwordarray[lwordcount] = lwordarray[lwordcount] | (0x80<<lbyteposition);
lwordarray[lnumberofwords-2] = lmessagelength<<3;
lwordarray[lnumberofwords-1] = lmessagelength>>>29;
Return lwordarray;
};

function Wordtohex (lValue) {
var wordtohexvalue= "", wordtohexvalue_temp= "", lbyte,lcount;
For (lCount = 0;lcount<=3;lcount++) {
Lbyte = (lvalue>>> (lcount*8)) & 255;
Wordtohexvalue_temp = "0" + lbyte.tostring (16);
Wordtohexvalue = Wordtohexvalue + wordtohexvalue_temp.substr (wordtohexvalue_temp.length-2,2);
}
Return wordtohexvalue;
};

function Utf8encode (string) {
string = String.Replace (/\r\n/g, "\ n");
var utftext = "";

For (var n = 0; N < string.length; n++) {

var c = String.charcodeat (n);

If (c < 128) {
Utftext + = String.fromCharCode (c);
}
else if ((c > 127) && (c < 2048)) {
Utftext + = String.fromCharCode ((c >> 6) | 192);
Utftext + = String.fromCharCode ((c & 63) | 128);
}
else {
Utftext + = String.fromCharCode ((c >> 12) | 224);
Utftext + = String.fromCharCode (((c >> 6) & 63) | 128);
Utftext + = String.fromCharCode ((c & 63) | 128);
}

}

Return utftext;
};

var X=array ();
var k,aa,bb,cc,dd,a,b,c,d;
var s11=7, s12=12, s13=17, s14=22;
var s21=5, s22=9, s23=14, s24=20;
var s31=4, s32=11, s33=16, s34=23;
var s41=6, s42=10, s43=15, s44=21;

string = Utf8encode (string);

x = Converttowordarray (string);

A = 0x67452301; b = 0xefcdab89; c = 0x98badcfe; D = 0x10325476;

For (K=0;K&LT;X.LENGTH;K+=16) {
aa=a; bb=b; cc=c; dd=d;
A=ff (a,b,c,d,x[k+0], s11,0xd76aa478);
D=ff (d,a,b,c,x[k+1], s12,0xe8c7b756);
C=ff (c,d,a,b,x[k+2], s13,0x242070db);
B=ff (b,c,d,a,x[k+3], s14,0xc1bdceee);
A=ff (a,b,c,d,x[k+4], s11,0xf57c0faf);
D=ff (d,a,b,c,x[k+5], s12,0x4787c62a);
C=ff (c,d,a,b,x[k+6], s13,0xa8304613);
B=ff (b,c,d,a,x[k+7], s14,0xfd469501);
A=ff (a,b,c,d,x[k+8], s11,0x698098d8);
D=ff (d,a,b,c,x[k+9], s12,0x8b44f7af);
C=ff (c,d,a,b,x[k+10],s13,0xffff5bb1);
B=ff (b,c,d,a,x[k+11],s14,0x895cd7be);
A=ff (a,b,c,d,x[k+12],s11,0x6b901122);
D=ff (d,a,b,c,x[k+13],s12,0xfd987193);
C=ff (c,d,a,b,x[k+14],s13,0xa679438e);
B=ff (b,c,d,a,x[k+15],s14,0x49b40821);
A=gg (a,b,c,d,x[k+1], s21,0xf61e2562);
D=gg (d,a,b,c,x[k+6], s22,0xc040b340);
C=gg (c,d,a,b,x[k+11],s23,0x265e5a51);
B=gg (b,c,d,a,x[k+0], s24,0xe9b6c7aa);
A=gg (a,b,c,d,x[k+5], s21,0xd62f105d);
D=gg (d,a,b,c,x[k+10],s22,0x2441453);
C=gg (c,d,a,b,x[k+15],s23,0xd8a1e681);
B=gg (b,c,d,a,x[k+4], s24,0xe7d3fbc8);
A=gg (a,b,c,d,x[k+9], s21,0x21e1cde6);
D=gg (d,a,b,c,x[k+14],s22,0xc33707d6);
C=gg (c,d,a,b,x[k+3], s23,0xf4d50d87);
B=gg (b,c,d,a,x[k+8], s24,0x455a14ed);
A=gg (a,b,c,d,x[k+13],s21,0xa9e3e905);
D=gg (d,a,b,c,x[k+2], s22,0xfcefa3f8);
C=gg (c,d,a,b,x[k+7], s23,0x676f02d9);
B=gg (b,c,d,a,x[k+12],s24,0x8d2a4c8a);
A=HH (a,b,c,d,x[k+5], s31,0xfffa3942);
D=HH (d,a,b,c,x[k+8], s32,0x8771f681);
C=hh (c,d,a,b,x[k+11],s33,0x6d9d6122);
B=hh (b,c,d,a,x[k+14],s34,0xfde5380c);
A=HH (a,b,c,d,x[k+1], s31,0xa4beea44);
D=HH (d,a,b,c,x[k+4], s32,0x4bdecfa9);
C=HH (c,d,a,b,x[k+7], s33,0xf6bb4b60);
B=hh (b,c,d,a,x[k+10],s34,0xbebfbc70);
A=hh (a,b,c,d,x[k+13],s31,0x289b7ec6);
D=HH (d,a,b,c,x[k+0], s32,0xeaa127fa);
C=HH (c,d,a,b,x[k+3], s33,0xd4ef3085);
B=HH (b,c,d,a,x[k+6], s34,0x4881d05);
A=HH (a,b,c,d,x[k+9], s31,0xd9d4d039);
D=hh (d,a,b,c,x[k+12],s32,0xe6db99e5);
C=hh (c,d,a,b,x[k+15],s33,0x1fa27cf8);
B=HH (b,c,d,a,x[k+2], s34,0xc4ac5665);
A=ii (a,b,c,d,x[k+0], s41,0xf4292244);
D=ii (d,a,b,c,x[k+7], s42,0x432aff97);
C=ii (c,d,a,b,x[k+14],s43,0xab9423a7);
B=ii (b,c,d,a,x[k+5], s44,0xfc93a039);
A=ii (a,b,c,d,x[k+12],s41,0x655b59c3);
D=ii (d,a,b,c,x[k+3], s42,0x8f0ccc92);
C=ii (c,d,a,b,x[k+10],s43,0xffeff47d);
B=ii (b,c,d,a,x[k+1], s44,0x85845dd1);
A=ii (a,b,c,d,x[k+8], s41,0x6fa87e4f);
D=ii (d,a,b,c,x[k+15],s42,0xfe2ce6e0);
C=ii (c,d,a,b,x[k+6], s43,0xa3014314);
B=ii (b,c,d,a,x[k+13],s44,0x4e0811a1);
A=ii (a,b,c,d,x[k+4], s41,0xf7537e82);
D=ii (d,a,b,c,x[k+11],s42,0xbd3af235);
C=ii (c,d,a,b,x[k+2], s43,0x2ad7d2bb);
B=ii (b,c,d,a,x[k+9], s44,0xeb86d391);
a=addunsigned (a,aa);
b=addunsigned (b,bb);
c=addunsigned (c,cc);
d=addunsigned (d,dd);
}

var temp = Wordtohex (a) +wordtohex (b) +wordtohex (c) +wordtohex (d);

return Temp.tolowercase ();
}

JS version MD5 (message-digest algorithm) encryption algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.