MD5 encrypted JavaScript implementation example

xiaoxiao2021-03-06  119

/ *? morgorithm?-?javascript'?modification?history:'??1.0?16-Feb-2001?-?phil?fresLet? (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 <

>> (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? |? {{

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? =? sMessageLength; 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?

LWORDCOUNT? =? (lbytecount- (lbytecount?%? 4) / 4;

LbytePosition? =? (lbytecount?%? 4) * 8;

LWORDARRAY [LWORDCOUNT]? =? (lwordArray [lwordcount]? |? (SMESSAGE.CHARCODEAT <

Lbytecount ;

}

LWORDCOUNT? =? (lbytecount- (lbytecount?%? 4) / 4;

LbytePosition? =? (lbytecount?%? 4) * 8;

LWORDARRAY [LWORDCOUNT] =? LWORDARRAY [LWORDCOUNT]? |? (0x80 <

LWORDARRAY [lnumberofwords-2]? =? LMessageLength << 3;

LWORDARRAY [lnumberofwords-1]? =? LMessageLength >>> 29;

RETURN? LWORDARRAY;

}

Function? Wordtohex (LVALUE)? {

VAR? WordtohexValue = "" "" "" "" "", 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?nd?2.?append?padding?bits?and?length?nd?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

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 ();

}?

转载请注明原文地址:https://www.9cbs.com/read-126670.html

New Post(0)