mirror of
https://github.com/emn178/js-sha3.git
synced 2026-08-12 19:41:36 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d6193d7f2 | ||
|
|
8ae70989ae | ||
|
|
035642212a | ||
|
|
9f8c2a3fb3 | ||
|
|
ec2a9948f9 | ||
|
|
f34e33dc99 | ||
|
|
97e70fed97 |
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
|
/node_modules/
|
||||||
/tests/
|
/tests/
|
||||||
node_modules/
|
|
||||||
|
|||||||
+3
-3
@@ -1,3 +1,3 @@
|
|||||||
node_modules/
|
/node_modules/
|
||||||
covreporter
|
/covreporter/
|
||||||
my_test
|
/my_test/
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
my_test
|
|
||||||
covreporter
|
|
||||||
+58
-38
@@ -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.
|
## v0.3.1 / 2015-05-22
|
||||||
* Fixed shake output incorrect when it's greater than 1088.
|
### 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.
|
## v0.2.0 / 2015-04-04
|
||||||
* Add shake alogirthms.
|
### 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
|
## v0.1.0 / 2015-02-23
|
||||||
|
### Added
|
||||||
* Support byte array and ArrayBuffer input.
|
- First version implementation.
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
# js-sha3
|
# js-sha3
|
||||||
|
|
||||||
[](https://travis-ci.org/emn178/js-sha3)
|
[](https://travis-ci.org/emn178/js-sha3)
|
||||||
[](https://coveralls.io/r/emn178/js-sha3?branch=master)
|
[](https://coveralls.io/r/emn178/js-sha3?branch=master)
|
||||||
|
[](https://nodei.co/npm/js-sha3/)
|
||||||
|
|
||||||
A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.
|
A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.
|
||||||
|
|
||||||
## Notice
|
## 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
|
## Demo
|
||||||
[SHA3-512 Online](http://emn178.github.io/online-tools/sha3_512.html)
|
[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-384 Online](http://emn178.github.io/online-tools/keccak_384.html)
|
||||||
[Keccak-256 Online](http://emn178.github.io/online-tools/keccak_256.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)
|
[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
|
## Download
|
||||||
[Compress](https://raw.github.com/emn178/js-sha3/master/build/sha3.min.js)
|
[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_128 = require('js-sha3').shake_128;
|
||||||
shake_256 = require('js-sha3').shake_256;
|
shake_256 = require('js-sha3').shake_256;
|
||||||
```
|
```
|
||||||
|
If you use TypeScript, you can import like this:
|
||||||
|
```TypeScript
|
||||||
|
import { sha3_512 } from 'js-sha3';
|
||||||
|
```
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
Code
|
Code
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "js-sha3",
|
"name": "js-sha3",
|
||||||
"version": "0.5.4",
|
"version": "0.5.7",
|
||||||
"main": ["src/sha3.js"],
|
"main": ["src/sha3.js"],
|
||||||
"ignore": [
|
"ignore": [
|
||||||
"samples",
|
"samples",
|
||||||
|
|||||||
Vendored
+2
-15
File diff suppressed because one or more lines are too long
Vendored
+92
@@ -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
@@ -1,16 +1,21 @@
|
|||||||
{
|
{
|
||||||
"name": "js-sha3",
|
"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.",
|
"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",
|
||||||
|
"types": "index.d.ts",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"expect.js": "~0.3.1",
|
"expect.js": "~0.3.1",
|
||||||
"jscoverage": "~0.5.9",
|
"jscoverage": "~0.5.9",
|
||||||
"mocha": "~2.3.2"
|
"mocha": "~2.3.2",
|
||||||
|
"uglifyjs": "~2.4.10"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha tests/node-test.js -r jscoverage",
|
"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": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
+54
-50
@@ -1,43 +1,45 @@
|
|||||||
/**
|
/**
|
||||||
* [js-sha3]{@link https://github.com/emn178/js-sha3}
|
* [js-sha3]{@link https://github.com/emn178/js-sha3}
|
||||||
*
|
*
|
||||||
* @version 0.5.4
|
* @version 0.5.7
|
||||||
* @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];
|
||||||
var SHAKE_BITS = [128, 256];
|
var SHAKE_BITS = [128, 256];
|
||||||
var OUTPUT_TYPES = ['hex', 'buffer', 'array'];
|
var OUTPUT_TYPES = ['hex', 'buffer', 'arrayBuffer', 'array'];
|
||||||
|
|
||||||
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,15 +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;
|
||||||
var createMethod = algorithm.createMethod;
|
for (var j = 0; j < bits.length; ++j) {
|
||||||
for (var j = 0;j < bits.length;++j) {
|
var methodName = algorithm.name +'_' + bits[j];
|
||||||
var method = algorithm.createMethod(bits[j], algorithm.padding);
|
methodNames.push(methodName);
|
||||||
methods[algorithm.name +'_' + bits[j]] = method;
|
methods[methodName] = algorithm.createMethod(bits[j], algorithm.padding);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,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.constructor == root.ArrayBuffer) {
|
if (notString && message.constructor === 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];
|
||||||
@@ -151,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,14 +168,14 @@
|
|||||||
Keccak.prototype.finalize = function () {
|
Keccak.prototype.finalize = function () {
|
||||||
var blocks = this.blocks, i = this.lastByteIndex, blockCount = this.blockCount, s = this.s;
|
var blocks = this.blocks, i = this.lastByteIndex, blockCount = this.blockCount, s = this.s;
|
||||||
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);
|
||||||
@@ -182,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;
|
||||||
}
|
}
|
||||||
@@ -213,10 +215,10 @@
|
|||||||
return hex;
|
return hex;
|
||||||
};
|
};
|
||||||
|
|
||||||
Keccak.prototype.buffer = 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;
|
||||||
@@ -227,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,14 +243,16 @@
|
|||||||
return buffer;
|
return buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Keccak.prototype.buffer = Keccak.prototype.arrayBuffer;
|
||||||
|
|
||||||
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));
|
})();
|
||||||
|
|||||||
+28
-1
@@ -1,4 +1,28 @@
|
|||||||
expect = require('expect.js');
|
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');
|
var sha3 = require('../src/sha3.js');
|
||||||
keccak_512 = sha3.keccak_512;
|
keccak_512 = sha3.keccak_512;
|
||||||
keccak_384 = sha3.keccak_384;
|
keccak_384 = sha3.keccak_384;
|
||||||
@@ -27,7 +51,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');
|
||||||
|
|||||||
+9
-6
@@ -2,7 +2,7 @@
|
|||||||
Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () {
|
Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () {
|
||||||
var array = new Uint8Array(this);
|
var array = new Uint8Array(this);
|
||||||
var hex = '';
|
var hex = '';
|
||||||
for (var i = 0;i < array.length;++i) {
|
for (var i = 0; i < array.length; ++i) {
|
||||||
var c = array[i].toString('16');
|
var c = array[i].toString('16');
|
||||||
hex += c.length == 1 ? '0' + c : c;
|
hex += c.length == 1 ? '0' + c : c;
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@
|
|||||||
},
|
},
|
||||||
'special length': {
|
'special length': {
|
||||||
'bce9da5b408846edd5bec9f26c2dee9bd835215c3f2b3876197067d87bc4d1af0cd97f94fda59761a0d804fe82383be2c6c4886fbb82e005fcf899449029f221' :'012345678901234567890123456789012345678901234567890123456789012345678901',
|
'bce9da5b408846edd5bec9f26c2dee9bd835215c3f2b3876197067d87bc4d1af0cd97f94fda59761a0d804fe82383be2c6c4886fbb82e005fcf899449029f221' :'012345678901234567890123456789012345678901234567890123456789012345678901',
|
||||||
'8bdcb85e6b52c29fafac0d3daf65492f2e3499e066da1a095a65eb1144849a26b2790a8b39c2a7fb747456f749391d953841a61cb13289f9806f04981c180a86' :'01234567890123456789012345678901234567890123456789012345678901234567890'
|
'8bdcb85e6b52c29fafac0d3daf65492f2e3499e066da1a095a65eb1144849a26b2790a8b39c2a7fb747456f749391d953841a61cb13289f9806f04981c180a86' :'01234567890123456789012345678901234567890123456789012345678901234567890'
|
||||||
},
|
},
|
||||||
'Array': {
|
'Array': {
|
||||||
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': [],
|
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': [],
|
||||||
@@ -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])
|
'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': {
|
'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: {
|
sha3_384: {
|
||||||
@@ -239,8 +240,9 @@
|
|||||||
runTestCases(methods, testCases);
|
runTestCases(methods, testCases);
|
||||||
|
|
||||||
describe('sha3_512', function () {
|
describe('sha3_512', function () {
|
||||||
context('#buffer', function () {
|
context('#arrayBuffer', function () {
|
||||||
it('should be equal', function () {
|
it('should be equal', function () {
|
||||||
|
expect(sha3_512.arrayBuffer('').toHexString()).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26');
|
||||||
expect(sha3_512.buffer('').toHexString()).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26');
|
expect(sha3_512.buffer('').toHexString()).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -262,7 +264,7 @@
|
|||||||
it('should be equal', function () {
|
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 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();
|
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]]);
|
hash.update([bytes[i]]);
|
||||||
}
|
}
|
||||||
expect(hash.hex()).to.be('01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450');
|
expect(hash.hex()).to.be('01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450');
|
||||||
@@ -277,7 +279,7 @@
|
|||||||
expect(keccak_512('01234567890123456789012345678901234567890123456789012345678901234567890')).to.be('3173e7abc754a0b2909410d78986428a9183e996864af02f421d273d9fa1b4e4a5b14e2998b20767712f53a01ff8f6ae2c3e71e51e2c0f24257b03e6da09eb77');
|
expect(keccak_512('01234567890123456789012345678901234567890123456789012345678901234567890')).to.be('3173e7abc754a0b2909410d78986428a9183e996864af02f421d273d9fa1b4e4a5b14e2998b20767712f53a01ff8f6ae2c3e71e51e2c0f24257b03e6da09eb77');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context('when Array', function () {
|
context('when Array', function () {
|
||||||
it('should be equal', function () {
|
it('should be equal', function () {
|
||||||
expect(keccak_512([])).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
expect(keccak_512([])).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
||||||
@@ -300,6 +302,7 @@
|
|||||||
|
|
||||||
context('when output ArrayBuffer', function () {
|
context('when output ArrayBuffer', function () {
|
||||||
it('should be equal', function () {
|
it('should be equal', function () {
|
||||||
|
expect(keccak_512.arrayBuffer('').toHexString()).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
||||||
expect(keccak_512.buffer('').toHexString()).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
expect(keccak_512.buffer('').toHexString()).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user