MD5 for JS

xiaoxiao2021-03-06  55

Function array (n) {for (i = 0; i

Function integer (n) {RETURN N% (0xfffffffff 1);}

Function SHR (A, B) {a = integer (a); b = integer (b); if (a-0x80000000> = 0) {a = a% 0x80000000; A >> = B; A = 0x40000000 >> ( B-1);} else a >> = b; returnon

Function shl1 (a) {a = a% 0x80000000; IF (A & 0x40000000 == 0x40000000) {a- = 0x40000000; A * = 2; A = 0x80000000;} else a * = 2; return A;}

Function SHL (a, b) {a = integer (a); b = integer (b); for (var i = 0; i

Function and (a, b) {a = integer (a); b = integer (b); var t1 = (A-0x80000000); var T2 = (b-0x80000000); if (t1> = 0) IF (T2 > = 0) RETURN ((T1 & T2) 0x80000000); Else Return (T1 & B); ELSE IF (T2> = 0) Return (A & T2); Else Return (A & B);}

Function or (a, b) {a = integer (a); b = integer (b); var T1 = (A-0x80000000); var T2 = (b-0x80000000); if (t1> = 0) IF (T2 > = 0) RETURN ((T1 | T2) 0x80000000); Else Return ((T1 | B) 0x80000000); Else IF (T2> = 0) Return ((a | t2) 0x80000000); Else Return (A | b);}

Function XOR (a, b) {a = integer (a); b = integer (b); var T1 = (a-0x80000000); var T2 = (b-0x80000000); if (t1> = 0) IF (T2 > = 0) RETURN (T1 ^ T2); ELSE RETURN ((T1 ^ b) 0x80000000); ELSE IF (T2> = 0) Return ((A ^ t2) 0x80000000); Else Return (A ^ b); }

Function Not (a) {a = integer (a); return (0xffffffff-a);}

/ * Here Begin The Real Algorithm * /

VAR State = new array (4); var count = new array (2); count [0] = 0; count [1] = 0; var buffer = new array (64); var transformuffer = new array (16); Var DigestBits = New Array (16); var S11 = 7; var S12 = 12; var S13 = 17; var S14 = 22; var S21 = 5; var S22 = 9; var S23 = 14; var S24 = 20; var S31 = 4; var S32 = 11; var S33 = 16; var S34 = 23; var s41 = 6; var s42 = 10; var s43 = 15; var s44 = 21;

Function F (x, y, z) {RETURN OR (AND (x, y), AND (not (x), z));}

Function G (X, Y, Z) {RETURN OR (AND (X, Z), AND (Y, NOT (Z)));}

Function h (x, y, z) {Return XOR (xor (x, y), z);}

Function I (X, Y, Z) {RETURN XOR (Y, OR (X, NOT (Z)));}

Function RotateLeft (A, N) {Return OR (SHL (A, N), (SHR (A, (32 - N))));}

Function FF (A, B, C, D, X, S, AC) {a = a f (b, d) x ac; a = rotateleft (a, s); A = A B; Return A;

Function GG (A, B, C, D, X, S, AC) {a = a g (b, c, d) x ac; a = rotateleft (a, s); A = A B; Return A;

Function HH (A, B, C, D, X, S, AC) {a = a h (b, c, d) x ac; a = rotateleft (a, s); A = a b; Return A;

Function II (A, B, C, D, X, S, AC) {a = a i (b, c, d) x ac; a = rotateleft (a, s); A = A B; Return A;

Function Transform (BUF, Offset) {var A = 0, B = 0, C = 0, D = 0; VAR X = Transformuffer; A = State [0]; B = State [1]; c = state [2] ; D = state [3]; for (i = 0; i <16; i ) {x [i] = AND (BUF [i * 4 offset], 0xFF); for (j = 1; j <4; J ) {x [i] = SHL (and (buf [i * 4 j offset], 0xff), j * 8);}} / * runk 1 * / a = ff (A, B, C, D, x [0], S11, 0xD76AA478); / * 1 * / d = ff (d, A, b, c, x [1], s12, 0xe8c7b756); / * 2 * / c = ff (c, D, A, B, X [2], S13, 0x242070dB); / * 3 * / b = ff (B, C, D, A, X [3], S14, 0XC1BDCEEE); / * 4 * / a = FF (A, B, C, D, X [4], S11, 0xF57C0FAF); / * 5 * / d = ff (D, A, B, C, X [5], S12, 0X4787C62A); / * 6 * / C = ff (C, D, A, B, X [6], S13, 0XA8304613); / * 7 * / b = ff (B, C, D, A, X [7], S14, 0xFD469501) ; / * 8 * / a = ff (A, B, C, D, X [8], S11, 0x698098D8); / * 9 * / d = ff (d, a, b, c, x [9], S12, 0x8B44F7AF); / * 10 * / c = ff (C, D, A, B, X [10], S13, 0xFFFF5BB1); / * 11 * / b = ff (B, C, D, A, X [11], S14, 0x895CD7BE); / * 12 * / a = ff (A, B, C, D, X [12], S11, 0X6B90112 2); / * 13 * / d = ff (D, A, B, C, X [13], S12, 0xFD987193); / * 14 * / c = ff (C, D, A, B, X [14 ], S13, 0xA679438E); / * 15 * / b = ff (B, C, D, A, X [15], S14, 0X49B40821); / * 16 * /

/ * ROUND 2 * / A = GG (A, B, C, D, X [1], S21, 0XF61E2562); / * 17 * / d = GG (D, A, B, C, X [6], S22, 0XC040B340); / * 18 * / c = GG (C, D, A, B, X [11], S23, 0X265E5A51); / * 19 * / b = GG (B, C, D, A, X [0], S24, 0XE9B6C7AA); / * 20 * / a = gg (A, B, C, D, X [5], S21, 0XD62F105D); / * 21 * / d = GG (D, A, B , C, X [10], S22, 0X2441453); / * 22 * ​​/ c = gg (C, D, A, B, X [15], S23, 0XD8A1E681); / * 23 * / b = GG (B , C, D, A, X [4], S24, 0XE7D3FBC8); / * 24 * / a = GG (A, B, C, D, X [9], S21, 0x21E1CDE6); / * 25 * / d = GG (D, A, B, C, X [14], S22, 0XC33707D6); / * 26 * / c = gg (C, D, A, B, X [3], S23, 0XF4D50D87); / * 27 * / b = gg (B, C, D, A, X [8], S24, 0X455A14ED); / * 28 * / a = gg (A, B, C, D, X [13], S21, 0XA9E3E905 ); / * 29 * / d = gg (D, A, B, C, X [2], S22, 0XFCEFA3F8); / * 30 * / c = gg (C, D, A, B, X [7] , S23, 0x676F02D9); / * 31 * / b = gg (B, C, D, A, X [12], S24, 0x8D2A4C8A); / * 32 * /

/ * ROUND 3 * / a = HH (A, B, C, D, X [5], S31, 0xFFFA3942); / * 33 * / d = HH (D, A, B, C, X [8], S32, 0x8771F681); / * 34 * / c = HH (C, D, A, B, X [11], S33, 0X6D9D6122); / * 35 * / b = HH (B, C, D, A, X [14], S34, 0xFDE5380C); / * 36 * / a = HH (A, B, C, D, X [1], S31, 0XA4Beea44); / * 37 * / d = HH (D, A, B , C, X [4], S32, 0x4BDECFA9); / * 38 * / c = HH (C, D, A, B, X [7], S33, 0xF6BB4B60); / * 39 * / b = HH (B , C, D, A, X [10], S34, 0XBEBFBC70); / * 40 * / a = HH (A, B, C, D, X [13], S31, 0x289B7EC6); / * 41 * / d = HH (D, A, B, C, X [0], S32, 0XEAA127FA); / * 42 * / c = HH (C, D, A, B, X [3], S33, 0xD4eF3085); / * 43 * / b = HH (B, C, D, A, X [6], S34, 0X4881D05); / * 44 * / a = HH (A, B, C, D, X [9], S31, 0XD9D4D039 ); / * 45 * / d = HH (D, A, B, C, X [12], S32, 0xE6DB99E5); / * 46 * / c = HH (C, D, A, B, X [15] , S33, 0x1FA27CF8); / * 47 * / b = HH (B, C, D, A, X [2], S34, 0XC4AC5665); / * 48 * /

/ * ROUND 4 * / A = II (A, B, C, D, X [0], S41, 0XF4292244); / * 49 * / d = II (D, A, B, C, X [7], S42, 0x432AFF97); / * 50 * / c = II (C, D, A, B, X [14], S43, 0XAB9423A7); / * 51 * / b = II (B, C, D, A, X [5], S44, 0XFC93A039); / * 52 * / a = II (A, B, C, D, X [12], S41, 0X65559C3); / * 53 * / d = II (D, A, B , C, X [3], S42, 0x8F0CCC92); / * 54 * / c = II (C, D, A, B, X [10], S43, 0xffeff47d); / * 55 * / b = II (B , C, D, A, X [1], S44, 0X85845DD1); / * 56 * / a = II (A, B, C, D, X [8], S41, 0X6FA87E4F); / * 57 * / d = II (D, A, B, C, X [15], S42, 0XFE2CE6E0); / * 58 * / c = II (C, D, A, B, X [6], S43, 0XA3014314); / * 59 * / b = II (B, C, D, A, X [13], S44, 0X4E0811A1); / * 60 * / a = II (A, B, C, D, X [4], S41, 0XF7537E82 ); / * 61 * / d = II (D, A, B, C, X [11], S42, 0XBD3AF235); / * 62 * / c = II (C, D, A, B, X [2] , S43, 0x2AD7D2BB); / * 63 * / b = II (B, C, D, A, X [9], S44, 0XEB86D391); / * 64 * / state [0] = a; state [1] = B; state [2] = C; state [3] = D;

}

Function init () {count [0] = count [1] = 0; state [0] = 0x67452301; State [1] = 0xEfcdab89; state [2] = 0x98badcfe; state [3] = 0x10325476; for (i = 0 i

Function update (b) {var index, i; index = and (shr (count [0], 3), 0x3f); if (count [0] <0xffffffff-7) count [0] = 8; else {count [1] ; count [0] - = 0xfffffffff 1; count [0] = 8;} buffer [index] = AND (b, 0xff); if (index> = 63) {Transform (buffer, 0 }}

Function Finish () {var bits = new array (8); var padding; var padding; var i = 0, index = 0, padlen = 0;

For (i = 0; i <4; i ) {bits [i] = and (shr (count [0), (i * 8)), 0xFF);} for (i = 0; i <4; i ) {BITS [i 4] = and (shr (count [1], (i * 8)), 0xFF);} index = and (shr (count [0], 3), 0x3f); Padlen = (Index < 56)? (56 - index): (120 - index); padding = new array (64); padding [0] = 0x80; for (i = 0; i

/ * End of the md5 algorithm * /

Function hexa (n) {var hexa_h = "0123456789abcdef"; var hexa_c = ""; var hexa_m = n; for (hexa_i = 0; hexa_i <8; hexa_i ) {hexa_c = hexa_h.Charat (Math.abs (HEXA_M)%% 16) HEXA_C; HEXA_M = Math.Floor (HEXA_M / 16);} Return HEXA_C;}

"! /".? Var ascii = "01234567890123456789012345678901" # $% & '() * , - / 0123456789:; <=> @ ABCDEFGHIJKLMNOPQRSTUVWXYZ " " [//] ^ _ `abcdefghijklmnopqrstuvwxyz {|} ~";

Function MD5 (Entree) {Var L, S, K, KA, KB, KC, KD;

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

New Post(0)