From 8ae70989aeadb03af48c0db001dba7d32cc86e96 Mon Sep 17 00:00:00 2001 From: "Chen, Yi-Cyuan" Date: Thu, 29 Dec 2016 09:31:53 +0800 Subject: [PATCH] Fixed ArrayBuffer dosen't work in Webpack. --- CHANGELOG.md | 4 +++ bower.json | 2 +- package.json | 2 +- src/sha3.js | 90 ++++++++++++++++++++++++---------------------- tests/node-test.js | 28 ++++++++++++++- 5 files changed, 80 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 862da1a..6aa11df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## v0.5.6 / 2016-12-29 +### Fixed +- ArrayBuffer dosen't work in Webpack. + ## v0.5.5 / 2016-09-26 ### Added - TypeScript support. diff --git a/bower.json b/bower.json index 86f09f5..3d7e5a6 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-sha3", - "version": "0.5.5", + "version": "0.5.6", "main": ["src/sha3.js"], "ignore": [ "samples", diff --git a/package.json b/package.json index a8a50e6..b4ef9a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-sha3", - "version": "0.5.5", + "version": "0.5.6", "description": "A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.", "main": "src/sha3.js", "typings": "index", diff --git a/src/sha3.js b/src/sha3.js index 362aade..77f3d29 100644 --- a/src/sha3.js +++ b/src/sha3.js @@ -1,27 +1,29 @@ /** * [js-sha3]{@link https://github.com/emn178/js-sha3} * - * @version 0.5.5 + * @version 0.5.6 * @author Chen, Yi-Cyuan [emn178@gmail.com] * @copyright Chen, Yi-Cyuan 2015-2016 * @license MIT */ -(function (root) { +/*jslint bitwise: true */ +(function () { 'use strict'; - var NODE_JS = typeof process == 'object' && process.versions && process.versions.node; + var root = typeof window === 'object' ? window : {}; + var NODE_JS = !root.JS_SHA3_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; if (NODE_JS) { root = global; } - var COMMON_JS = !root.JS_SHA3_TEST && typeof module == 'object' && module.exports; + var COMMON_JS = !root.JS_SHA3_NO_COMMON_JS && typeof module === 'object' && module.exports; var HEX_CHARS = '0123456789abcdef'.split(''); var SHAKE_PADDING = [31, 7936, 2031616, 520093696]; var KECCAK_PADDING = [1, 256, 65536, 16777216]; var PADDING = [6, 1536, 393216, 100663296]; var SHIFT = [0, 8, 16, 24]; var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649, - 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0, - 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771, + 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0, + 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771, 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648, 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648]; var BITS = [224, 256, 384, 512]; @@ -31,13 +33,13 @@ var createOutputMethod = function (bits, padding, outputType) { return function (message) { return new Keccak(bits, padding, bits).update(message)[outputType](); - } + }; }; var createShakeOutputMethod = function (bits, padding, outputType) { return function (message, outputBits) { return new Keccak(bits, padding, outputBits).update(message)[outputType](); - } + }; }; var createMethod = function (bits, padding) { @@ -48,7 +50,7 @@ method.update = function (message) { return method.create().update(message); }; - for (var i = 0;i < OUTPUT_TYPES.length;++i) { + for (var i = 0; i < OUTPUT_TYPES.length; ++i) { var type = OUTPUT_TYPES[i]; method[type] = createOutputMethod(bits, padding, type); } @@ -63,7 +65,7 @@ method.update = function (message, outputBits) { return method.create(outputBits).update(message); }; - for (var i = 0;i < OUTPUT_TYPES.length;++i) { + for (var i = 0; i < OUTPUT_TYPES.length; ++i) { var type = OUTPUT_TYPES[i]; method[type] = createShakeOutputMethod(bits, padding, type); } @@ -76,13 +78,15 @@ {name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod} ]; - var methods = {}; + var methods = {}, methodNames = []; - for (var i = 0;i < algorithms.length;++i) { + for (var i = 0; i < algorithms.length; ++i) { var algorithm = algorithms[i]; var bits = algorithm.bits; - for (var j = 0;j < bits.length;++j) { - methods[algorithm.name +'_' + bits[j]] = algorithm.createMethod(bits[j], algorithm.padding); + for (var j = 0; j < bits.length; ++j) { + var methodName = algorithm.name +'_' + bits[j]; + methodNames.push(methodName); + methods[methodName] = algorithm.createMethod(bits[j], algorithm.padding); } } @@ -99,33 +103,33 @@ this.outputBlocks = outputBits >> 5; this.extraBytes = (outputBits & 31) >> 3; - for (var i = 0;i < 50;++i) { + for (var i = 0; i < 50; ++i) { this.s[i] = 0; } - }; + } Keccak.prototype.update = function (message) { var notString = typeof message != 'string'; if (notString && message instanceof ArrayBuffer) { message = new Uint8Array(message); } - var length = message.length, blocks = this.blocks, byteCount = this.byteCount, - blockCount = this.blockCount, index = 0, s = this.s, i, code; - + var length = message.length, blocks = this.blocks, byteCount = this.byteCount, + blockCount = this.blockCount, index = 0, s = this.s, i, code; + while (index < length) { if (this.reset) { this.reset = false; blocks[0] = this.block; - for (i = 1;i < blockCount + 1;++i) { + for (i = 1; i < blockCount + 1; ++i) { blocks[i] = 0; } } if (notString) { - for (i = this.start;index < length && i < byteCount;++index) { + for (i = this.start; index < length && i < byteCount; ++index) { blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; } } else { - for (i = this.start;index < length && i < byteCount;++index) { + for (i = this.start; index < length && i < byteCount; ++index) { code = message.charCodeAt(index); if (code < 0x80) { blocks[i >> 2] |= code << SHIFT[i++ & 3]; @@ -149,7 +153,7 @@ if (i >= byteCount) { this.start = i - byteCount; this.block = blocks[blockCount]; - for (i = 0;i < blockCount;++i) { + for (i = 0; i < blockCount; ++i) { s[i] ^= blocks[i]; } f(s); @@ -166,12 +170,12 @@ blocks[i >> 2] |= this.padding[i & 3]; if (this.lastByteIndex == this.byteCount) { blocks[0] = blocks[blockCount]; - for (i = 1;i < blockCount + 1;++i) { + for (i = 1; i < blockCount + 1; ++i) { blocks[i] = 0; } } blocks[blockCount - 1] |= 0x80000000; - for (i = 0;i < blockCount;++i) { + for (i = 0; i < blockCount; ++i) { s[i] ^= blocks[i]; } f(s); @@ -180,18 +184,18 @@ Keccak.prototype.toString = Keccak.prototype.hex = function () { this.finalize(); - var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, + var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, extraBytes = this.extraBytes, i = 0, j = 0; var hex = '', block; while (j < outputBlocks) { - for (i = 0;i < blockCount && j < outputBlocks;++i, ++j) { + for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { block = s[i]; hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F] + HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F] + HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F] + HEX_CHARS[(block >> 28) & 0x0F] + HEX_CHARS[(block >> 24) & 0x0F]; } - if (j % blockCount == 0) { + if (j % blockCount === 0) { f(s); i = 0; } @@ -214,7 +218,7 @@ Keccak.prototype.arrayBuffer = function () { this.finalize(); - var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, + var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, extraBytes = this.extraBytes, i = 0, j = 0; var bytes = this.outputBits >> 3; var buffer; @@ -225,10 +229,10 @@ } var array = new Uint32Array(buffer); while (j < outputBlocks) { - for (i = 0;i < blockCount && j < outputBlocks;++i, ++j) { + for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { array[j] = s[i]; } - if (j % blockCount == 0) { + if (j % blockCount === 0) { f(s); } } @@ -244,11 +248,11 @@ Keccak.prototype.digest = Keccak.prototype.array = function () { this.finalize(); - var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, + var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, extraBytes = this.extraBytes, i = 0, j = 0; var array = [], offset, block; while (j < outputBlocks) { - for (i = 0;i < blockCount && j < outputBlocks;++i, ++j) { + for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { offset = j << 2; block = s[i]; array[offset] = block & 0xFF; @@ -256,7 +260,7 @@ array[offset + 2] = (block >> 16) & 0xFF; array[offset + 3] = (block >> 24) & 0xFF; } - if (j % blockCount == 0) { + if (j % blockCount === 0) { f(s); } } @@ -277,11 +281,11 @@ }; var f = function (s) { - var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, - b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, - b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, + var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, + b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, + b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49; - for (n = 0;n < 48;n += 2) { + for (n = 0; n < 48; n += 2) { c0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40]; c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41]; c2 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42]; @@ -459,13 +463,13 @@ s[0] ^= RC[n]; s[1] ^= RC[n + 1]; } - } + }; if (COMMON_JS) { module.exports = methods; - } else if (root) { - for (var key in methods) { - root[key] = methods[key]; + } else { + for (var i = 0; i < methodNames.length; ++i) { + root[methodNames[i]] = methods[methodNames[i]]; } } -}(this)); +})(); diff --git a/tests/node-test.js b/tests/node-test.js index 6f287dd..f135f5d 100644 --- a/tests/node-test.js +++ b/tests/node-test.js @@ -1,3 +1,26 @@ +// Node.js env +expect = require('expect.js'); +var sha3 = require('../src/sha3.js'); +keccak_512 = sha3.keccak_512; +keccak_384 = sha3.keccak_384; +keccak_256 = sha3.keccak_256; +keccak_224 = sha3.keccak_224; +sha3_512 = sha3.sha3_512; +sha3_384 = sha3.sha3_384; +sha3_256 = sha3.sha3_256; +sha3_224 = sha3.sha3_224; +shake_128 = sha3.shake_128; +shake_256 = sha3.shake_256; +require('./test.js'); +require('./test-shake.js'); + +delete require.cache[require.resolve('../src/sha3.js')]; +delete require.cache[require.resolve('./test.js')]; +delete require.cache[require.resolve('./test-shake.js')]; + +// Webpack browser env +JS_SHA3_NO_NODE_JS = true; +window = global; expect = require('expect.js'); var sha3 = require('../src/sha3.js'); keccak_512 = sha3.keccak_512; @@ -27,7 +50,10 @@ keccak_224 = null; shake_128 = null; shake_256 = null; -JS_SHA3_TEST = true; +// browser env +JS_SHA3_NO_NODE_JS = true; +JS_SHA3_NO_COMMON_JS = true; +window = global; require('../src/sha3.js'); require('./test.js'); require('./test-shake.js');