|
8 | 8 | * See http://caligatio.github.com/jsSHA/ for more information |
9 | 9 | */ |
10 | 10 |
|
11 | | -/*jslint |
12 | | - bitwise: true, multivar: true, for: true, this: true, sub: true, esversion: 3 |
13 | | -*/ |
14 | | - |
15 | 11 | /** |
16 | 12 | * SUPPORTED_ALGS is the stub for a compile flag that will cause pruning of |
17 | 13 | * functions that are not needed when a limited number of SHA families are |
@@ -292,7 +288,7 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
292 | 288 |
|
293 | 289 | for (j = 0; j < strPart.length; j += 1) |
294 | 290 | { |
295 | | - index = b64Tab.indexOf(strPart[j]); |
| 291 | + index = b64Tab.indexOf(strPart.charAt(j)); |
296 | 292 | tmpInt |= index << (18 - (6 * j)); |
297 | 293 | } |
298 | 294 |
|
@@ -331,26 +327,46 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
331 | 327 | */ |
332 | 328 | function arraybuffer2packed(arr, existingPacked, existingPackedLen, bigEndianMod) |
333 | 329 | { |
334 | | - var packed, i, existingByteLen, intOffset, byteOffset, shiftModifier, arrView; |
| 330 | + return uint8array2packed(new Uint8Array(arr), existingPacked, existingPackedLen, bigEndianMod) |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * Convert an Uint8Array to an array of big-endian words |
| 335 | + * |
| 336 | + * @private |
| 337 | + * @param {Uint8Array} arr Uint8Array to be converted to binary |
| 338 | + * representation |
| 339 | + * @param {Array<number>} existingPacked A packed int array of bytes to |
| 340 | + * append the results to |
| 341 | + * @param {number} existingPackedLen The number of bits in the existingPacked |
| 342 | + * array |
| 343 | + * @param {number} bigEndianMod Modifier for whether hash function is |
| 344 | + * big or small endian |
| 345 | + * @return {{value : Array<number>, binLen : number}} Hash list where |
| 346 | + * "value" contains the output number array and "binLen" is the binary |
| 347 | + * length of "value" |
| 348 | + */ |
| 349 | + function uint8array2packed(arr, existingPacked, existingPackedLen, bigEndianMod) |
| 350 | + { |
| 351 | + var packed, i, existingByteLen, intOffset, byteOffset, shiftModifier; |
335 | 352 |
|
336 | 353 | packed = existingPacked || [0]; |
337 | 354 | existingPackedLen = existingPackedLen || 0; |
338 | 355 | existingByteLen = existingPackedLen >>> 3; |
339 | 356 | shiftModifier = (bigEndianMod === -1) ? 3 : 0; |
340 | | - arrView = new Uint8Array(arr); |
341 | 357 |
|
342 | | - for (i = 0; i < arr.byteLength; i += 1) |
| 358 | + for (i = 0; i < arr.length; i += 1) |
343 | 359 | { |
344 | 360 | byteOffset = i + existingByteLen; |
345 | 361 | intOffset = byteOffset >>> 2; |
346 | 362 | if (packed.length <= intOffset) |
347 | 363 | { |
348 | 364 | packed.push(0); |
349 | 365 | } |
350 | | - packed[intOffset] |= arrView[i] << (8 * (shiftModifier + bigEndianMod * (byteOffset % 4))); |
| 366 | + packed[intOffset] |= arr[i] << (8 * (shiftModifier + bigEndianMod * (byteOffset % 4))); |
351 | 367 | } |
352 | 368 |
|
353 | | - return {"value" : packed, "binLen" : arr.byteLength * 8 + existingPackedLen}; |
| 369 | + return {"value" : packed, "binLen" : arr.length * 8 + existingPackedLen}; |
354 | 370 | } |
355 | 371 |
|
356 | 372 | /** |
@@ -482,6 +498,31 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
482 | 498 | return retVal; |
483 | 499 | } |
484 | 500 |
|
| 501 | + /** |
| 502 | + * Convert an array of big-endian words to an Uint8Array |
| 503 | + * |
| 504 | + * @private |
| 505 | + * @param {Array<number>} packed Array of integers to be converted to |
| 506 | + * an Uint8Array |
| 507 | + * @param {number} outputLength Length of output in bits |
| 508 | + * @param {number} bigEndianMod Modifier for whether hash function is |
| 509 | + * big or small endian |
| 510 | + * @return {Uint8Array} Raw bytes representation of the parameter in an |
| 511 | + * Uint8Array |
| 512 | + */ |
| 513 | + function packed2uint8array(packed, outputLength, bigEndianMod) |
| 514 | + { |
| 515 | + var length = outputLength / 8, i, retVal = new Uint8Array(length), shiftModifier; |
| 516 | + shiftModifier = (bigEndianMod === -1) ? 3 : 0; |
| 517 | + |
| 518 | + for (i = 0; i < length; i += 1) |
| 519 | + { |
| 520 | + retVal[i] = (packed[i >>> 2] >>> (8 * (shiftModifier + bigEndianMod * (i % 4)))) & 0xFF; |
| 521 | + } |
| 522 | + |
| 523 | + return retVal; |
| 524 | + } |
| 525 | + |
485 | 526 | /** |
486 | 527 | * Validate hash list containing output formatting options, ensuring |
487 | 528 | * presence of every option or adding the default value |
@@ -537,7 +578,7 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
537 | 578 | * UTF16LE) |
538 | 579 | * @param {number} bigEndianMod Modifier for whether hash function is |
539 | 580 | * big or small endian |
540 | | - * @return {function((string|ArrayBuffer), Array<number>=, number=): {value : |
| 581 | + * @return {function((string|ArrayBuffer|Uint8Array), Array<number>=, number=): {value : |
541 | 582 | * Array<number>, binLen : number}} Function that will convert an input |
542 | 583 | * string to a packed int array |
543 | 584 | */ |
@@ -648,8 +689,30 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
648 | 689 | return arraybuffer2packed(arr, existingBin, existingBinLen, bigEndianMod); |
649 | 690 | }; |
650 | 691 | break; |
| 692 | + case "UINT8ARRAY": |
| 693 | + try { |
| 694 | + retVal = new Uint8Array(0); |
| 695 | + } catch(ignore) { |
| 696 | + throw new Error("UINT8ARRAY not supported by this environment"); |
| 697 | + } |
| 698 | + /** |
| 699 | + * @param {Uint8Array} arr Uint8Array to be converted to binary |
| 700 | + * representation |
| 701 | + * @param {Array<number>} existingBin A packed int array of bytes to |
| 702 | + * append the results to |
| 703 | + * @param {number} existingBinLen The number of bits in the existingBin |
| 704 | + * array |
| 705 | + * @return {{value : Array<number>, binLen : number}} Hash list where |
| 706 | + * "value" contains the output number array and "binLen" is the binary |
| 707 | + * length of "value" |
| 708 | + */ |
| 709 | + retVal = function(arr, existingBin, existingBinLen) |
| 710 | + { |
| 711 | + return uint8array2packed(arr, existingBin, existingBinLen, bigEndianMod); |
| 712 | + }; |
| 713 | + break; |
651 | 714 | default: |
652 | | - throw new Error("format must be HEX, TEXT, B64, BYTES, or ARRAYBUFFER"); |
| 715 | + throw new Error("format must be HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY"); |
653 | 716 | } |
654 | 717 |
|
655 | 718 | return retVal; |
@@ -1856,7 +1919,7 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
1856 | 1919 | * @param {string} variant The desired SHA variant (SHA-1, SHA-224, SHA-256, |
1857 | 1920 | * SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-384, or SHA3-512) |
1858 | 1921 | * @param {string} inputFormat The format of srcString: HEX, TEXT, B64, |
1859 | | - * BYTES, or ARRAYBUFFER |
| 1922 | + * BYTES, ARRAYBUFFER, or UINT8ARRAY |
1860 | 1923 | * @param {{encoding: (string|undefined), numRounds: (number|undefined)}=} |
1861 | 1924 | * options Optional values |
1862 | 1925 | */ |
@@ -1986,9 +2049,9 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
1986 | 2049 | * immediately after jsSHA object instantiation |
1987 | 2050 | * |
1988 | 2051 | * @expose |
1989 | | - * @param {string|ArrayBuffer} key The key used to calculate the HMAC |
| 2052 | + * @param {string|ArrayBuffer|Uint8Array} key The key used to calculate the HMAC |
1990 | 2053 | * @param {string} inputFormat The format of key, HEX, TEXT, B64, BYTES, |
1991 | | - * or ARRAYBUFFER |
| 2054 | + * ARRAYBUFFER, or UINT8ARRAY |
1992 | 2055 | * @param {{encoding : (string|undefined)}=} options Associative array |
1993 | 2056 | * of input format options |
1994 | 2057 | */ |
@@ -2071,7 +2134,7 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
2071 | 2134 | * rest for either a future update or getHash call. |
2072 | 2135 | * |
2073 | 2136 | * @expose |
2074 | | - * @param {string|ArrayBuffer} srcString The string to be hashed |
| 2137 | + * @param {string|ArrayBuffer|Uint8Array} srcString The string to be hashed |
2075 | 2138 | */ |
2076 | 2139 | this.update = function(srcString) |
2077 | 2140 | { |
@@ -2107,10 +2170,10 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
2107 | 2170 | * |
2108 | 2171 | * @expose |
2109 | 2172 | * @param {string} format The desired output formatting (B64, HEX, |
2110 | | - * BYTES, or ARRAYBUFFER) |
| 2173 | + * BYTES, ARRAYBUFFER, or UINT8ARRAY) |
2111 | 2174 | * @param {{outputUpper : (boolean|undefined), b64Pad : (string|undefined), |
2112 | 2175 | * shakeLen : (number|undefined)}=} options Hash list of output formatting options |
2113 | | - * @return {string|ArrayBuffer} The string representation of the hash |
| 2176 | + * @return {string|ArrayBuffer|Uint8Array} The string representation of the hash |
2114 | 2177 | * in the format specified. |
2115 | 2178 | */ |
2116 | 2179 | this.getHash = function(format, options) |
@@ -2153,8 +2216,16 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
2153 | 2216 | } |
2154 | 2217 | formatFunc = function(binarray) {return packed2arraybuffer(binarray, outputBinLen, bigEndianMod);}; |
2155 | 2218 | break; |
| 2219 | + case "UINT8ARRAY": |
| 2220 | + try { |
| 2221 | + i = new Uint8Array(0); |
| 2222 | + } catch (ignore) { |
| 2223 | + throw new Error("UINT8ARRAY not supported by this environment"); |
| 2224 | + } |
| 2225 | + formatFunc = function(binarray) {return packed2uint8array(binarray, outputBinLen, bigEndianMod);}; |
| 2226 | + break; |
2156 | 2227 | default: |
2157 | | - throw new Error("format must be HEX, B64, BYTES, or ARRAYBUFFER"); |
| 2228 | + throw new Error("format must be HEX, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY"); |
2158 | 2229 | } |
2159 | 2230 |
|
2160 | 2231 | finalizedState = finalizeFunc(remainder.slice(), remainderLen, processedLen, stateCloneFunc(intermediateState), outputBinLen); |
@@ -2182,11 +2253,11 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
2182 | 2253 | * |
2183 | 2254 | * @expose |
2184 | 2255 | * @param {string} format The desired output formatting |
2185 | | - * (B64, HEX, BYTES, or ARRAYBUFFER) |
| 2256 | + * (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY) |
2186 | 2257 | * @param {{outputUpper : (boolean|undefined), b64Pad : (string|undefined), |
2187 | 2258 | * shakeLen : (number|undefined)}=} options associative array of output |
2188 | 2259 | * formatting options |
2189 | | - * @return {string|ArrayBuffer} The string representation of the hash in the |
| 2260 | + * @return {string|ArrayBuffer|Uint8Array} The string representation of the hash in the |
2190 | 2261 | * format specified. |
2191 | 2262 | */ |
2192 | 2263 | this.getHMAC = function(format, options) |
@@ -2220,8 +2291,16 @@ var SUPPORTED_ALGS = 8 | 4 | 2 | 1; |
2220 | 2291 | } |
2221 | 2292 | formatFunc = function(binarray) {return packed2arraybuffer(binarray, outputBinLen, bigEndianMod);}; |
2222 | 2293 | break; |
| 2294 | + case "UINT8ARRAY": |
| 2295 | + try { |
| 2296 | + formatFunc = new Uint8Array(0); |
| 2297 | + } catch(ignore) { |
| 2298 | + throw new Error("UINT8ARRAY not supported by this environment"); |
| 2299 | + } |
| 2300 | + formatFunc = function(binarray) {return packed2uint8array(binarray, outputBinLen, bigEndianMod);}; |
| 2301 | + break; |
2223 | 2302 | default: |
2224 | | - throw new Error("outputFormat must be HEX, B64, BYTES, or ARRAYBUFFER"); |
| 2303 | + throw new Error("outputFormat must be HEX, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY"); |
2225 | 2304 | } |
2226 | 2305 |
|
2227 | 2306 | firstHash = finalizeFunc(remainder.slice(), remainderLen, processedLen, stateCloneFunc(intermediateState), outputBinLen); |
|
0 commit comments