JavaScript written by MD5 related encryption functions, including HMAC

zhaozj2021-02-16  58

/ * * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. * Other contributors: Greg Holt, Andrew Kepert, Ydnar , Lostinet * Distributed Under The BSD license * See http://pajhome.org.uk/crypt/md5 for more info. * /

. / * * Configurable variables You may need to tweak these to be compatible with * the server-side, but the defaults work in most cases * / var hexcase = 0; / * hex output format 0 - lowercase;.. 1 - uppercase * / var b64pad = ""; / * base-64 pad character. "=" = "for strict RFC Compliance * / var chrsz = 8; / * bits per input character. 8 - ascii; 16 - unicode * /

/ * * These are the functions you'll usually want to call * They take string arguments and return either hex or base-64 encoded strings * / function hex_md5 (s) {return binl2hex (core_md5 (str2binl (s), s.length * chrsz));} Function B64_MD5 (S) {RETURN BINL2B64 (Core_MD5 (STR2BINL (S), S.Length * Chrsz);} Function STR_MD5 (S) {RETURN BINL2STR (Core_MD5 (Str2binl (s), S.LENGTH * chrsz));} function hex_hmac_md5 (key, data) {return binl2hex (core_hmac_md5 (key, data));} function b64_hmac_md5 (key, data) {return binl2b64 (core_hmac_md5 (key, data));} function str_hmac_md5 (key DATA) {Return BINL2STR (Core_HMAC_MD5 (Key, Data));

/ * * Perform a Simple Self-test to see if the vm is working * / function md5_vm_test () {RETURN HEX_MD5 ("ABC") == "900150983cd24fb0d6963f7d28e17f72";}

/ * * Calculate the md5 of an array of little-endian word, and a bit length * / function core_md5 (x, len) {/ * append padding * / x [len >> 5] | = 0x80 << ((Len )% 32); ((LEN 64) >>> 9) << 4) 14] = len; var a = 1732584193; var b = -271733879; var c = -1732584194; var d = 271733878 ;

For (var i = 0; i

A = MD5_FF (A, B, C, D, X [i 0], 7, -680876936); D = MD5_FF (D, A, B, C, X [i 1], 12, -389564586); c = MD5_FF (C, D, A, B, X [i 2], 17, 606105819); B = MD5_FF (B, C, D, A, X [i 3], 22, -1044525330); A = MD5_FF (A , B, C, D, X [I 4], 7, -176418897); D = MD5_FF (D, A, B, C, X [i 5], 12, 1200080426); C = MD5_FF (C, D, A, B, X [i 6], 17, -1473231341); b = MD5_FF (B, C, D, A, X [i 7], 22, -45705983); A = MD5_FF (A, B, C, D, X [i 8], 7, 1770035416); D = MD5_FF (D, A, B, C, X [i 9], 12, -1958414417); C = MD5_FF (C, D, A, B, X [i 10], 17, -42063); b = MD5_FF (B, C, D, A, X [i 11], 22, -1990404162); A = MD5_FF (A, B, C, D, X [i 12], 7, 1804603682); D = MD5_FF (D, A, B, C, X [i 13], 12, -40341101); C = MD5_FF (C, D, A, B, X [ i 14], 17, -1502002290); B = MD5_FF (B, C, D, A, X [i 15], 22, 1236535329);

A = MD5_GG (A, B, C, D, X [i 1], 5, -165796510); D = MD5_GG (D, A, B, C, X [i 6], 9, -1069501632); c = MD5_GG (C, D, A, B, X [i 11], 14, 643717713); B = Md5_GG (B, C, D, A, X [i 0], 20, -373897302); A = MD5_GG ( A, B, C, D, X [i 5], 5, -701558691); D = MD5_GG (D, A, B, C, X [i 10], 9, 38016083); C = MD5_GG (C, D, A, B, X [i 15], 14, -660478335); B = MD5_GG (B, C, D, A, X [i 4], 20, -405537848); A = MD5_GG (A, B , C, D, X [I 9], 5, 568446438); D = MD5_GG (D, A, B, C, X [i 14], 9, -1019803690); C = MD5_GG (C, D, A , B, X [I 3], 14, -187363961); B = MD5_GG (B, C, D, A, X [i 8], 20, 1163531501); A = MD5_GG (A, B, C, D, X [i 13], 5, -1444681467); D = MD5_GG (D, A, B, C, X [i 2], 9, -51403784); C = MD5_GG (C, D, A, B, X [i 7], 14, 1735328473); B = MD5_GG (B, C, D, A, X [i 12], 20, -1926607734);

A = MD5_HH (A, B, C, D, X [i 5], 4, -378558); D = MD5_HH (D, A, B, C, X [i 8], 11, -2022574463); c = MD5_HH (C, D, A, B, X [I 11], 16, 1839030562); B = MD5_HH (B, C, D, A, X [i 14], 23, -35309556); A = MD5_HH (A, B, C, D, X [I 1], 4, -1530992060); D = MD5_HH (D, A, B, C, X [i 4], 11, 1272893353); C = MD5_HH (C, D, A, B, X [i 7], 16, -155497632); B = MD5_HH (B, C, D, A, X [i 10], 23, -1094730640); A = MD5_HH (A, B , C, D, X [I 13], 4, 681279174); D = MD5_HH (D, A, B, C, X [i 0], 11, -358537222); C = MD5_HH (C, D, A , B, X [i 3], 16, -722521979); B = MD5_HH (B, C, D, A, X [i 6], 23, 76029189); A = MD5_HH (A, B, C, D, X [i 9], 4, -640364487); D = MD5_HH (D, A, B, C, X [i 12], 11, -421815835); C = MD5_HH (C, D, A, B, X [i 15], 16, 530742520); B = MD5_HH (B, C, D, A, X [i 2], 23, -995338651);

A = MD5_II (A, B, C, D, X [i 0], 6, -198630844); D = MD5_II (D, A, B, C, X [i 7], 10, 1126891415); C = MD5_II (C, D, A, B, X [I 14], 15, -1416354905); B = MD5_II (B, C, D, A, X [i 5], 21, -57434055); A = MD5_II ( A, B, C, D, X [i 12], 6, 1700485571); D = MD5_II (D, A, B, C, X [i 3], 10, -1894986606); C = MD5_II (C, D, A, B, X [i 10], 15, -1051523); B = MD5_II (B, C, D, A, X [i 1], 21, -2054922799); A = MD5_II (A, B , C, D, X [i 8], 6, 1873313359); D = MD5_II (D, A, B, C, X [i 15], 10, -30611744); C = MD5_II (C, D, A , B, X [i 6], 15, -1560198380); B = MD5_II (B, C, D, A, X [i 13], 21, 1309151649); A = MD5_II (A, B, C, D , x [i 4], 6, -145523070); D = MD5_II (D, A, B, C, X [i 11], 10, -1120210379); C = MD5_II (C, D, A, B, X [i 2], 15, 718787259); b = md5_ii (B, C, D, A, X [i 9], 21, -343485551); A = SAFE_ADD (A, OLDA); B = SAFE_ADD (B, OLDB); C = SAFE_ADD (C, OLDC); D = SAFE_ADD (D, OLDD);} Return Array (A, B, C, D);

}

