MD5 encryption for JavaScript implementations
Last Update:2017-02-28
Source: Internet
Author: User
javascript| encryption <script>
/* MD5 message-digest Algorithm-javascript
Modification HISTORY:
1.0 16-feb-2001-phil Fresle ("A href=" mailto:sales@frez.co.uk ">sales@frez.co.uk </A>)-Initial Version (vb/asp code)
1.0 21-feb-2001-enrico Mosanghini ("A href=" mailto:erik504@yahoo.com ">erik504@yahoo.com </A>)-JavaScript porting
*/
function MD5 (smessage) {
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 & y) | ((~x) & Z); }
function G (x,y,z) {return (X & Z) | (Y & (~z)); }
function H (x,y,z) {return (x ^ y ^ z);}
function I (x,y,z) {return (y ^ (x | (~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 (smessage) {
var Lwordcount;
var lmessagelength = smessage.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] | (Smessage.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;
}
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;
Steps 1 and 2. Append padding bits and length and convert to words
x = Converttowordarray (smessage);
Step 3. Initialise
A = 0x67452301; b = 0xefcdab89; c = 0x98badcfe; D = 0x10325476;
Step 4. Process the message in 16-word blocks
for (k=0;k<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);
}
Step 5. Output the 128 bit digest
var temp= wordtohex (a) +wordtohex (b) +wordtohex (c) +wordtohex (d);
return Temp.tolowercase ();
}
</script>
<input type=text id=myinput><input type=text ondblclick= ' this.value=md5 (myinput.value) ' <p> is compared with Delphi implementation of MD5 encryption, English numerals are the same as the results of the encryption, Chinese is not the same, may be different from the processing of Chinese characters. The copyright declaration of the </P> <p> function is on top.
</P>