MD5 encrypted JavaScript Implementation example
Last Update:2017-02-28
Source: Internet
Author: User
Encryption/* MD5 Message-digest Algorithm-javascript
' Modification HISTORY:
' 1.0 16-feb-2001-phil Fresle (sales@frez.co.uk)-Initial Version (vb/asp code)
' 1.0 21-feb-2001-enrico Mosanghini (erik504@yahoo.com)-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);