JavaScript version of SHA-1

xiaoxiao2021-03-06  113

/ * A JavaScript Implementation of the Secure Hash Algorithm, SHA-1, AS Defined * in FIPS PUB 180-1 * Copyright (c) Paul Johnston 2000 - 2002. * See http://pajhome.org.uk/site/ Legal.html for details. * /

/ * * Convert a 32-bit number to a hex string with ms-byte first * / var hex_chr = "0123456789abcdef"; function hex (num) {var str = ""; for (var j = 7; J> = 0 ; J-) Str = HEX_CHR.CHARAT ((NUM >> (j * 4)) & 0x0f); Return Str;}

/ * * Convert a string to a sequence of 16-word blocks, stored as an array. * Append padding bits and the length, as described in the SHA1 standard. * / Function str2blks_SHA1 (str) {var nblk = ((str. Length 8) >> 6) 1; VAR BLKS = New Array (NBLK * 16); for (var i = 0; i > 2] | = Str.Charcodeat (i) << (24 - (i% 4) * 8); BLKS [I >> 2] | = 0x80 << (24 - (i% 4) * 8); BLKS [NBLK * 16 - 1] = Str.Length * 8; Return Blks;}

/ * 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 rol (num, cnt) {Return (Num << CNT) | (NUM >>> (32 - CNT));}

/ * Perform the appropriate Triplet combination function for the current * ity * / function ft (t, b, c, d) {IF (t <20) Return (B & C) | (~ b) & d); IF (t <40) RETURN B ^ C ^ D; IF (T <60) Return (B & D) | (C & D); Return B ^ C ^ D;} / * * determine The appropriate additive constant for the current ity {RETURN (T <20)? 1518500249: (t <40)? 1859775393: (T <60)? -189497514;

/ * * Take a string and return the hex representation of its SHA-1. * / Function Calcsha1 (STR) {var x = str2blks_sha1 (str); var); VAR W = New Array (80);

VAR A = 1732584193; VAR B = -271733879; VAR C = -1732584194; var d = 271733878; var E = -1009589776;

For (var i = 0; i = 16) {var Olda = a; var = b; var Oldc = C; var Oldd = D; var Olde = E;

For (var j = 0; j <80; j ) {IF (j <16) w [j] = x [i j]; else w [j] = rol (w [j-3] ^ w [j -8] ^ w [j-14] ^ w [j-16], 1); var t = safe_add (SAFE_ADD (ROL (A, 5), FT (J, B, C, D)), SAFE_ADD (SAFE_ADD (e, w [j]), kt (j))); E = D; D = C; c = rol (b, 30); b = a; a = t;}

A = SAFE_ADD (A, OLDA); B = SAFE_ADD (B, OLDB); C = SAFE_ADD (C, OLDC); D = SAFE_ADD (D, OLDD); E = SAFE_ADD (E, OLDE);} Return HEX (A ) HEX (B) HEX (C) HEX (D) HEX (E);

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

New Post(0)