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 & 0x3ffffffff) (LY & 0x3FFFFFF);
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 ConvertTtoWordArray (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 { LWORDCOUNT = (lbytecount- (lbytecount% 4) / 4; LbytePosition = (Lbytecount% 4) * 8; LWordArray [LWORDCOUNT] = (LWORDARRAY [LWORDCOUNT] | (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 = "" "" "" "" "", 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 blocksfor (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 (); } script>