diff --git a/bower.json b/bower.json index 156e43e2..dc7430ca 100644 --- a/bower.json +++ b/bower.json @@ -26,6 +26,7 @@ "menu": "~0.7.5", "pako": "~0.2.5", "google-diff-match-patch": "~0.1.0", - "daffy": "~1.0.1" + "daffy": "~1.0.1", + "jsSHA": "~1.5.0" } } diff --git a/lib/client/edit.js b/lib/client/edit.js index 544a14fc..5a4cf341 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -472,8 +472,8 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format; } function sha(callback) { - var dir = CloudCmd.LIBDIR, - url = dir + 'sha/jsSHA.js'; + var dir = '/modules/jsSHA/', + url = dir + 'src/sha1.js'; DOM.load.js(url, function() { var shaObj, hash, error, diff --git a/lib/sha/jsSHA.js b/lib/sha/jsSHA.js deleted file mode 100644 index cb179a36..00000000 --- a/lib/sha/jsSHA.js +++ /dev/null @@ -1,1527 +0,0 @@ -/** - * @preserve A JavaScript implementation of the SHA family of hashes, as - * defined in FIPS PUB 180-2 as well as the corresponding HMAC implementation - * as defined in FIPS PUB 198a - * - * Copyright Brian Turek 2008-2014 - * Distributed under the BSD License - * See http://caligatio.github.com/jsSHA/ for more information - * - * Several functions taken from Paul Johnston - */ - - /** - * SUPPORTED_ALGS is the stub for a compile flag that will cause pruning of - * functions that are not needed when a limited number of SHA families are - * selected - * - * @define {number} ORed value of SHA variants to be supported - * 1 = SHA-1, 2 = SHA-224/SHA-256, 4 = SHA-384/SHA-512 - */ -var SUPPORTED_ALGS = 4 | 2 | 1; - -(function (global) -{ - "use strict"; - /** - * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number - * - * @private - * @constructor - * @this {Int_64} - * @param {number} msint_32 The most significant 32-bits of a 64-bit number - * @param {number} lsint_32 The least significant 32-bits of a 64-bit number - */ - function Int_64(msint_32, lsint_32) - { - this.highOrder = msint_32; - this.lowOrder = lsint_32; - } - - /** - * Convert a string to an array of big-endian words - * - * @private - * @param {string} str String to be converted to binary representation - * @param {string} utfType The Unicode type, UTF8 or UTF16, to use to - * encode the source string - * @return {{value : Array., binLen : number}} Hash list where - * "value" contains the output number array and "binLen" is the binary - * length of "value" - */ - function str2binb(str, utfType) - { - var bin = [], codePnt, binArr = [], byteCnt = 0, i, j, offset; - - if ("UTF8" === utfType) - { - for (i = 0; i < str.length; i += 1) - { - codePnt = str.charCodeAt(i); - binArr = []; - - if (0x80 > codePnt) - { - binArr.push(codePnt); - } - else if (0x800 > codePnt) - { - binArr.push(0xC0 | (codePnt >>> 6)); - binArr.push(0x80 | (codePnt & 0x3F)); - } - else if ((0xd800 > codePnt) || (0xe000 <= codePnt)) { - binArr.push( - 0xe0 | (codePnt >>> 12), - 0x80 | ((codePnt >>> 6) & 0x3f), - 0x80 | (codePnt & 0x3f) - ); - } - else - { - i += 1; - codePnt = 0x10000 + (((codePnt & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff)); - binArr.push( - 0xf0 | (codePnt >>> 18), - 0x80 | ((codePnt >>> 12) & 0x3f), - 0x80 | ((codePnt >>> 6) & 0x3f), - 0x80 | (codePnt & 0x3f) - ); - } - - for (j = 0; j < binArr.length; j += 1) - { - offset = byteCnt >>> 2; - while (bin.length <= offset) - { - bin.push(0); - } - bin[offset] |= binArr[j] << (24 - (8 * (byteCnt % 4))); - byteCnt += 1; - } - } - } - else if ("UTF16" === utfType) - { - for (i = 0; i < str.length; i += 1) - { - codePnt = str.charCodeAt(i); - - offset = byteCnt >>> 2; - while (bin.length <= offset) - { - bin.push(0); - } - bin[offset] |= str.charCodeAt(i) << (16 - (8 * (byteCnt % 4))); - byteCnt += 2; - } - } - return {"value" : bin, "binLen" : byteCnt * 8}; - } - - /** - * Convert a hex string to an array of big-endian words - * - * @private - * @param {string} str String to be converted to binary representation - * @return {{value : Array., binLen : number}} Hash list where - * "value" contains the output number array and "binLen" is the binary - * length of "value" - */ - function hex2binb(str) - { - var bin = [], length = str.length, i, num, offset; - - if (0 !== (length % 2)) - { - throw "String of HEX type must be in byte increments"; - } - - for (i = 0; i < length; i += 2) - { - num = parseInt(str.substr(i, 2), 16); - if (!isNaN(num)) - { - offset = i >>> 3; - while (bin.length <= offset) - { - bin.push(0); - } - bin[i >>> 3] |= num << (24 - (4 * (i % 8))); - } - else - { - throw "String of HEX type contains invalid characters"; - } - } - - return {"value" : bin, "binLen" : length * 4}; - } - - /** - * Convert a string of raw bytes to an array of big-endian words - * - * @private - * @param {string} str String of raw bytes to be converted to binary representation - * @return {{value : Array., binLen : number}} Hash list where - * "value" contains the output number array and "binLen" is the binary - * length of "value" - */ - function bytes2binb(str) - { - var bin = [], codePnt, i, offset; - - for (i = 0; i < str.length; i += 1) - { - codePnt = str.charCodeAt(i); - - offset = i >>> 2; - if (bin.length <= offset) - { - bin.push(0); - } - bin[offset] |= codePnt << (24 - (8 * (i % 4))); - } - - return {"value" : bin, "binLen" : str.length * 8}; - } - - /** - * Convert a base-64 string to an array of big-endian words - * - * @private - * @param {string} str String to be converted to binary representation - * @return {{value : Array., binLen : number}} Hash list where - * "value" contains the output number array and "binLen" is the binary - * length of "value" - */ - function b642binb(str) - { - var retVal = [], byteCnt = 0, index, i, j, tmpInt, strPart, firstEqual, offset, - b64Tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - if (-1 === str.search(/^[a-zA-Z0-9=+\/]+$/)) - { - throw "Invalid character in base-64 string"; - } - firstEqual = str.indexOf('='); - str = str.replace(/\=/g, ''); - if ((-1 !== firstEqual) && (firstEqual < str.length)) - { - throw "Invalid '=' found in base-64 string"; - } - - for (i = 0; i < str.length; i += 4) - { - strPart = str.substr(i, 4); - tmpInt = 0; - - for (j = 0; j < strPart.length; j += 1) - { - index = b64Tab.indexOf(strPart[j]); - tmpInt |= index << (18 - (6 * j)); - } - - for (j = 0; j < strPart.length - 1; j += 1) - { - offset = byteCnt >>> 2; - while (retVal.length <= offset) - { - retVal.push(0); - } - retVal[offset] |= ((tmpInt >>> (16 - (j * 8))) & 0xFF) << - (24 - (8 * (byteCnt % 4))); - byteCnt += 1; - } - } - - return {"value" : retVal, "binLen" : byteCnt * 8}; - } - - /** - * Convert an array of big-endian words to a hex string. - * - * @private - * @param {Array.} binarray Array of integers to be converted to - * hexidecimal representation - * @param {{outputUpper : boolean, b64Pad : string}} formatOpts Hash list - * containing validated output formatting options - * @return {string} Hexidecimal representation of the parameter in string - * form - */ - function binb2hex(binarray, formatOpts) - { - var hex_tab = "0123456789abcdef", str = "", - length = binarray.length * 4, i, srcByte; - - for (i = 0; i < length; i += 1) - { - /* The below is more than a byte but it gets taken care of later */ - srcByte = binarray[i >>> 2] >>> ((3 - (i % 4)) * 8); - str += hex_tab.charAt((srcByte >>> 4) & 0xF) + - hex_tab.charAt(srcByte & 0xF); - } - - return (formatOpts["outputUpper"]) ? str.toUpperCase() : str; - } - - /** - * Convert an array of big-endian words to a base-64 string - * - * @private - * @param {Array.} binarray Array of integers to be converted to - * base-64 representation - * @param {{outputUpper : boolean, b64Pad : string}} formatOpts Hash list - * containing validated output formatting options - * @return {string} Base-64 encoded representation of the parameter in - * string form - */ - function binb2b64(binarray, formatOpts) - { - var str = "", length = binarray.length * 4, i, j, triplet, offset, int1, int2, - b64Tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - for (i = 0; i < length; i += 3) - { - offset = (i + 1) >>> 2; - int1 = (binarray.length <= offset) ? 0 : binarray[offset]; - offset = (i + 2) >>> 2; - int2 = (binarray.length <= offset) ? 0 : binarray[offset]; - triplet = (((binarray[i >>> 2] >>> 8 * (3 - i % 4)) & 0xFF) << 16) | - (((int1 >>> 8 * (3 - (i + 1) % 4)) & 0xFF) << 8) | - ((int2 >>> 8 * (3 - (i + 2) % 4)) & 0xFF); - for (j = 0; j < 4; j += 1) - { - if (i * 8 + j * 6 <= binarray.length * 32) - { - str += b64Tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F); - } - else - { - str += formatOpts["b64Pad"]; - } - } - } - return str; - } - - /** - * Convert an array of big-endian words to raw bytes string - * - * @private - * @param {Array.} binarray Array of integers to be converted to - * a raw bytes string representation - * @param {!Object} formatOpts Unused Hash list - * @return {string} Raw bytes representation of the parameter in string - * form - */ - function binb2bytes(binarray, formatOpts) - { - var str = "", length = binarray.length * 4, i, srcByte; - - for (i = 0; i < length; i += 1) - { - srcByte = (binarray[i >>> 2] >>> ((3 - (i % 4)) * 8)) & 0xFF; - str += String.fromCharCode(srcByte); - } - - return str; - } - - /** - * Validate hash list containing output formatting options, ensuring - * presence of every option or adding the default value - * - * @private - * @param {{outputUpper : boolean, b64Pad : string}|undefined} outputOpts - * Hash list of output formatting options - * @return {{outputUpper : boolean, b64Pad : string}} Validated hash list - * containing output formatting options - */ - function getOutputOpts(outputOpts) - { - var retVal = {"outputUpper" : false, "b64Pad" : "="}; - - try - { - if (outputOpts.hasOwnProperty("outputUpper")) - { - retVal["outputUpper"] = outputOpts["outputUpper"]; - } - - if (outputOpts.hasOwnProperty("b64Pad")) - { - retVal["b64Pad"] = outputOpts["b64Pad"]; - } - } - catch(ignore) - {} - - if ("boolean" !== typeof(retVal["outputUpper"])) - { - throw "Invalid outputUpper formatting option"; - } - - if ("string" !== typeof(retVal["b64Pad"])) - { - throw "Invalid b64Pad formatting option"; - } - - return retVal; - } - - /** - * The 32-bit implementation of circular rotate left - * - * @private - * @param {number} x The 32-bit integer argument - * @param {number} n The number of bits to shift - * @return {number} The x shifted circularly by n bits - */ - function rotl_32(x, n) - { - return (x << n) | (x >>> (32 - n)); - } - - /** - * The 32-bit implementation of circular rotate right - * - * @private - * @param {number} x The 32-bit integer argument - * @param {number} n The number of bits to shift - * @return {number} The x shifted circularly by n bits - */ - function rotr_32(x, n) - { - return (x >>> n) | (x << (32 - n)); - } - - /** - * The 64-bit implementation of circular rotate right - * - * @private - * @param {Int_64} x The 64-bit integer argument - * @param {number} n The number of bits to shift - * @return {Int_64} The x shifted circularly by n bits - */ - function rotr_64(x, n) - { - var retVal = null, tmp = new Int_64(x.highOrder, x.lowOrder); - - if (32 >= n) - { - retVal = new Int_64( - (tmp.highOrder >>> n) | ((tmp.lowOrder << (32 - n)) & 0xFFFFFFFF), - (tmp.lowOrder >>> n) | ((tmp.highOrder << (32 - n)) & 0xFFFFFFFF) - ); - } - else - { - retVal = new Int_64( - (tmp.lowOrder >>> (n - 32)) | ((tmp.highOrder << (64 - n)) & 0xFFFFFFFF), - (tmp.highOrder >>> (n - 32)) | ((tmp.lowOrder << (64 - n)) & 0xFFFFFFFF) - ); - } - - return retVal; - } - - /** - * The 32-bit implementation of shift right - * - * @private - * @param {number} x The 32-bit integer argument - * @param {number} n The number of bits to shift - * @return {number} The x shifted by n bits - */ - function shr_32(x, n) - { - return x >>> n; - } - - /** - * The 64-bit implementation of shift right - * - * @private - * @param {Int_64} x The 64-bit integer argument - * @param {number} n The number of bits to shift - * @return {Int_64} The x shifted by n bits - */ - function shr_64(x, n) - { - var retVal = null; - - if (32 >= n) - { - retVal = new Int_64( - x.highOrder >>> n, - x.lowOrder >>> n | ((x.highOrder << (32 - n)) & 0xFFFFFFFF) - ); - } - else - { - retVal = new Int_64( - 0, - x.highOrder >>> (n - 32) - ); - } - - return retVal; - } - - /** - * The 32-bit implementation of the NIST specified Parity function - * - * @private - * @param {number} x The first 32-bit integer argument - * @param {number} y The second 32-bit integer argument - * @param {number} z The third 32-bit integer argument - * @return {number} The NIST specified output of the function - */ - function parity_32(x, y, z) - { - return x ^ y ^ z; - } - - /** - * The 32-bit implementation of the NIST specified Ch function - * - * @private - * @param {number} x The first 32-bit integer argument - * @param {number} y The second 32-bit integer argument - * @param {number} z The third 32-bit integer argument - * @return {number} The NIST specified output of the function - */ - function ch_32(x, y, z) - { - return (x & y) ^ (~x & z); - } - - /** - * The 64-bit implementation of the NIST specified Ch function - * - * @private - * @param {Int_64} x The first 64-bit integer argument - * @param {Int_64} y The second 64-bit integer argument - * @param {Int_64} z The third 64-bit integer argument - * @return {Int_64} The NIST specified output of the function - */ - function ch_64(x, y, z) - { - return new Int_64( - (x.highOrder & y.highOrder) ^ (~x.highOrder & z.highOrder), - (x.lowOrder & y.lowOrder) ^ (~x.lowOrder & z.lowOrder) - ); - } - - /** - * The 32-bit implementation of the NIST specified Maj function - * - * @private - * @param {number} x The first 32-bit integer argument - * @param {number} y The second 32-bit integer argument - * @param {number} z The third 32-bit integer argument - * @return {number} The NIST specified output of the function - */ - function maj_32(x, y, z) - { - return (x & y) ^ (x & z) ^ (y & z); - } - - /** - * The 64-bit implementation of the NIST specified Maj function - * - * @private - * @param {Int_64} x The first 64-bit integer argument - * @param {Int_64} y The second 64-bit integer argument - * @param {Int_64} z The third 64-bit integer argument - * @return {Int_64} The NIST specified output of the function - */ - function maj_64(x, y, z) - { - return new Int_64( - (x.highOrder & y.highOrder) ^ - (x.highOrder & z.highOrder) ^ - (y.highOrder & z.highOrder), - (x.lowOrder & y.lowOrder) ^ - (x.lowOrder & z.lowOrder) ^ - (y.lowOrder & z.lowOrder) - ); - } - - /** - * The 32-bit implementation of the NIST specified Sigma0 function - * - * @private - * @param {number} x The 32-bit integer argument - * @return {number} The NIST specified output of the function - */ - function sigma0_32(x) - { - return rotr_32(x, 2) ^ rotr_32(x, 13) ^ rotr_32(x, 22); - } - - /** - * The 64-bit implementation of the NIST specified Sigma0 function - * - * @private - * @param {Int_64} x The 64-bit integer argument - * @return {Int_64} The NIST specified output of the function - */ - function sigma0_64(x) - { - var rotr28 = rotr_64(x, 28), rotr34 = rotr_64(x, 34), - rotr39 = rotr_64(x, 39); - - return new Int_64( - rotr28.highOrder ^ rotr34.highOrder ^ rotr39.highOrder, - rotr28.lowOrder ^ rotr34.lowOrder ^ rotr39.lowOrder); - } - - /** - * The 32-bit implementation of the NIST specified Sigma1 function - * - * @private - * @param {number} x The 32-bit integer argument - * @return {number} The NIST specified output of the function - */ - function sigma1_32(x) - { - return rotr_32(x, 6) ^ rotr_32(x, 11) ^ rotr_32(x, 25); - } - - /** - * The 64-bit implementation of the NIST specified Sigma1 function - * - * @private - * @param {Int_64} x The 64-bit integer argument - * @return {Int_64} The NIST specified output of the function - */ - function sigma1_64(x) - { - var rotr14 = rotr_64(x, 14), rotr18 = rotr_64(x, 18), - rotr41 = rotr_64(x, 41); - - return new Int_64( - rotr14.highOrder ^ rotr18.highOrder ^ rotr41.highOrder, - rotr14.lowOrder ^ rotr18.lowOrder ^ rotr41.lowOrder); - } - - /** - * The 32-bit implementation of the NIST specified Gamma0 function - * - * @private - * @param {number} x The 32-bit integer argument - * @return {number} The NIST specified output of the function - */ - function gamma0_32(x) - { - return rotr_32(x, 7) ^ rotr_32(x, 18) ^ shr_32(x, 3); - } - - /** - * The 64-bit implementation of the NIST specified Gamma0 function - * - * @private - * @param {Int_64} x The 64-bit integer argument - * @return {Int_64} The NIST specified output of the function - */ - function gamma0_64(x) - { - var rotr1 = rotr_64(x, 1), rotr8 = rotr_64(x, 8), shr7 = shr_64(x, 7); - - return new Int_64( - rotr1.highOrder ^ rotr8.highOrder ^ shr7.highOrder, - rotr1.lowOrder ^ rotr8.lowOrder ^ shr7.lowOrder - ); - } - - /** - * The 32-bit implementation of the NIST specified Gamma1 function - * - * @private - * @param {number} x The 32-bit integer argument - * @return {number} The NIST specified output of the function - */ - function gamma1_32(x) - { - return rotr_32(x, 17) ^ rotr_32(x, 19) ^ shr_32(x, 10); - } - - /** - * The 64-bit implementation of the NIST specified Gamma1 function - * - * @private - * @param {Int_64} x The 64-bit integer argument - * @return {Int_64} The NIST specified output of the function - */ - function gamma1_64(x) - { - var rotr19 = rotr_64(x, 19), rotr61 = rotr_64(x, 61), - shr6 = shr_64(x, 6); - - return new Int_64( - rotr19.highOrder ^ rotr61.highOrder ^ shr6.highOrder, - rotr19.lowOrder ^ rotr61.lowOrder ^ shr6.lowOrder - ); - } - - /** - * Add two 32-bit integers, wrapping at 2^32. This uses 16-bit operations - * internally to work around bugs in some JS interpreters. - * - * @private - * @param {number} a The first 32-bit integer argument to be added - * @param {number} b The second 32-bit integer argument to be added - * @return {number} The sum of a + b - */ - function safeAdd_32_2(a, b) - { - var lsw = (a & 0xFFFF) + (b & 0xFFFF), - msw = (a >>> 16) + (b >>> 16) + (lsw >>> 16); - - return ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF); - } - - /** - * Add four 32-bit integers, wrapping at 2^32. This uses 16-bit operations - * internally to work around bugs in some JS interpreters. - * - * @private - * @param {number} a The first 32-bit integer argument to be added - * @param {number} b The second 32-bit integer argument to be added - * @param {number} c The third 32-bit integer argument to be added - * @param {number} d The fourth 32-bit integer argument to be added - * @return {number} The sum of a + b + c + d - */ - function safeAdd_32_4(a, b, c, d) - { - var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF), - msw = (a >>> 16) + (b >>> 16) + (c >>> 16) + (d >>> 16) + - (lsw >>> 16); - - return ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF); - } - - /** - * Add five 32-bit integers, wrapping at 2^32. This uses 16-bit operations - * internally to work around bugs in some JS interpreters. - * - * @private - * @param {number} a The first 32-bit integer argument to be added - * @param {number} b The second 32-bit integer argument to be added - * @param {number} c The third 32-bit integer argument to be added - * @param {number} d The fourth 32-bit integer argument to be added - * @param {number} e The fifth 32-bit integer argument to be added - * @return {number} The sum of a + b + c + d + e - */ - function safeAdd_32_5(a, b, c, d, e) - { - var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF) + - (e & 0xFFFF), - msw = (a >>> 16) + (b >>> 16) + (c >>> 16) + (d >>> 16) + - (e >>> 16) + (lsw >>> 16); - - return ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF); - } - - /** - * Add two 64-bit integers, wrapping at 2^64. This uses 16-bit operations - * internally to work around bugs in some JS interpreters. - * - * @private - * @param {Int_64} x The first 64-bit integer argument to be added - * @param {Int_64} y The second 64-bit integer argument to be added - * @return {Int_64} The sum of x + y - */ - function safeAdd_64_2(x, y) - { - var lsw, msw, lowOrder, highOrder; - - lsw = (x.lowOrder & 0xFFFF) + (y.lowOrder & 0xFFFF); - msw = (x.lowOrder >>> 16) + (y.lowOrder >>> 16) + (lsw >>> 16); - lowOrder = ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF); - - lsw = (x.highOrder & 0xFFFF) + (y.highOrder & 0xFFFF) + (msw >>> 16); - msw = (x.highOrder >>> 16) + (y.highOrder >>> 16) + (lsw >>> 16); - highOrder = ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF); - - return new Int_64(highOrder, lowOrder); - } - - /** - * Add four 64-bit integers, wrapping at 2^64. This uses 16-bit operations - * internally to work around bugs in some JS interpreters. - * - * @private - * @param {Int_64} a The first 64-bit integer argument to be added - * @param {Int_64} b The second 64-bit integer argument to be added - * @param {Int_64} c The third 64-bit integer argument to be added - * @param {Int_64} d The fouth 64-bit integer argument to be added - * @return {Int_64} The sum of a + b + c + d - */ - function safeAdd_64_4(a, b, c, d) - { - var lsw, msw, lowOrder, highOrder; - - lsw = (a.lowOrder & 0xFFFF) + (b.lowOrder & 0xFFFF) + - (c.lowOrder & 0xFFFF) + (d.lowOrder & 0xFFFF); - msw = (a.lowOrder >>> 16) + (b.lowOrder >>> 16) + - (c.lowOrder >>> 16) + (d.lowOrder >>> 16) + (lsw >>> 16); - lowOrder = ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF); - - lsw = (a.highOrder & 0xFFFF) + (b.highOrder & 0xFFFF) + - (c.highOrder & 0xFFFF) + (d.highOrder & 0xFFFF) + (msw >>> 16); - msw = (a.highOrder >>> 16) + (b.highOrder >>> 16) + - (c.highOrder >>> 16) + (d.highOrder >>> 16) + (lsw >>> 16); - highOrder = ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF); - - return new Int_64(highOrder, lowOrder); - } - - /** - * Add five 64-bit integers, wrapping at 2^64. This uses 16-bit operations - * internally to work around bugs in some JS interpreters. - * - * @private - * @param {Int_64} a The first 64-bit integer argument to be added - * @param {Int_64} b The second 64-bit integer argument to be added - * @param {Int_64} c The third 64-bit integer argument to be added - * @param {Int_64} d The fouth 64-bit integer argument to be added - * @param {Int_64} e The fouth 64-bit integer argument to be added - * @return {Int_64} The sum of a + b + c + d + e - */ - function safeAdd_64_5(a, b, c, d, e) - { - var lsw, msw, lowOrder, highOrder; - - lsw = (a.lowOrder & 0xFFFF) + (b.lowOrder & 0xFFFF) + - (c.lowOrder & 0xFFFF) + (d.lowOrder & 0xFFFF) + - (e.lowOrder & 0xFFFF); - msw = (a.lowOrder >>> 16) + (b.lowOrder >>> 16) + - (c.lowOrder >>> 16) + (d.lowOrder >>> 16) + (e.lowOrder >>> 16) + - (lsw >>> 16); - lowOrder = ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF); - - lsw = (a.highOrder & 0xFFFF) + (b.highOrder & 0xFFFF) + - (c.highOrder & 0xFFFF) + (d.highOrder & 0xFFFF) + - (e.highOrder & 0xFFFF) + (msw >>> 16); - msw = (a.highOrder >>> 16) + (b.highOrder >>> 16) + - (c.highOrder >>> 16) + (d.highOrder >>> 16) + - (e.highOrder >>> 16) + (lsw >>> 16); - highOrder = ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF); - - return new Int_64(highOrder, lowOrder); - } - - /** - * Calculates the SHA-1 hash of the string set at instantiation - * - * @private - * @param {Array.} message The binary array representation of the - * string to hash - * @param {number} messageLen The number of bits in the message - * @return {Array.} The array of integers representing the SHA-1 - * hash of message - */ - function coreSHA1(message, messageLen) - { - var W = [], a, b, c, d, e, T, ch = ch_32, parity = parity_32, - maj = maj_32, rotl = rotl_32, safeAdd_2 = safeAdd_32_2, i, t, - safeAdd_5 = safeAdd_32_5, appendedMessageLength, offset, - H = [ - 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 - ]; - - offset = (((messageLen + 65) >>> 9) << 4) + 15; - while (message.length <= offset) - { - message.push(0); - } - /* Append '1' at the end of the binary string */ - message[messageLen >>> 5] |= 0x80 << (24 - (messageLen % 32)); - /* Append length of binary string in the position such that the new - length is a multiple of 512. Logic does not work for even multiples - of 512 but there can never be even multiples of 512 */ - message[offset] = messageLen; - - appendedMessageLength = message.length; - - for (i = 0; i < appendedMessageLength; i += 16) - { - a = H[0]; - b = H[1]; - c = H[2]; - d = H[3]; - e = H[4]; - - for (t = 0; t < 80; t += 1) - { - if (t < 16) - { - W[t] = message[t + i]; - } - else - { - W[t] = rotl(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1); - } - - if (t < 20) - { - T = safeAdd_5(rotl(a, 5), ch(b, c, d), e, 0x5a827999, W[t]); - } - else if (t < 40) - { - T = safeAdd_5(rotl(a, 5), parity(b, c, d), e, 0x6ed9eba1, W[t]); - } - else if (t < 60) - { - T = safeAdd_5(rotl(a, 5), maj(b, c, d), e, 0x8f1bbcdc, W[t]); - } else { - T = safeAdd_5(rotl(a, 5), parity(b, c, d), e, 0xca62c1d6, W[t]); - } - - e = d; - d = c; - c = rotl(b, 30); - b = a; - a = T; - } - - H[0] = safeAdd_2(a, H[0]); - H[1] = safeAdd_2(b, H[1]); - H[2] = safeAdd_2(c, H[2]); - H[3] = safeAdd_2(d, H[3]); - H[4] = safeAdd_2(e, H[4]); - } - - return H; - } - - /** - * Calculates the desired SHA-2 hash of the string set at instantiation - * - * @private - * @param {Array.} message The binary array representation of the - * string to hash - * @param {number} messageLen The number of bits in message - * @param {string} variant The desired SHA-2 variant - * @return {Array.} The array of integers representing the SHA-2 - * hash of message - */ - function coreSHA2(message, messageLen, variant) - { - var a, b, c, d, e, f, g, h, T1, T2, H, numRounds, lengthPosition, i, t, - binaryStringInc, binaryStringMult, safeAdd_2, safeAdd_4, safeAdd_5, - gamma0, gamma1, sigma0, sigma1, ch, maj, Int, W = [], int1, int2, offset, - appendedMessageLength, retVal, - K = [ - 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, - 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, - 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, - 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, - 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, - 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, - 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, - 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, - 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, - 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, - 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, - 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, - 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, - 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, - 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, - 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2 - ], - H_trunc = [ - 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, - 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 - ], - H_full = [ - 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, - 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 - ]; - - /* Set up the various function handles and variable for the specific - * variant */ - if ((variant === "SHA-224" || variant === "SHA-256") && - (2 & SUPPORTED_ALGS)) - { - /* 32-bit variant */ - numRounds = 64; - lengthPosition = (((messageLen + 65) >>> 9) << 4) + 15; - binaryStringInc = 16; - binaryStringMult = 1; - Int = Number; - safeAdd_2 = safeAdd_32_2; - safeAdd_4 = safeAdd_32_4; - safeAdd_5 = safeAdd_32_5; - gamma0 = gamma0_32; - gamma1 = gamma1_32; - sigma0 = sigma0_32; - sigma1 = sigma1_32; - maj = maj_32; - ch = ch_32; - - if ("SHA-224" === variant) - { - H = H_trunc; - } - else /* "SHA-256" === variant */ - { - H = H_full; - } - } - else if ((variant === "SHA-384" || variant === "SHA-512") && - (4 & SUPPORTED_ALGS)) - { - /* 64-bit variant */ - numRounds = 80; - lengthPosition = (((messageLen + 128) >>> 10) << 5) + 31; - binaryStringInc = 32; - binaryStringMult = 2; - Int = Int_64; - safeAdd_2 = safeAdd_64_2; - safeAdd_4 = safeAdd_64_4; - safeAdd_5 = safeAdd_64_5; - gamma0 = gamma0_64; - gamma1 = gamma1_64; - sigma0 = sigma0_64; - sigma1 = sigma1_64; - maj = maj_64; - ch = ch_64; - - K = [ - new Int(K[ 0], 0xd728ae22), new Int(K[ 1], 0x23ef65cd), - new Int(K[ 2], 0xec4d3b2f), new Int(K[ 3], 0x8189dbbc), - new Int(K[ 4], 0xf348b538), new Int(K[ 5], 0xb605d019), - new Int(K[ 6], 0xaf194f9b), new Int(K[ 7], 0xda6d8118), - new Int(K[ 8], 0xa3030242), new Int(K[ 9], 0x45706fbe), - new Int(K[10], 0x4ee4b28c), new Int(K[11], 0xd5ffb4e2), - new Int(K[12], 0xf27b896f), new Int(K[13], 0x3b1696b1), - new Int(K[14], 0x25c71235), new Int(K[15], 0xcf692694), - new Int(K[16], 0x9ef14ad2), new Int(K[17], 0x384f25e3), - new Int(K[18], 0x8b8cd5b5), new Int(K[19], 0x77ac9c65), - new Int(K[20], 0x592b0275), new Int(K[21], 0x6ea6e483), - new Int(K[22], 0xbd41fbd4), new Int(K[23], 0x831153b5), - new Int(K[24], 0xee66dfab), new Int(K[25], 0x2db43210), - new Int(K[26], 0x98fb213f), new Int(K[27], 0xbeef0ee4), - new Int(K[28], 0x3da88fc2), new Int(K[29], 0x930aa725), - new Int(K[30], 0xe003826f), new Int(K[31], 0x0a0e6e70), - new Int(K[32], 0x46d22ffc), new Int(K[33], 0x5c26c926), - new Int(K[34], 0x5ac42aed), new Int(K[35], 0x9d95b3df), - new Int(K[36], 0x8baf63de), new Int(K[37], 0x3c77b2a8), - new Int(K[38], 0x47edaee6), new Int(K[39], 0x1482353b), - new Int(K[40], 0x4cf10364), new Int(K[41], 0xbc423001), - new Int(K[42], 0xd0f89791), new Int(K[43], 0x0654be30), - new Int(K[44], 0xd6ef5218), new Int(K[45], 0x5565a910), - new Int(K[46], 0x5771202a), new Int(K[47], 0x32bbd1b8), - new Int(K[48], 0xb8d2d0c8), new Int(K[49], 0x5141ab53), - new Int(K[50], 0xdf8eeb99), new Int(K[51], 0xe19b48a8), - new Int(K[52], 0xc5c95a63), new Int(K[53], 0xe3418acb), - new Int(K[54], 0x7763e373), new Int(K[55], 0xd6b2b8a3), - new Int(K[56], 0x5defb2fc), new Int(K[57], 0x43172f60), - new Int(K[58], 0xa1f0ab72), new Int(K[59], 0x1a6439ec), - new Int(K[60], 0x23631e28), new Int(K[61], 0xde82bde9), - new Int(K[62], 0xb2c67915), new Int(K[63], 0xe372532b), - new Int(0xca273ece, 0xea26619c), new Int(0xd186b8c7, 0x21c0c207), - new Int(0xeada7dd6, 0xcde0eb1e), new Int(0xf57d4f7f, 0xee6ed178), - new Int(0x06f067aa, 0x72176fba), new Int(0x0a637dc5, 0xa2c898a6), - new Int(0x113f9804, 0xbef90dae), new Int(0x1b710b35, 0x131c471b), - new Int(0x28db77f5, 0x23047d84), new Int(0x32caab7b, 0x40c72493), - new Int(0x3c9ebe0a, 0x15c9bebc), new Int(0x431d67c4, 0x9c100d4c), - new Int(0x4cc5d4be, 0xcb3e42b6), new Int(0x597f299c, 0xfc657e2a), - new Int(0x5fcb6fab, 0x3ad6faec), new Int(0x6c44198c, 0x4a475817) - ]; - - if ("SHA-384" === variant) - { - H = [ - new Int(0xcbbb9d5d, H_trunc[0]), new Int(0x0629a292a, H_trunc[1]), - new Int(0x9159015a, H_trunc[2]), new Int(0x0152fecd8, H_trunc[3]), - new Int(0x67332667, H_trunc[4]), new Int(0x98eb44a87, H_trunc[5]), - new Int(0xdb0c2e0d, H_trunc[6]), new Int(0x047b5481d, H_trunc[7]) - ]; - } - else /* "SHA-512" === variant */ - { - H = [ - new Int(H_full[0], 0xf3bcc908), new Int(H_full[1], 0x84caa73b), - new Int(H_full[2], 0xfe94f82b), new Int(H_full[3], 0x5f1d36f1), - new Int(H_full[4], 0xade682d1), new Int(H_full[5], 0x2b3e6c1f), - new Int(H_full[6], 0xfb41bd6b), new Int(H_full[7], 0x137e2179) - ]; - } - } - else - { - throw "Unexpected error in SHA-2 implementation"; - } - - while (message.length <= lengthPosition) - { - message.push(0); - } - /* Append '1' at the end of the binary string */ - message[messageLen >>> 5] |= 0x80 << (24 - messageLen % 32); - /* Append length of binary string in the position such that the new - * length is correct */ - message[lengthPosition] = messageLen; - - appendedMessageLength = message.length; - - for (i = 0; i < appendedMessageLength; i += binaryStringInc) - { - a = H[0]; - b = H[1]; - c = H[2]; - d = H[3]; - e = H[4]; - f = H[5]; - g = H[6]; - h = H[7]; - - for (t = 0; t < numRounds; t += 1) - { - if (t < 16) - { - offset = t * binaryStringMult + i; - int1 = (message.length <= offset) ? 0 : message[offset]; - int2 = (message.length <= offset + 1) ? 0 : message[offset + 1]; - /* Bit of a hack - for 32-bit, the second term is ignored */ - W[t] = new Int(int1, int2); - } - else - { - W[t] = safeAdd_4( - gamma1(W[t - 2]), W[t - 7], - gamma0(W[t - 15]), W[t - 16] - ); - } - - T1 = safeAdd_5(h, sigma1(e), ch(e, f, g), K[t], W[t]); - T2 = safeAdd_2(sigma0(a), maj(a, b, c)); - h = g; - g = f; - f = e; - e = safeAdd_2(d, T1); - d = c; - c = b; - b = a; - a = safeAdd_2(T1, T2); - - } - - H[0] = safeAdd_2(a, H[0]); - H[1] = safeAdd_2(b, H[1]); - H[2] = safeAdd_2(c, H[2]); - H[3] = safeAdd_2(d, H[3]); - H[4] = safeAdd_2(e, H[4]); - H[5] = safeAdd_2(f, H[5]); - H[6] = safeAdd_2(g, H[6]); - H[7] = safeAdd_2(h, H[7]); - } - - if (("SHA-224" === variant) && (2 & SUPPORTED_ALGS)) - { - retVal = [ - H[0], H[1], H[2], H[3], - H[4], H[5], H[6] - ]; - } - else if (("SHA-256" === variant) && (2 & SUPPORTED_ALGS)) - { - retVal = H; - } - else if (("SHA-384" === variant) && (4 & SUPPORTED_ALGS)) - { - retVal = [ - H[0].highOrder, H[0].lowOrder, - H[1].highOrder, H[1].lowOrder, - H[2].highOrder, H[2].lowOrder, - H[3].highOrder, H[3].lowOrder, - H[4].highOrder, H[4].lowOrder, - H[5].highOrder, H[5].lowOrder - ]; - } - else if (("SHA-512" === variant) && (4 & SUPPORTED_ALGS)) - { - retVal = [ - H[0].highOrder, H[0].lowOrder, - H[1].highOrder, H[1].lowOrder, - H[2].highOrder, H[2].lowOrder, - H[3].highOrder, H[3].lowOrder, - H[4].highOrder, H[4].lowOrder, - H[5].highOrder, H[5].lowOrder, - H[6].highOrder, H[6].lowOrder, - H[7].highOrder, H[7].lowOrder - ]; - } - else /* This should never be reached */ - { - throw "Unexpected error in SHA-2 implementation"; - } - - return retVal; - } - - /** - * jsSHA is the workhorse of the library. Instantiate it with the string to - * be hashed as the parameter - * - * @constructor - * @this {jsSHA} - * @param {string} srcString The string to be hashed - * @param {string} inputFormat The format of srcString, HEX, TEXT, B64, or BYTES - * @param {string=} encoding The text encoding to use to encode the source - * string - */ - var jsSHA = function(srcString, inputFormat, encoding) - { - var strBinLen = 0, strToHash = [0], utfType = '', srcConvertRet = null; - - utfType = encoding || "UTF8"; - - if (!(("UTF8" === utfType) || ("UTF16" === utfType))) - { - throw "encoding must be UTF8 or UTF16"; - } - - /* Convert the input string into the correct type */ - if ("HEX" === inputFormat) - { - if (0 !== (srcString.length % 2)) - { - throw "srcString of HEX type must be in byte increments"; - } - srcConvertRet = hex2binb(srcString); - strBinLen = srcConvertRet["binLen"]; - strToHash = srcConvertRet["value"]; - } - else if ("TEXT" === inputFormat) - { - srcConvertRet = str2binb(srcString, utfType); - strBinLen = srcConvertRet["binLen"]; - strToHash = srcConvertRet["value"]; - } - else if ("B64" === inputFormat) - { - srcConvertRet = b642binb(srcString); - strBinLen = srcConvertRet["binLen"]; - strToHash = srcConvertRet["value"]; - } - else if ("BYTES" === inputFormat) - { - srcConvertRet = bytes2binb(srcString); - strBinLen = srcConvertRet["binLen"]; - strToHash = srcConvertRet["value"]; - } - else - { - throw "inputFormat must be HEX, TEXT, B64, or BYTES"; - } - - /** - * Returns the desired SHA hash of the string specified at instantiation - * using the specified parameters - * - * @expose - * @param {string} variant The desired SHA variant (SHA-1, SHA-224, - * SHA-256, SHA-384, or SHA-512) - * @param {string} format The desired output formatting (B64, HEX, or BYTES) - * @param {number=} numRounds The number of rounds of hashing to be - * executed - * @param {{outputUpper : boolean, b64Pad : string}=} outputFormatOpts - * Hash list of output formatting options - * @return {string} The string representation of the hash in the format - * specified - */ - this.getHash = function(variant, format, numRounds, outputFormatOpts) - { - var formatFunc = null, message = strToHash.slice(), - messageBinLen = strBinLen, i; - - /* Need to do argument patching since both numRounds and - outputFormatOpts are optional */ - if (3 === arguments.length) - { - if ("number" !== typeof numRounds) - { - outputFormatOpts = numRounds; - numRounds = 1; - } - } - else if (2 === arguments.length) - { - numRounds = 1; - } - - /* Validate the numRounds argument */ - if ((numRounds !== parseInt(numRounds, 10)) || (1 > numRounds)) - { - throw "numRounds must a integer >= 1"; - } - - /* Validate the output format selection */ - switch (format) - { - case "HEX": - formatFunc = binb2hex; - break; - case "B64": - formatFunc = binb2b64; - break; - case "BYTES": - formatFunc = binb2bytes; - break; - default: - throw "format must be HEX, B64, or BYTES"; - } - - if (("SHA-1" === variant) && (1 & SUPPORTED_ALGS)) - { - for (i = 0; i < numRounds; i += 1) - { - message = coreSHA1(message, messageBinLen); - messageBinLen = 160; - } - } - else if (("SHA-224" === variant) && (2 & SUPPORTED_ALGS)) - { - for (i = 0; i < numRounds; i += 1) - { - message = coreSHA2(message, messageBinLen, variant); - messageBinLen = 224; - } - } - else if (("SHA-256" === variant) && (2 & SUPPORTED_ALGS)) - { - for (i = 0; i < numRounds; i += 1) - { - message = coreSHA2(message, messageBinLen, variant); - messageBinLen = 256; - } - } - else if (("SHA-384" === variant) && (4 & SUPPORTED_ALGS)) - { - for (i = 0; i < numRounds; i += 1) - { - message = coreSHA2(message, messageBinLen, variant); - messageBinLen = 384; - } - } - else if (("SHA-512" === variant) && (4 & SUPPORTED_ALGS)) - { - for (i = 0; i < numRounds; i += 1) - { - message = coreSHA2(message, messageBinLen, variant); - messageBinLen = 512; - } - } - else - { - throw "Chosen SHA variant is not supported"; - } - - return formatFunc(message, getOutputOpts(outputFormatOpts)); - }; - - /** - * Returns the desired HMAC of the string specified at instantiation - * using the key and variant parameter - * - * @expose - * @param {string} key The key used to calculate the HMAC - * @param {string} inputFormat The format of key, HEX, TEXT, B64, or BYTES - * @param {string} variant The desired SHA variant (SHA-1, SHA-224, - * SHA-256, SHA-384, or SHA-512) - * @param {string} outputFormat The desired output formatting - * (B64, HEX, or BYTES) - * @param {{outputUpper : boolean, b64Pad : string}=} outputFormatOpts - * associative array of output formatting options - * @return {string} The string representation of the hash in the format - * specified - */ - this.getHMAC = function(key, inputFormat, variant, outputFormat, - outputFormatOpts) - { - var formatFunc, keyToUse, blockByteSize, blockBitSize, i, - retVal, lastArrayIndex, keyBinLen, hashBitSize, - keyWithIPad = [], keyWithOPad = [], keyConvertRet = null; - - /* Validate the output format selection */ - switch (outputFormat) - { - case "HEX": - formatFunc = binb2hex; - break; - case "B64": - formatFunc = binb2b64; - break; - case "BYTES": - formatFunc = binb2bytes; - break; - default: - throw "outputFormat must be HEX, B64, or BYTES"; - } - - /* Validate the hash variant selection and set needed variables */ - if (("SHA-1" === variant) && (1 & SUPPORTED_ALGS)) - { - blockByteSize = 64; - hashBitSize = 160; - } - else if (("SHA-224" === variant) && (2 & SUPPORTED_ALGS)) - { - blockByteSize = 64; - hashBitSize = 224; - } - else if (("SHA-256" === variant) && (2 & SUPPORTED_ALGS)) - { - blockByteSize = 64; - hashBitSize = 256; - } - else if (("SHA-384" === variant) && (4 & SUPPORTED_ALGS)) - { - blockByteSize = 128; - hashBitSize = 384; - } - else if (("SHA-512" === variant) && (4 & SUPPORTED_ALGS)) - { - blockByteSize = 128; - hashBitSize = 512; - } - else - { - throw "Chosen SHA variant is not supported"; - } - - /* Validate input format selection */ - if ("HEX" === inputFormat) - { - keyConvertRet = hex2binb(key); - keyBinLen = keyConvertRet["binLen"]; - keyToUse = keyConvertRet["value"]; - } - else if ("TEXT" === inputFormat) - { - keyConvertRet = str2binb(key, utfType); - keyBinLen = keyConvertRet["binLen"]; - keyToUse = keyConvertRet["value"]; - } - else if ("B64" === inputFormat) - { - keyConvertRet = b642binb(key); - keyBinLen = keyConvertRet["binLen"]; - keyToUse = keyConvertRet["value"]; - } - else if ("BYTES" === inputFormat) - { - keyConvertRet = bytes2binb(key); - keyBinLen = keyConvertRet["binLen"]; - keyToUse = keyConvertRet["value"]; - } - else - { - throw "inputFormat must be HEX, TEXT, B64, or BYTES"; - } - - /* These are used multiple times, calculate and store them */ - blockBitSize = blockByteSize * 8; - lastArrayIndex = (blockByteSize / 4) - 1; - - /* Figure out what to do with the key based on its size relative to - * the hash's block size */ - if (blockByteSize < (keyBinLen / 8)) - { - if (("SHA-1" === variant) && (1 & SUPPORTED_ALGS)) - { - keyToUse = coreSHA1(keyToUse, keyBinLen); - } - else if (6 & SUPPORTED_ALGS) - { - keyToUse = coreSHA2(keyToUse, keyBinLen, variant); - } - else - { - throw "Unexpected error in HMAC implementation"; - } - /* For all variants, the block size is bigger than the output - * size so there will never be a useful byte at the end of the - * string */ - while (keyToUse.length <= lastArrayIndex) - { - keyToUse.push(0); - } - keyToUse[lastArrayIndex] &= 0xFFFFFF00; - } - else if (blockByteSize > (keyBinLen / 8)) - { - /* If the blockByteSize is greater than the key length, there - * will always be at LEAST one "useless" byte at the end of the - * string */ - while (keyToUse.length <= lastArrayIndex) - { - keyToUse.push(0); - } - keyToUse[lastArrayIndex] &= 0xFFFFFF00; - } - - /* Create ipad and opad */ - for (i = 0; i <= lastArrayIndex; i += 1) - { - keyWithIPad[i] = keyToUse[i] ^ 0x36363636; - keyWithOPad[i] = keyToUse[i] ^ 0x5C5C5C5C; - } - - /* Calculate the HMAC */ - if (("SHA-1" === variant) && (1 & SUPPORTED_ALGS)) - { - retVal = coreSHA1( - keyWithOPad.concat( - coreSHA1( - keyWithIPad.concat(strToHash), - blockBitSize + strBinLen - ) - ), - blockBitSize + hashBitSize); - } - else if (6 & SUPPORTED_ALGS) - { - retVal = coreSHA2( - keyWithOPad.concat( - coreSHA2( - keyWithIPad.concat(strToHash), - blockBitSize + strBinLen, - variant - ) - ), - blockBitSize + hashBitSize, variant); - } - else - { - throw "Unexpected error in HMAC implementation"; - } - - return formatFunc(retVal, getOutputOpts(outputFormatOpts)); - }; - }; - - if (("function" === typeof define) && (define["amd"])) /* AMD Support */ - { - define(function() - { - return jsSHA; - }); - } else if ("undefined" !== typeof exports) /* Node Support */ - { - if (("undefined" !== typeof module) && module["exports"]) - { - module["exports"] = exports = jsSHA; - } - else { - exports = jsSHA; - } - } else { /* Browsers and Web Workers*/ - global["jsSHA"] = jsSHA; - } -}(this)); diff --git a/lib/sha/package.json b/modules/jsSHA/.bower.json similarity index 51% rename from lib/sha/package.json rename to modules/jsSHA/.bower.json index cd344167..5c1e78dd 100644 --- a/lib/sha/package.json +++ b/modules/jsSHA/.bower.json @@ -1,6 +1,6 @@ { - "name": "jssha", - "version": "2.0.0", + "name": "jsSHA", + "version": "1.5.0", "description": "jsSHA is a JavaScript implementation of the entire family of SHA hashes as defined in FIPS 180-2 (SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512) as well as HMAC", "main": "src/sha.js", "repository": { @@ -23,17 +23,23 @@ "hash" ], "license": "BSD", - "author": "Brian Turek ", - "bugs": { - "url": "https://github.com/Caligatio/jsSHA/issues" + "authors": [ + "Brian Turek " + ], + "homepage": "http://caligatio.github.com/jsSHA/", + "ignore": [ + "build", + "test", + "src/sha_dev.js" + ], + "_release": "1.5.0", + "_resolution": { + "type": "version", + "tag": "v1.5.0", + "commit": "c65d3e2d3e548c38774b81b463117c114238399e" }, - "engines": { - "node": "*" - }, - "homepage": "https://github.com/Caligatio/jsSHA", - "dependencies": {}, - "devDependencies": {}, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - } -} + "_source": "git://github.com/Caligatio/jsSHA.git", + "_target": "~1.5.0", + "_originalSource": "jsSHA", + "_direct": true +} \ No newline at end of file diff --git a/modules/jsSHA/.npmignore b/modules/jsSHA/.npmignore new file mode 100755 index 00000000..3c65cf5e --- /dev/null +++ b/modules/jsSHA/.npmignore @@ -0,0 +1,6 @@ +build +test +src/sha_dev.js +src/sha1.js +src/sha256.js +src/sha512.js diff --git a/modules/jsSHA/CHANGELOG b/modules/jsSHA/CHANGELOG new file mode 100755 index 00000000..5230296b --- /dev/null +++ b/modules/jsSHA/CHANGELOG @@ -0,0 +1,99 @@ +------------------------- + jsSHA - ChangeLog +------------------------- + +1.5 (2013-12-15) +========================= +- Added optional numRounds argument to getHash + - Note: this necessitated removing the hash result caching functionality +- Reduced file size by optimizing internal constants +- Removed charSize input and replaced with encoding to handle Unicode. NOTE: + Only Code points up to 0xFFFF are supported. + - charSize = 16 is effectively replaced by encoding = "UTF16" + - charSize = 8 was wrong in terms of handling UTF-8 and has been replaced by + encoding = "UTF8" +- Changed method of referencing "window" to be compatible with WebWorkers, + Node.js, and AMD (thanks piranna!) + +1.42 (2012-12-28) +========================= +- Readded v1.4 Safari patch to support older versions + +1.41 (2012-12-23) +========================= +- Fixed incorrect hash issue with Chrome x64 v25 (Dev channel), also provides + stable patch to v1.4 Safari issue. + +1.4 (2012-12-08) +========================= +- Added new input type, TEXT, that is functionally identical to ASCII* +- Added new input type, B64, for base-64 encoded strings +- Added new input and output formatting parameters + - getHash and getHMAC take an optional parameter, outputFormatOpts, + that is a hash list containing the keys "outputUpper" (boolean, only + applicable to HEX output) and "b64Pad" (string, only applicable to Base-64 + output) that have default values of false and "=", respectively + - jsSHA constructor takes an optional parameter, charSize (8 or 16) that + specifies the character width of the input (TEXT and ASCII input only) +- Modified comments to be Google Closure Compiler compliant +- Added a SUPPORTED_ALGS flag that, when used with the Google Closure Compiler, + will remove unused functions/function portions + - Removed all src/*_nice.js files as the SUPPORTED_ALGS flag renders them + obsolete +- All production-ready files are now produced using the Google Closure Compiler + with ADVANCED_OPTIMIZATIONS resulting in further reduced filesizes +- The SHA-1 only implementation now requires that that "SHA-1" be specified as + the variant when using getHash and getHMAC +- Removed test/HMAC.py as new NIST tests made the need for it obsolete +- Significantly changed the test/test.html to make it easier to understand and + to allow for easier adding of test cases +- Replaced previous error returning code with thrown exceptions +- Fix for 64-bit Safari issue (thanks Ron Garret and Chris Warren-Smith!) + - NOTE: While this fix works, it is merely a workaround for a WebKit JavaScript + optimizer bug, see https://bugs.webkit.org/show_bug.cgi?id=88673 for more detail + +* This library misused the term ASCII so input type of TEXT was added with the + intention of deprecating ASCII + +1.31 (2012-07-21) +========================= +- Updated project URL to point to new GitHub repository +- Added a compressed version of sha.js + +1.3 (2010-09-01) +========================= +- Changed method of declaring objects/classes +- Moved non-instance specific variables and methods to class scope +- Removed logically correct but unneeded conditionals + +1.2 (2009-07-22) +========================= +- Added the HMAC algorithm for all supported hashes (using both ASCII and hex + keys) +- As a result of adding HMAC, added support for hash input text to be hex + (ASCII representation of hex) +- Added multiple variants of safeAdd functions, resulting in a significant + performance gain +- Removed wrapper.js file +- Used a different JavaScript compressor resulting in smaller file sizes + +1.11 (2008-12-07) +========================= +- Fixed a base-64 encoding issue resulting from a missing capital 'X' + +1.1 (2008-09-25) +========================= +- Fixed an issue with incorrect hashes being generated when jsSHA ojbects were + used to generate multiple hashes + +1.0 (2008-09-25) +========================= +- Made all functions/variables follow an object-orientated methodology +- Removed support for string hash output as the hash is rarely ASCII friendly +- Changed the interface to calculate hashes (see README) +- Made sha.js validate against JSLint (http://www.jslint.com/) using + "Recommended" settings + +0.1 (2008-02-21) +========================= +- Initial public release diff --git a/modules/jsSHA/LICENSE b/modules/jsSHA/LICENSE new file mode 100755 index 00000000..a225b1c5 --- /dev/null +++ b/modules/jsSHA/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2008-2013, Brian Turek +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * The names of the contributors may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING,BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCEOR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISEDOF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/modules/jsSHA/README.md b/modules/jsSHA/README.md new file mode 100755 index 00000000..8831c4ed --- /dev/null +++ b/modules/jsSHA/README.md @@ -0,0 +1,121 @@ +# jsSHA +A JavaScript implementation of the complete Secure Hash Standard family + (SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512) as well as HMAC by + Brian Turek + +## About +jsSHA is a javaScript implementation of the complete Secure Hash Algorithm +family as defined by FIPS PUB 180-2 +(http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf). + +It also includes the HMAC algorithm with SHA support as defined by FIPS PUB 198-1 +(http://csrc.nist.gov/publications/fips/fips198-1/FIPS-198-1_final.pdf) + +With the slow phasing out of MD5 as the standard hash to use in web +applications, a client-side implementation of the complete Secure Hash Standard +family was needed. Due to SHA-384 and SHA-512's use of 64-bit values throughout +the algorithm, JavaScript can not easily natively support the calculation of +these hashes. As a result, a bit of hacking had to be done to make sure the +values behaved themselves. SHA-224 was added to the Secure Hash Standard family +on 25 February 2004 so it was also included in this package. + +## Files +**src/sha_dev.js** + +A commented implementation of the entire SHA family of hashes. Not to be used +in production. + +**src/sha.js** + +A Google Closure Compiler optimized version of the entire library + +**src/sha1.js** + +A Google Closure Compiler optimized version the library with non SHA-1 +functionality removed + +**src/sha256.js** + +A Google Closure Compiler optimized version the library with non SHA-224/SHA-256 +functionality removed + +**src/sha512.js** + +A Google Closure Compiler optimized version the library with non SHA-384/SHA-512 +functionality removed + +**test/test.html** + +A test page that calculates various hashes and has their correct values + +**build/make-release** + +A Bash script that runs the various Google Closure Compiler commands to build +a release + +**build/externs.js** +File needed solely to make the Google Closure Compilter work + +## Usage + +### Browser +Include the desired JavaScript file (sha.js, sha1.js, sha256.js, or sha512.js) +in your header (sha.js used below): + + + +Instantiate a new jsSHA object with your string to be hashed and its format +(HEX or TEXT) as the parameters. Then, call getHash with the desired hash +variant (SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512) and output type +(HEX or B64). + +In the example below, "This is a Test" and "SHA-512" were used +as the string to be hashed and variant respectively. Also, the HMAC using TEXT +key "SecretKey" and hashing algorithm SHA-512 was calculated. + + var shaObj = new jsSHA("This is a Test", "TEXT"); + var hash = shaObj.getHash("SHA-512", "HEX"); + var hmac = shaObj.getHMAC("SecretKey", "TEXT", "SHA-512", "HEX"); + +The constructor takes an optional parameter, encoding, that specifies the +encoding used to encode TEXT-type inputs. Valid options are "UTF8" and "UTF16" +and it defaults to "UTF8" + +getHash takes two optional parameters, a numRounds integer and an outputFormatOpts +hashlist. numRounds controls the number of hashing iterations/rounds performed +and defaults to a value of "1" if not specified. outputFormatOpts dictates +some formatting options for the output. By default, +`outputFormatOpts = {"outputUpper" : false, "b64Pad" : "="}`. These +options are intelligently interpreted based upon the chosen output format. + +getHMAC also takes an optional outputFormatOpts hashlist which operates the exact +same way as above. + +### Node.js +jsSHA is available through NPM and be installed by simply doing + + npm install jsSHA +To use the module, first require it using: + + jsSHA = require("jsSHA"); + +The rest of the instructions are identical to the [Browser](#browser) section above. + +## Compiling +This library makes use of the Google Closure Compiler +(https://developers.google.com/closure/compiler) to both boost performance +and reduce filesizes. To compile sha_dev.js into a customized output file, use +a command like the following: + + java -jar compiler.jar --define="SUPPORTED_ALGS=" \ + --externs /path/to/build/externs.js --warning_level VERBOSE \ + --compilation_level ADVANCED_OPTIMIZATIONS \ + --js /path/to/sha_dev.js --js_output_file /path/to/sha.js + +where is a bitwise OR of the following values: + - 4 for SHA-384/SHA-512 + - 2 for SHA-224/256 + - 1 for SHA-1 + +##Contact Info +The project's website is located at [http://caligatio.github.com/jsSHA/](http://caligatio.github.com/jsSHA/) diff --git a/modules/jsSHA/bower.json b/modules/jsSHA/bower.json new file mode 100755 index 00000000..4f06e020 --- /dev/null +++ b/modules/jsSHA/bower.json @@ -0,0 +1,35 @@ +{ + "name" : "jsSHA", + "version" : "1.5.0", + "description" : "jsSHA is a JavaScript implementation of the entire family of SHA hashes as defined in FIPS 180-2 (SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512) as well as HMAC", + "main" : "src/sha.js", + "repository" : { + "type" : "git", + "url" : "https://github.com/Caligatio/jsSHA.git" + }, + "keywords" : [ + "SHA-1", + "SHA-256", + "SHA-224", + "SHA-384", + "SHA-512", + "SHA1", + "SHA256", + "SHA224", + "SHA384", + "SHA512", + "SHA2", + "HMAC", + "hash" + ], + "license" : "BSD", + "authors" : [ + "Brian Turek " + ], + "homepage" : "http://caligatio.github.com/jsSHA/", + "ignore": [ + "build", + "test", + "src/sha_dev.js" + ] +} \ No newline at end of file diff --git a/modules/jsSHA/package.json b/modules/jsSHA/package.json new file mode 100755 index 00000000..313d0ccc --- /dev/null +++ b/modules/jsSHA/package.json @@ -0,0 +1,33 @@ +{ + "name" : "jsSHA", + "version" : "1.5.0", + "description" : "jsSHA is a JavaScript implementation of the entire family of SHA hashes as defined in FIPS 180-2 (SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512) as well as HMAC", + "main" : "src/sha.js", + "repository" : { + "type" : "git", + "url" : "https://github.com/Caligatio/jsSHA.git" + }, + "keywords" : [ + "SHA-1", + "SHA-256", + "SHA-224", + "SHA-384", + "SHA-512", + "SHA1", + "SHA256", + "SHA224", + "SHA384", + "SHA512", + "SHA2", + "HMAC", + "hash" + ], + "license" : "BSD", + "author" : "Brian Turek ", + "bugs" : { + "url": "https://github.com/Caligatio/jsSHA/issues" + }, + "engines" : { + "node" : "*" + } +} \ No newline at end of file diff --git a/modules/jsSHA/src/sha.js b/modules/jsSHA/src/sha.js new file mode 100755 index 00000000..27fa59e6 --- /dev/null +++ b/modules/jsSHA/src/sha.js @@ -0,0 +1,34 @@ +/* + A JavaScript implementation of the SHA family of hashes, as + defined in FIPS PUB 180-2 as well as the corresponding HMAC implementation + as defined in FIPS PUB 198a + + Copyright Brian Turek 2008-2013 + Distributed under the BSD License + See http://caligatio.github.com/jsSHA/ for more information + + Several functions taken from Paul Johnston +*/ +(function(T){function z(a,c,b){var g=0,f=[0],h="",l=null,h=b||"UTF8";if("UTF8"!==h&&"UTF16"!==h)throw"encoding must be UTF8 or UTF16";if("HEX"===c){if(0!==a.length%2)throw"srcString of HEX type must be in byte increments";l=B(a);g=l.binLen;f=l.value}else if("ASCII"===c||"TEXT"===c)l=J(a,h),g=l.binLen,f=l.value;else if("B64"===c)l=K(a),g=l.binLen,f=l.value;else throw"inputFormat must be HEX, TEXT, ASCII, or B64";this.getHash=function(a,c,b,h){var l=null,d=f.slice(),n=g,p;3===arguments.length?"number"!== +typeof b&&(h=b,b=1):2===arguments.length&&(b=1);if(b!==parseInt(b,10)||1>b)throw"numRounds must a integer >= 1";switch(c){case "HEX":l=L;break;case "B64":l=M;break;default:throw"format must be HEX or B64";}if("SHA-1"===a)for(p=0;pp/8&&(d[b]&=4294967040);for(n=0;n<=b;n+=1)w[n]=d[n]^909522486,x[n]=d[n]^1549556828;c="SHA-1"===c?y(x.concat(y(w.concat(f),a+g)),a+m):v(x.concat(v(w.concat(f),a+g,c)),a+m,c);return l(c,N(s))}}function s(a,c){this.a=a;this.b=c}function J(a,c){var b=[],g,f=[],h=0,l;if("UTF8"===c)for(l=0;l>>12,f[1]=128|(g&4032)>>>6,f[2]=128|g&63):128>>6,f[1]=128|g&63):f[0]=g,g=0;g>>2]|=f[g]<<24-h%4*8,h+=1;else if("UTF16"===c)for(l=0;l>>2]|=a.charCodeAt(l)<<16-h%4*8,h+=2;return{value:b,binLen:8*h}}function B(a){var c=[],b=a.length,g,f;if(0!==b%2)throw"String of HEX type must be in byte increments";for(g=0;g>>3]|=f<<24-g%8*4}return{value:c, +binLen:4*b}}function K(a){var c=[],b=0,g,f,h,l,r;if(-1===a.search(/^[a-zA-Z0-9=+\/]+$/))throw"Invalid character in base-64 string";g=a.indexOf("=");a=a.replace(/\=/g,"");if(-1!==g&&g>2]|=(g>>>16-8*c&255)<<24-b%4*8,b+=1}return{value:d,binLen:8*b}}function x(a,d){var b="",f=4*a.length,e,c;for(e=0;e>>2]>>>8*(3-e%4),b+="0123456789abcdef".charAt(c>>>4&15)+"0123456789abcdef".charAt(c&15);return d.outputUpper?b.toUpperCase():b}function y(a,d){var b="",f=4*a.length,e,c,g;for(e=0;e>>2]>>>8*(3-e%4)&255)<<16|(a[e+1>>> +2]>>>8*(3-(e+1)%4)&255)<<8|a[e+2>>>2]>>>8*(3-(e+2)%4)&255,c=0;4>c;c+=1)b=8*e+6*c<=32*a.length?b+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(g>>>6*(3-c)&63):b+d.b64Pad;return b}function z(a){var d={outputUpper:!1,b64Pad:"="};try{a.hasOwnProperty("outputUpper")&&(d.outputUpper=a.outputUpper),a.hasOwnProperty("b64Pad")&&(d.b64Pad=a.b64Pad)}catch(b){}if("boolean"!==typeof d.outputUpper)throw"Invalid outputUpper formatting option";if("string"!==typeof d.b64Pad)throw"Invalid b64Pad formatting option"; +return d}function B(a,d){return a<>>32-d}function C(a,d,b){return a^d^b}function D(a,d,b){return a&d^~a&b}function E(a,d,b){return a&d^a&b^d&b}function F(a,d){var b=(a&65535)+(d&65535);return((a>>>16)+(d>>>16)+(b>>>16)&65535)<<16|b&65535}function G(a,d,b,f,e){var c=(a&65535)+(d&65535)+(b&65535)+(f&65535)+(e&65535);return((a>>>16)+(d>>>16)+(b>>>16)+(f>>>16)+(e>>>16)+(c>>>16)&65535)<<16|c&65535}function s(a,d){var b=[],f,e,c,g,p,q,s=D,t=C,v=E,h=B,k=F,m,l,r=G,u,n=[1732584193,4023233417,2562383102, +271733878,3285377520];a[d>>>5]|=128<<24-d%32;a[(d+65>>>9<<4)+15]=d;u=a.length;for(m=0;ml;l+=1)b[l]=16>l?a[l+m]:h(b[l-3]^b[l-8]^b[l-14]^b[l-16],1),q=20>l?r(h(f,5),s(e,c,g),p,1518500249,b[l]):40>l?r(h(f,5),t(e,c,g),p,1859775393,b[l]):60>l?r(h(f,5),v(e,c,g),p,2400959708,b[l]):r(h(f,5),t(e,c,g),p,3395469782,b[l]),p=g,g=c,c=h(e,30),e=f,f=q;n[0]=k(f,n[0]);n[1]=k(e,n[1]);n[2]=k(c,n[2]);n[3]=k(g,n[3]);n[4]=k(p,n[4])}return n}"function"===typeof define&& +typeof define.amd?define(function(){return q}):"undefined"!==typeof exports?"undefined"!==typeof module&&module.exports?module.exports=exports=q:exports=q:A.jsSHA=q})(this); diff --git a/modules/jsSHA/src/sha256.js b/modules/jsSHA/src/sha256.js new file mode 100755 index 00000000..6a7ebbe7 --- /dev/null +++ b/modules/jsSHA/src/sha256.js @@ -0,0 +1,23 @@ +/* + A JavaScript implementation of the SHA family of hashes, as + defined in FIPS PUB 180-2 as well as the corresponding HMAC implementation + as defined in FIPS PUB 198a + + Copyright Brian Turek 2008-2013 + Distributed under the BSD License + See http://caligatio.github.com/jsSHA/ for more information + + Several functions taken from Paul Johnston +*/ +(function(B){function r(a,c,b){var f=0,e=[0],g="",h=null,g=b||"UTF8";if("UTF8"!==g&&"UTF16"!==g)throw"encoding must be UTF8 or UTF16";if("HEX"===c){if(0!==a.length%2)throw"srcString of HEX type must be in byte increments";h=u(a);f=h.binLen;e=h.value}else if("ASCII"===c||"TEXT"===c)h=v(a,g),f=h.binLen,e=h.value;else if("B64"===c)h=w(a),f=h.binLen,e=h.value;else throw"inputFormat must be HEX, TEXT, ASCII, or B64";this.getHash=function(a,c,b,g){var h=null,d=e.slice(),l=f,m;3===arguments.length?"number"!== +typeof b&&(g=b,b=1):2===arguments.length&&(b=1);if(b!==parseInt(b,10)||1>b)throw"numRounds must a integer >= 1";switch(c){case "HEX":h=x;break;case "B64":h=y;break;default:throw"format must be HEX or B64";}if("SHA-224"===a)for(m=0;mm/8&&(d[b]&=4294967040);for(l=0;l<=b;l+=1)A[l]=d[l]^909522486,s[l]=d[l]^1549556828;c=q(s.concat(q(A.concat(e),a+f,c)),a+n,c);return h(c, +z(k))}}function v(a,c){var b=[],f,e=[],g=0,h;if("UTF8"===c)for(h=0;h>>12,e[1]=128|(f&4032)>>>6,e[2]=128|f&63):128>>6,e[1]=128|f&63):e[0]=f,f=0;f>>2]|=e[f]<<24-g%4*8,g+=1;else if("UTF16"===c)for(h=0;h>>2]|=a.charCodeAt(h)<<16-g%4*8,g+=2;return{value:b,binLen:8*g}}function u(a){var c=[],b=a.length,f,e;if(0!==b%2)throw"String of HEX type must be in byte increments";for(f=0;f< +b;f+=2){e=parseInt(a.substr(f,2),16);if(isNaN(e))throw"String of HEX type contains invalid characters";c[f>>>3]|=e<<24-f%8*4}return{value:c,binLen:4*b}}function w(a){var c=[],b=0,f,e,g,h,k;if(-1===a.search(/^[a-zA-Z0-9=+\/]+$/))throw"Invalid character in base-64 string";f=a.indexOf("=");a=a.replace(/\=/g,"");if(-1!==f&&f>2]|=(l>>>16-8*k&255)<<24-b%4*8,b+=1}return{value:c,binLen:8*b}}function A(a,c){var b="",h=4*a.length,f,k;for(f=0;f>>2]>>>8*(3-f%4),b+="0123456789abcdef".charAt(k>>>4&15)+"0123456789abcdef".charAt(k&15);return c.outputUpper?b.toUpperCase():b}function B(a,c){var b="",h=4*a.length,f,k,l;for(f=0;f>>2]>>>8*(3-f%4)&255)<<16|(a[f+1>>>2]>>>8*(3-(f+1)%4)&255)<<8|a[f+2>>>2]>>>8*(3-(f+2)%4)&255,k=0;4>k;k+=1)b=8*f+6*k<=32*a.length?b+ +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(l>>>6*(3-k)&63):b+c.b64Pad;return b}function C(a){var c={outputUpper:!1,b64Pad:"="};try{a.hasOwnProperty("outputUpper")&&(c.outputUpper=a.outputUpper),a.hasOwnProperty("b64Pad")&&(c.b64Pad=a.b64Pad)}catch(b){}if("boolean"!==typeof c.outputUpper)throw"Invalid outputUpper formatting option";if("string"!==typeof c.b64Pad)throw"Invalid b64Pad formatting option";return c}function p(a,c){var b=null,b=new n(a.a,a.b);return b=32>=c? +new n(b.a>>>c|b.b<<32-c&4294967295,b.b>>>c|b.a<<32-c&4294967295):new n(b.b>>>c-32|b.a<<64-c&4294967295,b.a>>>c-32|b.b<<64-c&4294967295)}function D(a,c){var b=null;return b=32>=c?new n(a.a>>>c,a.b>>>c|a.a<<32-c&4294967295):new n(0,a.a>>>c-32)}function K(a,c,b){return new n(a.a&c.a^~a.a&b.a,a.b&c.b^~a.b&b.b)}function L(a,c,b){return new n(a.a&c.a^a.a&b.a^c.a&b.a,a.b&c.b^a.b&b.b^c.b&b.b)}function M(a){var c=p(a,28),b=p(a,34);a=p(a,39);return new n(c.a^b.a^a.a,c.b^b.b^a.b)}function N(a){var c=p(a,14), +b=p(a,18);a=p(a,41);return new n(c.a^b.a^a.a,c.b^b.b^a.b)}function O(a){var c=p(a,1),b=p(a,8);a=D(a,7);return new n(c.a^b.a^a.a,c.b^b.b^a.b)}function P(a){var c=p(a,19),b=p(a,61);a=D(a,6);return new n(c.a^b.a^a.a,c.b^b.b^a.b)}function Q(a,c){var b,h,f;b=(a.b&65535)+(c.b&65535);h=(a.b>>>16)+(c.b>>>16)+(b>>>16);f=(h&65535)<<16|b&65535;b=(a.a&65535)+(c.a&65535)+(h>>>16);h=(a.a>>>16)+(c.a>>>16)+(b>>>16);return new n((h&65535)<<16|b&65535,f)}function R(a,c,b,h){var f,k,l;f=(a.b&65535)+(c.b&65535)+(b.b& +65535)+(h.b&65535);k=(a.b>>>16)+(c.b>>>16)+(b.b>>>16)+(h.b>>>16)+(f>>>16);l=(k&65535)<<16|f&65535;f=(a.a&65535)+(c.a&65535)+(b.a&65535)+(h.a&65535)+(k>>>16);k=(a.a>>>16)+(c.a>>>16)+(b.a>>>16)+(h.a>>>16)+(f>>>16);return new n((k&65535)<<16|f&65535,l)}function S(a,c,b,h,f){var k,l,p;k=(a.b&65535)+(c.b&65535)+(b.b&65535)+(h.b&65535)+(f.b&65535);l=(a.b>>>16)+(c.b>>>16)+(b.b>>>16)+(h.b>>>16)+(f.b>>>16)+(k>>>16);p=(l&65535)<<16|k&65535;k=(a.a&65535)+(c.a&65535)+(b.a&65535)+(h.a&65535)+(f.a&65535)+(l>>> +16);l=(a.a>>>16)+(c.a>>>16)+(b.a>>>16)+(h.a>>>16)+(f.a>>>16)+(k>>>16);return new n((l&65535)<<16|k&65535,p)}function t(a,c,b){var h,f,k,l,p,t,u,E,x,e,m,q,r,y,v,s,z,A,B,C,D,F,G,H,d,w=[],I,g=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891, +3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298];e=[3238371032,914150663,812702999,4144912697,4290775857,1750603025,1694076839,3204075428];f=[1779033703,3144134277,1013904242, +2773480762,1359893119,2600822924,528734635,1541459225];if("SHA-384"===b||"SHA-512"===b)m=80,h=(c+128>>>10<<5)+31,y=32,v=2,d=n,s=Q,z=R,A=S,B=O,C=P,D=M,F=N,H=L,G=K,g=[new d(g[0],3609767458),new d(g[1],602891725),new d(g[2],3964484399),new d(g[3],2173295548),new d(g[4],4081628472),new d(g[5],3053834265),new d(g[6],2937671579),new d(g[7],3664609560),new d(g[8],2734883394),new d(g[9],1164996542),new d(g[10],1323610764),new d(g[11],3590304994),new d(g[12],4068182383),new d(g[13],991336113),new d(g[14], +633803317),new d(g[15],3479774868),new d(g[16],2666613458),new d(g[17],944711139),new d(g[18],2341262773),new d(g[19],2007800933),new d(g[20],1495990901),new d(g[21],1856431235),new d(g[22],3175218132),new d(g[23],2198950837),new d(g[24],3999719339),new d(g[25],766784016),new d(g[26],2566594879),new d(g[27],3203337956),new d(g[28],1034457026),new d(g[29],2466948901),new d(g[30],3758326383),new d(g[31],168717936),new d(g[32],1188179964),new d(g[33],1546045734),new d(g[34],1522805485),new d(g[35],2643833823), +new d(g[36],2343527390),new d(g[37],1014477480),new d(g[38],1206759142),new d(g[39],344077627),new d(g[40],1290863460),new d(g[41],3158454273),new d(g[42],3505952657),new d(g[43],106217008),new d(g[44],3606008344),new d(g[45],1432725776),new d(g[46],1467031594),new d(g[47],851169720),new d(g[48],3100823752),new d(g[49],1363258195),new d(g[50],3750685593),new d(g[51],3785050280),new d(g[52],3318307427),new d(g[53],3812723403),new d(g[54],2003034995),new d(g[55],3602036899),new d(g[56],1575990012), +new d(g[57],1125592928),new d(g[58],2716904306),new d(g[59],442776044),new d(g[60],593698344),new d(g[61],3733110249),new d(g[62],2999351573),new d(g[63],3815920427),new d(3391569614,3928383900),new d(3515267271,566280711),new d(3940187606,3454069534),new d(4118630271,4000239992),new d(116418474,1914138554),new d(174292421,2731055270),new d(289380356,3203993006),new d(460393269,320620315),new d(685471733,587496836),new d(852142971,1086792851),new d(1017036298,365543100),new d(1126000580,2618297676), +new d(1288033470,3409855158),new d(1501505948,4234509866),new d(1607167915,987167468),new d(1816402316,1246189591)],e="SHA-384"===b?[new d(3418070365,e[0]),new d(1654270250,e[1]),new d(2438529370,e[2]),new d(355462360,e[3]),new d(1731405415,e[4]),new d(41048885895,e[5]),new d(3675008525,e[6]),new d(1203062813,e[7])]:[new d(f[0],4089235720),new d(f[1],2227873595),new d(f[2],4271175723),new d(f[3],1595750129),new d(f[4],2917565137),new d(f[5],725511199),new d(f[6],4215389547),new d(f[7],327033209)]; +else throw"Unexpected error in SHA-2 implementation";a[c>>>5]|=128<<24-c%32;a[h]=c;I=a.length;for(q=0;qr?new d(a[r*v+q],a[r*v+q+1]):z(C(w[r-2]),w[r-7],B(w[r-15]),w[r-16]),E=A(u,F(l),G(l,p,t),g[r],w[r]),x=s(D(c),H(c,h,f)),u=t,t=p,p=l,l=s(k,E),k=f,f=h,h=c,c=s(E,x);e[0]=s(c,e[0]);e[1]=s(h,e[1]);e[2]=s(f,e[2]);e[3]=s(k,e[3]);e[4]=s(l,e[4]);e[5]=s(p,e[5]);e[6]=s(t,e[6]);e[7]=s(u,e[7])}if("SHA-384"===b)a=[e[0].a,e[0].b, +e[1].a,e[1].b,e[2].a,e[2].b,e[3].a,e[3].b,e[4].a,e[4].b,e[5].a,e[5].b];else if("SHA-512"===b)a=[e[0].a,e[0].b,e[1].a,e[1].b,e[2].a,e[2].b,e[3].a,e[3].b,e[4].a,e[4].b,e[5].a,e[5].b,e[6].a,e[6].b,e[7].a,e[7].b];else throw"Unexpected error in SHA-2 implementation";return a}"function"===typeof define&&typeof define.amd?define(function(){return u}):"undefined"!==typeof exports?"undefined"!==typeof module&&module.exports?module.exports=exports=u:exports=u:J.jsSHA=u})(this);