Compare commits

...
7 Commits
Author SHA1 Message Date
Chen, Yi-Cyuan 5d6193d7f2 Fixed ArrayBuffer detection in old browsers. 2016-12-30 10:17:55 +08:00
Chen, Yi-Cyuan 8ae70989ae Fixed ArrayBuffer dosen't work in Webpack. 2016-12-29 09:31:53 +08:00
emn178andGitHub 035642212a Merge pull request #10 from happylynx/arraybuffer-fix
webpack & ArrayBuffer argument fix
2016-12-29 08:48:13 +08:00
happylynx 9f8c2a3fb3 Fix of recognition of ArrayBuffer parameters
If js-sha3 is loaded using 'webpack' loader then the top-most
fuction is not called with global object (`global` in node,
`window` in browsers) and thus `root.ArrayBuffer` is undefined.

This patch introduces usage of `ArrayBuffer` without `root`
prefix similary to how e.g. `Uint8Array` is already used.
2016-12-27 20:18:36 +01:00
happylynx ec2a9948f9 Test of non-empty ArrayBuffer added 2016-12-27 20:02:45 +01:00
Yi-Cyuan f34e33dc99 update README 2016-09-26 10:55:14 +08:00
Yi-Cyuan 97e70fed97 Added
- TypeScript support.
- ArrayBuffer method.
Deprecated
- Buffer method.
2016-09-26 10:46:34 +08:00
12 changed files with 266 additions and 122 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
/node_modules/
/tests/
node_modules/
+3 -3
View File
@@ -1,3 +1,3 @@
node_modules/
covreporter
my_test
/node_modules/
/covreporter/
/my_test/
-2
View File
@@ -1,2 +0,0 @@
my_test
covreporter
+58 -38
View File
@@ -1,54 +1,74 @@
# v0.5.4 / 2016-09-12
# Change Log
* Added CommonJS detection.
## v0.5.7 / 2016-12-30
### Fixed
- ArrayBuffer detection in old browsers.
# v0.5.3 / 2016-09-08
## v0.5.6 / 2016-12-29
### Fixed
- ArrayBuffer dosen't work in Webpack.
* Added some files to npm package.
## v0.5.5 / 2016-09-26
### Added
- TypeScript support.
- ArrayBuffer method.
### Deprecated
- Buffer method.
# v0.5.2 / 2016-06-06
## v0.5.4 / 2016-09-12
### Fixed
- CommonJS detection.
* Fixed shake output incorrect in the special length.
## v0.5.3 / 2016-09-08
### Added
- Some missing files to npm package.
# v0.5.1 / 2015-10-27
## v0.5.2 / 2016-06-06
### Fixed
- Shake output incorrect in the special length.
* Update package.json and bower.json.
## v0.5.1 / 2015-10-27
### Fixed
- Version in package.json and bower.json.
# v0.5.0 / 2015-09-23
## v0.5.0 / 2015-09-23
### Added
- Hash object with create/update interface.
* Support update interface.
## v0.4.1 / 2015-09-18
### Added
- Integer array output.
### Fixed
- Shake output incorrect when it's greater than 1088.
# v0.4.1 / 2015-09-18
## v0.4.0 / 2015-09-17
### Added
- ArrayBuffer output.
- Shake alogirthms.
* Support to output Integer Array.
* Fixed shake output incorrect when it's greater than 1088.
## v0.3.1 / 2015-05-22
### Fixed
- Some bugs.
# v0.4.0 / 2015-09-17
## v0.3.0 / 2015-05-21
### Added
- Integer array input.
- ArrayBuffer input.
* Support to output ArrayBuffer.
* Add shake alogirthms.
## v0.2.0 / 2015-04-04
### Added
- NIST's May 2014 SHA-3 version.
### Changed
- Rename original methods to keccak.
# v0.3.1 / 2015-05-22
## v0.1.2 / 2015-02-27
### Changed
- Improve performance.
* Fixed bugs.
## v0.1.1 / 2015-02-26
### Changed
- Improve performance.
# v0.3.0 / 2015-05-21
* Support byte array and ArrayBuffer input.
# v0.2.0 / 2015-04-04
* Implement NIST's May 2014 SHA-3 version.
* Rename original methods to keccak.
# v0.1.2 / 2015-02-27
* Improve performance.
# v0.1.1 / 2015-02-26
* Improve performance.
# v0.1.0 / 2015-02-23
* Initial release
## v0.1.0 / 2015-02-23
### Added
- First version implementation.
+9 -1
View File
@@ -2,11 +2,13 @@
[![Build Status](https://travis-ci.org/emn178/js-sha3.svg?branch=master)](https://travis-ci.org/emn178/js-sha3)
[![Coverage Status](https://coveralls.io/repos/emn178/js-sha3/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-sha3?branch=master)
[![NPM](https://nodei.co/npm/js-sha3.png?stars&downloads)](https://nodei.co/npm/js-sha3/)
A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.
## Notice
Sha3 methods has been renamed to keccak since v0.2.0. It means that sha3 methods of v0.1.x are equal to keccak methods of v0.2.x and later.
* Sha3 methods has been renamed to keccak since v0.2.0. It means that sha3 methods of v0.1.x are equal to keccak methods of v0.2.x and later.
* `buffer` method is deprecated. This maybe confuse with Buffer in node.js. Please use `arrayBuffer` instead.
## Demo
[SHA3-512 Online](http://emn178.github.io/online-tools/sha3_512.html)
@@ -17,6 +19,8 @@ Sha3 methods has been renamed to keccak since v0.2.0. It means that sha3 methods
[Keccak-384 Online](http://emn178.github.io/online-tools/keccak_384.html)
[Keccak-256 Online](http://emn178.github.io/online-tools/keccak_256.html)
[Keccak-224 Online](http://emn178.github.io/online-tools/keccak_224.html)
[Shake-128 Online](http://emn178.github.io/online-tools/shake_128.html)
[Shake-256 Online](http://emn178.github.io/online-tools/shake_256.html)
## Download
[Compress](https://raw.github.com/emn178/js-sha3/master/build/sha3.min.js)
@@ -81,6 +85,10 @@ keccak_224 = require('js-sha3').keccak_224;
shake_128 = require('js-sha3').shake_128;
shake_256 = require('js-sha3').shake_256;
```
If you use TypeScript, you can import like this:
```TypeScript
import { sha3_512 } from 'js-sha3';
```
## Example
Code
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "js-sha3",
"version": "0.5.4",
"version": "0.5.7",
"main": ["src/sha3.js"],
"ignore": [
"samples",
+2 -15
View File
File diff suppressed because one or more lines are too long
Vendored
+92
View File
@@ -0,0 +1,92 @@
type Message = string | number[] | ArrayBuffer | Uint8Array;
interface Hasher {
/**
* Update hash
*
* @param message The message you want to hash.
*/
update(message: Message): Hasher;
/**
* Return hash in hex string.
*/
hex(): string;
/**
* Return hash in hex string.
*/
toString(): string;
/**
* Return hash in ArrayBuffer.
*/
arrayBuffer(): ArrayBuffer;
/**
* Return hash in integer array.
*/
digest(): number[];
/**
* Return hash in integer array.
*/
array(): number[];
}
interface Hash {
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
*/
(message: Message): string;
/**
* Create a hash object.
*/
create(): Hasher;
/**
* Create a hash object and hash message.
*
* @param message The message you want to hash.
*/
update(message: Message): Hasher;
}
interface ShakeHash {
/**
* Hash and return hex string.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
(message: Message, outputBits: number): string;
/**
* Create a hash object.
*
* @param outputBits The length of output.
*/
create(outputBits: number): Hasher;
/**
* Create a hash object and hash message.
*
* @param message The message you want to hash.
* @param outputBits The length of output.
*/
update(message: Message, outputBits: number): Hasher;
}
export var sha3_512: Hash;
export var sha3_384: Hash;
export var sha3_256: Hash;
export var sha3_224: Hash;
export var keccak_512: Hash;
export var keccak_384: Hash;
export var keccak_256: Hash;
export var keccak_224: Hash;
export var shake_128: ShakeHash;
export var shake_256: ShakeHash;
+8 -3
View File
@@ -1,16 +1,21 @@
{
"name": "js-sha3",
"version": "0.5.4",
"version": "0.5.7",
"description": "A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha3.js",
"typings": "index",
"types": "index.d.ts",
"devDependencies": {
"expect.js": "~0.3.1",
"jscoverage": "~0.5.9",
"mocha": "~2.3.2"
"mocha": "~2.3.2",
"uglifyjs": "~2.4.10"
},
"scripts": {
"test": "mocha tests/node-test.js -r jscoverage",
"coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls"
"report": "mocha tests/node-test.js -r jscoverage --covout=html",
"coveralls": "mocha tests/node-test.js -R mocha-lcov-reporter -r jscoverage | coveralls",
"build": "uglifyjs src/sha3.js --compress --mangle --comments --output build/sha3.min.js"
},
"repository": {
"type": "git",
+43 -39
View File
@@ -1,19 +1,21 @@
/**
* [js-sha3]{@link https://github.com/emn178/js-sha3}
*
* @version 0.5.4
* @version 0.5.7
* @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];
@@ -26,18 +28,18 @@
2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648];
var BITS = [224, 256, 384, 512];
var SHAKE_BITS = [128, 256];
var OUTPUT_TYPES = ['hex', 'buffer', 'array'];
var OUTPUT_TYPES = ['hex', 'buffer', 'arrayBuffer', 'array'];
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,15 +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;
var createMethod = algorithm.createMethod;
for (var j = 0;j < bits.length;++j) {
var method = algorithm.createMethod(bits[j], algorithm.padding);
methods[algorithm.name +'_' + bits[j]] = method;
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);
}
}
@@ -101,14 +103,14 @@
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.constructor == root.ArrayBuffer) {
var notString = typeof message !== 'string';
if (notString && message.constructor === ArrayBuffer) {
message = new Uint8Array(message);
}
var length = message.length, blocks = this.blocks, byteCount = this.byteCount,
@@ -118,16 +120,16 @@
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];
@@ -151,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,14 +168,14 @@
Keccak.prototype.finalize = function () {
var blocks = this.blocks, i = this.lastByteIndex, blockCount = this.blockCount, s = this.s;
blocks[i >> 2] |= this.padding[i & 3];
if (this.lastByteIndex == this.byteCount) {
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);
@@ -186,14 +188,14 @@
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;
}
@@ -213,7 +215,7 @@
return hex;
};
Keccak.prototype.buffer = function () {
Keccak.prototype.arrayBuffer = function () {
this.finalize();
var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks,
@@ -227,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);
}
}
@@ -241,6 +243,8 @@
return buffer;
};
Keccak.prototype.buffer = Keccak.prototype.arrayBuffer;
Keccak.prototype.digest = Keccak.prototype.array = function () {
this.finalize();
@@ -248,7 +252,7 @@
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);
}
}
@@ -281,7 +285,7 @@
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));
})();
+28 -1
View File
@@ -1,4 +1,28 @@
expect = require('expect.js');
// Node.js env
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;
keccak_384 = sha3.keccak_384;
@@ -27,7 +51,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');
+7 -4
View File
@@ -2,7 +2,7 @@
Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () {
var array = new Uint8Array(this);
var hex = '';
for (var i = 0;i < array.length;++i) {
for (var i = 0; i < array.length; ++i) {
var c = array[i].toString('16');
hex += c.length == 1 ? '0' + c : c;
}
@@ -98,7 +98,8 @@
'01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])
},
'ArrayBuffer': {
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': new ArrayBuffer(0)
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': new ArrayBuffer(0),
'01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]).buffer
}
},
sha3_384: {
@@ -239,8 +240,9 @@
runTestCases(methods, testCases);
describe('sha3_512', function () {
context('#buffer', function () {
context('#arrayBuffer', function () {
it('should be equal', function () {
expect(sha3_512.arrayBuffer('').toHexString()).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26');
expect(sha3_512.buffer('').toHexString()).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26');
});
});
@@ -262,7 +264,7 @@
it('should be equal', function () {
var bytes = [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103];
var hash = sha3_512.create();
for (var i = 0;i < bytes.length;++i) {
for (var i = 0; i < bytes.length; ++i) {
hash.update([bytes[i]]);
}
expect(hash.hex()).to.be('01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450');
@@ -300,6 +302,7 @@
context('when output ArrayBuffer', function () {
it('should be equal', function () {
expect(keccak_512.arrayBuffer('').toHexString()).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
expect(keccak_512.buffer('').toHexString()).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
});
});