michael@0: /* michael@0: * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined michael@0: * in FIPS PUB 180-1 michael@0: * Version 2.1 Copyright Paul Johnston 2000 - 2002. michael@0: * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet michael@0: * Distributed under the BSD License michael@0: * See http://pajhome.org.uk/crypt/md5 for details. michael@0: */ michael@0: michael@0: /* michael@0: * Configurable variables. You may need to tweak these to be compatible with michael@0: * the server-side, but the defaults work in most cases. michael@0: */ michael@0: var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ michael@0: var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ michael@0: var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ michael@0: michael@0: /* michael@0: * These are the functions you'll usually want to call michael@0: * They take string arguments and return either hex or base-64 encoded strings michael@0: */ michael@0: function hexsha1(s){return binb2hex(coresha1(str2binb(s),s.length * chrsz));} michael@0: function b64sha1(s){return binb2b64(coresha1(str2binb(s),s.length * chrsz));} michael@0: function strsha1(s){return binb2str(coresha1(str2binb(s),s.length * chrsz));} michael@0: function hexhmacsha1(key, data){ return binb2hex(corehmacsha1(key, data));} michael@0: function b64hmacsha1(key, data){ return binb2b64(corehmacsha1(key, data));} michael@0: function strhmacsha1(key, data){ return binb2str(corehmacsha1(key, data));} michael@0: michael@0: /* michael@0: * Perform a simple self-test to see if the VM is working michael@0: */ michael@0: function sha1vmtest() michael@0: { michael@0: return hexsha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d"; michael@0: } michael@0: michael@0: /* michael@0: * Calculate the SHA-1 of an array of big-endian words, and a bit length michael@0: */ michael@0: function coresha1(x, len) michael@0: { michael@0: /* append padding */ michael@0: x[len >> 5] |= 0x80 << (24 - len % 32); michael@0: x[((len + 64 >> 9) << 4) + 15] = len; michael@0: michael@0: var w = Array(80); michael@0: var a = 1732584193; michael@0: var b = -271733879; michael@0: var c = -1732584194; michael@0: var d = 271733878; michael@0: var e = -1009589776; michael@0: michael@0: for(var i = 0; i < x.length; i += 16) michael@0: { michael@0: var olda = a; michael@0: var oldb = b; michael@0: var oldc = c; michael@0: var oldd = d; michael@0: var olde = e; michael@0: michael@0: for(var j = 0; j < 80; j++) michael@0: { michael@0: if(j < 16) w[j] = x[i + j]; michael@0: else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); michael@0: var t = safeadd(safeadd(rol(a, 5), sha1ft(j, b, c, d)), michael@0: safeadd(safeadd(e, w[j]), sha1kt(j))); michael@0: e = d; michael@0: d = c; michael@0: c = rol(b, 30); michael@0: b = a; michael@0: a = t; michael@0: } michael@0: michael@0: a = safeadd(a, olda); michael@0: b = safeadd(b, oldb); michael@0: c = safeadd(c, oldc); michael@0: d = safeadd(d, oldd); michael@0: e = safeadd(e, olde); michael@0: } michael@0: return Array(a, b, c, d, e); michael@0: michael@0: } michael@0: michael@0: /* michael@0: * Perform the appropriate triplet combination function for the current michael@0: * iteration michael@0: */ michael@0: function sha1ft(t, b, c, d) michael@0: { michael@0: if(t < 20) return (b & c) | ((~b) & d); michael@0: if(t < 40) return b ^ c ^ d; michael@0: if(t < 60) return (b & c) | (b & d) | (c & d); michael@0: return b ^ c ^ d; michael@0: } michael@0: michael@0: /* michael@0: * Determine the appropriate additive constant for the current iteration michael@0: */ michael@0: function sha1kt(t) michael@0: { michael@0: return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : michael@0: (t < 60) ? -1894007588 : -899497514; michael@0: } michael@0: michael@0: /* michael@0: * Calculate the HMAC-SHA1 of a key and some data michael@0: */ michael@0: function corehmacsha1(key, data) michael@0: { michael@0: var bkey = str2binb(key); michael@0: if(bkey.length > 16) bkey = coresha1(bkey, key.length * chrsz); michael@0: michael@0: var ipad = Array(16), opad = Array(16); michael@0: for(var i = 0; i < 16; i++) michael@0: { michael@0: ipad[i] = bkey[i] ^ 0x36363636; michael@0: opad[i] = bkey[i] ^ 0x5C5C5C5C; michael@0: } michael@0: michael@0: var hash = coresha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz); michael@0: return coresha1(opad.concat(hash), 512 + 160); michael@0: } michael@0: michael@0: /* michael@0: * Add integers, wrapping at 2^32. This uses 16-bit operations internally michael@0: * to work around bugs in some JS interpreters. michael@0: */ michael@0: function safeadd(x, y) michael@0: { michael@0: var lsw = (x & 0xFFFF) + (y & 0xFFFF); michael@0: var msw = (x >> 16) + (y >> 16) + (lsw >> 16); michael@0: return (msw << 16) | (lsw & 0xFFFF); michael@0: } michael@0: michael@0: /* michael@0: * Bitwise rotate a 32-bit number to the left. michael@0: */ michael@0: function rol(num, cnt) michael@0: { michael@0: return (num << cnt) | (num >>> (32 - cnt)); michael@0: } michael@0: michael@0: /* michael@0: * Convert an 8-bit or 16-bit string to an array of big-endian words michael@0: * In 8-bit function, characters >255 have their hi-byte silently ignored. michael@0: */ michael@0: function str2binb(str) michael@0: { michael@0: var bin = Array(); michael@0: var mask = (1 << chrsz) - 1; michael@0: for(var i = 0; i < str.length * chrsz; i += chrsz) michael@0: bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32); michael@0: return bin; michael@0: } michael@0: michael@0: /* michael@0: * Convert an array of big-endian words to a string michael@0: */ michael@0: function binb2str(bin) michael@0: { michael@0: var str = ""; michael@0: var mask = (1 << chrsz) - 1; michael@0: for(var i = 0; i < bin.length * 32; i += chrsz) michael@0: str += String.fromCharCode((bin[i>>5] >>> (24 - i%32)) & mask); michael@0: return str; michael@0: } michael@0: michael@0: /* michael@0: * Convert an array of big-endian words to a hex string. michael@0: */ michael@0: function binb2hex(binarray) michael@0: { michael@0: var hextab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; michael@0: var str = ""; michael@0: for(var i = 0; i < binarray.length * 4; i++) michael@0: { michael@0: str += hextab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + michael@0: hextab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF); michael@0: } michael@0: return str; michael@0: } michael@0: michael@0: /* michael@0: * Convert an array of big-endian words to a base-64 string michael@0: */ michael@0: function binb2b64(binarray) michael@0: { michael@0: var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; michael@0: var str = ""; michael@0: for(var i = 0; i < binarray.length * 4; i += 3) michael@0: { michael@0: var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16) michael@0: | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) michael@0: | ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF); michael@0: for(var j = 0; j < 4; j++) michael@0: { michael@0: if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; michael@0: else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); michael@0: } michael@0: } michael@0: return str; michael@0: }