Fixed ArrayBuffer dosen't work in Webpack.

pull/12/head
Chen, Yi-Cyuan 9 years ago
parent 035642212a
commit 8ae70989ae

@ -1,5 +1,9 @@
# Change Log # Change Log
## v0.5.6 / 2016-12-29
### Fixed
- ArrayBuffer dosen't work in Webpack.
## v0.5.5 / 2016-09-26 ## v0.5.5 / 2016-09-26
### Added ### Added
- TypeScript support. - TypeScript support.

@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.5.5", "version": "0.5.6",
"main": ["src/sha3.js"], "main": ["src/sha3.js"],
"ignore": [ "ignore": [
"samples", "samples",

@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "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.", "description": "A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha3.js", "main": "src/sha3.js",
"typings": "index", "typings": "index",

@ -1,27 +1,29 @@
/** /**
* [js-sha3]{@link https://github.com/emn178/js-sha3} * [js-sha3]{@link https://github.com/emn178/js-sha3}
* *
* @version 0.5.5 * @version 0.5.6
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2015-2016 * @copyright Chen, Yi-Cyuan 2015-2016
* @license MIT * @license MIT
*/ */
(function (root) { /*jslint bitwise: true */
(function () {
'use strict'; '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) { if (NODE_JS) {
root = global; 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 HEX_CHARS = '0123456789abcdef'.split('');
var SHAKE_PADDING = [31, 7936, 2031616, 520093696]; var SHAKE_PADDING = [31, 7936, 2031616, 520093696];
var KECCAK_PADDING = [1, 256, 65536, 16777216]; var KECCAK_PADDING = [1, 256, 65536, 16777216];
var PADDING = [6, 1536, 393216, 100663296]; var PADDING = [6, 1536, 393216, 100663296];
var SHIFT = [0, 8, 16, 24]; var SHIFT = [0, 8, 16, 24];
var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649, var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649,
0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0, 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0,
2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771, 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771,
2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648, 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648,
2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648]; 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648];
var BITS = [224, 256, 384, 512]; var BITS = [224, 256, 384, 512];
@ -31,13 +33,13 @@
var createOutputMethod = function (bits, padding, outputType) { var createOutputMethod = function (bits, padding, outputType) {
return function (message) { return function (message) {
return new Keccak(bits, padding, bits).update(message)[outputType](); return new Keccak(bits, padding, bits).update(message)[outputType]();
} };
}; };
var createShakeOutputMethod = function (bits, padding, outputType) { var createShakeOutputMethod = function (bits, padding, outputType) {
return function (message, outputBits) { return function (message, outputBits) {
return new Keccak(bits, padding, outputBits).update(message)[outputType](); return new Keccak(bits, padding, outputBits).update(message)[outputType]();
} };
}; };
var createMethod = function (bits, padding) { var createMethod = function (bits, padding) {
@ -48,7 +50,7 @@
method.update = function (message) { method.update = function (message) {
return method.create().update(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]; var type = OUTPUT_TYPES[i];
method[type] = createOutputMethod(bits, padding, type); method[type] = createOutputMethod(bits, padding, type);
} }
@ -63,7 +65,7 @@
method.update = function (message, outputBits) { method.update = function (message, outputBits) {
return method.create(outputBits).update(message); 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]; var type = OUTPUT_TYPES[i];
method[type] = createShakeOutputMethod(bits, padding, type); method[type] = createShakeOutputMethod(bits, padding, type);
} }
@ -76,13 +78,15 @@
{name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod} {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 algorithm = algorithms[i];
var bits = algorithm.bits; var bits = algorithm.bits;
for (var j = 0;j < bits.length;++j) { for (var j = 0; j < bits.length; ++j) {
methods[algorithm.name +'_' + bits[j]] = algorithm.createMethod(bits[j], algorithm.padding); 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.outputBlocks = outputBits >> 5;
this.extraBytes = (outputBits & 31) >> 3; this.extraBytes = (outputBits & 31) >> 3;
for (var i = 0;i < 50;++i) { for (var i = 0; i < 50; ++i) {
this.s[i] = 0; this.s[i] = 0;
} }
}; }
Keccak.prototype.update = function (message) { Keccak.prototype.update = function (message) {
var notString = typeof message != 'string'; var notString = typeof message != 'string';
if (notString && message instanceof ArrayBuffer) { if (notString && message instanceof ArrayBuffer) {
message = new Uint8Array(message); message = new Uint8Array(message);
} }
var length = message.length, blocks = this.blocks, byteCount = this.byteCount, var length = message.length, blocks = this.blocks, byteCount = this.byteCount,
blockCount = this.blockCount, index = 0, s = this.s, i, code; blockCount = this.blockCount, index = 0, s = this.s, i, code;
while (index < length) { while (index < length) {
if (this.reset) { if (this.reset) {
this.reset = false; this.reset = false;
blocks[0] = this.block; blocks[0] = this.block;
for (i = 1;i < blockCount + 1;++i) { for (i = 1; i < blockCount + 1; ++i) {
blocks[i] = 0; blocks[i] = 0;
} }
} }
if (notString) { 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]; blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
} }
} else { } else {
for (i = this.start;index < length && i < byteCount;++index) { for (i = this.start; index < length && i < byteCount; ++index) {
code = message.charCodeAt(index); code = message.charCodeAt(index);
if (code < 0x80) { if (code < 0x80) {
blocks[i >> 2] |= code << SHIFT[i++ & 3]; blocks[i >> 2] |= code << SHIFT[i++ & 3];
@ -149,7 +153,7 @@
if (i >= byteCount) { if (i >= byteCount) {
this.start = i - byteCount; this.start = i - byteCount;
this.block = blocks[blockCount]; this.block = blocks[blockCount];
for (i = 0;i < blockCount;++i) { for (i = 0; i < blockCount; ++i) {
s[i] ^= blocks[i]; s[i] ^= blocks[i];
} }
f(s); f(s);
@ -166,12 +170,12 @@
blocks[i >> 2] |= this.padding[i & 3]; blocks[i >> 2] |= this.padding[i & 3];
if (this.lastByteIndex == this.byteCount) { if (this.lastByteIndex == this.byteCount) {
blocks[0] = blocks[blockCount]; blocks[0] = blocks[blockCount];
for (i = 1;i < blockCount + 1;++i) { for (i = 1; i < blockCount + 1; ++i) {
blocks[i] = 0; blocks[i] = 0;
} }
} }
blocks[blockCount - 1] |= 0x80000000; blocks[blockCount - 1] |= 0x80000000;
for (i = 0;i < blockCount;++i) { for (i = 0; i < blockCount; ++i) {
s[i] ^= blocks[i]; s[i] ^= blocks[i];
} }
f(s); f(s);
@ -180,18 +184,18 @@
Keccak.prototype.toString = Keccak.prototype.hex = function () { Keccak.prototype.toString = Keccak.prototype.hex = function () {
this.finalize(); 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; extraBytes = this.extraBytes, i = 0, j = 0;
var hex = '', block; var hex = '', block;
while (j < outputBlocks) { while (j < outputBlocks) {
for (i = 0;i < blockCount && j < outputBlocks;++i, ++j) { for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) {
block = s[i]; block = s[i];
hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F] + hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F] +
HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F] + HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F] +
HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F] + HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F] +
HEX_CHARS[(block >> 28) & 0x0F] + HEX_CHARS[(block >> 24) & 0x0F]; HEX_CHARS[(block >> 28) & 0x0F] + HEX_CHARS[(block >> 24) & 0x0F];
} }
if (j % blockCount == 0) { if (j % blockCount === 0) {
f(s); f(s);
i = 0; i = 0;
} }
@ -214,7 +218,7 @@
Keccak.prototype.arrayBuffer = function () { Keccak.prototype.arrayBuffer = function () {
this.finalize(); 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; extraBytes = this.extraBytes, i = 0, j = 0;
var bytes = this.outputBits >> 3; var bytes = this.outputBits >> 3;
var buffer; var buffer;
@ -225,10 +229,10 @@
} }
var array = new Uint32Array(buffer); var array = new Uint32Array(buffer);
while (j < outputBlocks) { 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]; array[j] = s[i];
} }
if (j % blockCount == 0) { if (j % blockCount === 0) {
f(s); f(s);
} }
} }
@ -244,11 +248,11 @@
Keccak.prototype.digest = Keccak.prototype.array = function () { Keccak.prototype.digest = Keccak.prototype.array = function () {
this.finalize(); 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; extraBytes = this.extraBytes, i = 0, j = 0;
var array = [], offset, block; var array = [], offset, block;
while (j < outputBlocks) { while (j < outputBlocks) {
for (i = 0;i < blockCount && j < outputBlocks;++i, ++j) { for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) {
offset = j << 2; offset = j << 2;
block = s[i]; block = s[i];
array[offset] = block & 0xFF; array[offset] = block & 0xFF;
@ -256,7 +260,7 @@
array[offset + 2] = (block >> 16) & 0xFF; array[offset + 2] = (block >> 16) & 0xFF;
array[offset + 3] = (block >> 24) & 0xFF; array[offset + 3] = (block >> 24) & 0xFF;
} }
if (j % blockCount == 0) { if (j % blockCount === 0) {
f(s); f(s);
} }
} }
@ -277,11 +281,11 @@
}; };
var f = function (s) { var f = function (s) {
var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, 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, 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, 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; 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]; c0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40];
c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41]; c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41];
c2 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42]; c2 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42];
@ -459,13 +463,13 @@
s[0] ^= RC[n]; s[0] ^= RC[n];
s[1] ^= RC[n + 1]; s[1] ^= RC[n + 1];
} }
} };
if (COMMON_JS) { if (COMMON_JS) {
module.exports = methods; module.exports = methods;
} else if (root) { } else {
for (var key in methods) { for (var i = 0; i < methodNames.length; ++i) {
root[key] = methods[key]; root[methodNames[i]] = methods[methodNames[i]];
} }
} }
}(this)); })();

@ -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'); expect = require('expect.js');
var sha3 = require('../src/sha3.js'); var sha3 = require('../src/sha3.js');
keccak_512 = sha3.keccak_512; keccak_512 = sha3.keccak_512;
@ -27,7 +50,10 @@ keccak_224 = null;
shake_128 = null; shake_128 = null;
shake_256 = 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('../src/sha3.js');
require('./test.js'); require('./test.js');
require('./test-shake.js'); require('./test-shake.js');

Loading…
Cancel
Save