/ * * The releases IMPLEMENT THE FOUR Basic Operations The algorithm Uses. * / Function MD5_CMN (q, a, b, x, s, t) {return safe_add (bit_rol (Safe_ADD (A, Q), SAFE_ADD (X, T)), s), b);} function MD5_FF (A, B, C, D, X, S, T) {RETURN MD5_CMN ((B & C) | ((~ b) & d), A, B , X, S, T);} Function MD5_GG (A, B, C, D, X, S, T) {RETURN MD5_CMN ((B & D) | (C & (~ D)), A, B, X , S, T);} function MD5_HH (A, B, C, D, X, S, T) {RETURN MD5_CMN (B ^ C ^ D, A, B, X, S, T);} function MD5_II (a , B, C, D, X, S, T) {RETURN MD5_CMN (C ^ (B | (~ D)), A, B, X, S, T);}

/ * * Calculate the HMAC-MD5, OF A KEY AND SOME DATA * / FUNCTION CORE_HMAC_MD5 (Key, DATA) {var Bkey = str2binl (key); if (bkey.length> 16) bkey = core_md5 (BKEY, Key.Length * chrsz); VAR iPad = array (16), opAd = array (16); for (var i = 0; i <16; i ) {ipad [i] = bkey [i] ^ 0x36363636; opad [i] = BKEY [I] ^ 0x5c5c5c5c;}

Var hash = core_md5 (iPad.Concat), 512 Data.Length * Chrsz); Return Core_MD5 (Opad.concat (Hash), 512 128);

/ * Add Integers, Wrapping AT 2 ^ 32. This Uses 16-bit Operations Internally * To Work Around Bugs in Some JS Interpreters. * / Function Safe_Add (x, y) {var Lsw = (x & 0xfff) (Y & 0xfff); VAR MSW = (x >> 16) (Y >> 16) (LSW >> 16); Return (MSW << 16) | (Lsw & 0xfff);}

/ * * Bitwise Rotate a 32-bit number to the left. * / Function bit_rol (num, cnt) {return (Num << CNT) | (NUM >>> (32 - CNT));}

/ * * Convert a string to an array of little-endian Words * if Chrs> 255 Have Their Hi-byte Silently Ignored. * / Function Str2binl (STR) {var bin = array (); var Mask = 1 << chrsz) - 1; for (var i = 0; i > 5] | = (Str.Charcodeat (I / Chrsz) & mask) < <(i% 32); Return Bin;

/ * * * Convert an array of little-endian words to a string * / function binl2str (bin) {var str = ""; var mask = (1 << chrsz) - 1; for (var i = 0; i > 5] >>> (I% 32)) & mask; Return Str;}

/ * * Convert An Array of Little-Endian Words to a hex string. * / Function BINL2HEX (BINARRAY) {var hex_tab = hexcase? "0123456789abcdef": "0123456789abcdef"; var str = ""; for (var i = 0; I > 2] >> ((i% 4) * 8 4)) & 0xf) hex_tab.charat ((BinArray [I >> 2] >> ((i% 4) * 8)) & 0xF);} Return Str;} / * * Convert An Array of Little-Endian Words to a base-64 string * / function binl2b64 (binaRray ) {var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /"; var str = ""; for (var i = 0; i > 2] >> 8 * (I% 4)) & 0xff) << 16) | ((Binarray [i 1 >> 2] >> 8 * ((i 1)% 4)) & 0xff) << 8 ) | ((Binarray [i 2 >> 2] >> 8 * ((i 2)% 4)) & 0xff); for (var j = 0; j <4; j ) {if (i * 8 J * 6> Binarray.Length * 32) STR = B64Pad; Else Str = Tab.Charat ((Triplet >> 6 * (3-j)) & 0x3f);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

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

New Post(0)