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 | ||
|
|
bb08d1fc64 | ||
|
|
f0ff77ca47 |
+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,4 +0,0 @@
|
|||||||
my_test
|
|
||||||
covreporter
|
|
||||||
build
|
|
||||||
tests
|
|
||||||
+3
-5
@@ -1,10 +1,8 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- "4.1"
|
- "0.12.15"
|
||||||
- "4.0"
|
- "4.5"
|
||||||
- "0.12"
|
- "6.5.0"
|
||||||
- "0.11"
|
|
||||||
- "0.10"
|
|
||||||
before_install:
|
before_install:
|
||||||
- npm install coveralls
|
- npm install coveralls
|
||||||
- npm install mocha-lcov-reporter
|
- npm install mocha-lcov-reporter
|
||||||
|
|||||||
+58
-30
@@ -1,46 +1,74 @@
|
|||||||
# v0.5.2 / 2016-06-06
|
# Change Log
|
||||||
|
|
||||||
* Fixed shake output incorrect in the special length.
|
## v0.5.7 / 2016-12-30
|
||||||
|
### Fixed
|
||||||
|
- ArrayBuffer detection in old browsers.
|
||||||
|
|
||||||
# v0.5.1 / 2015-10-27
|
## v0.5.6 / 2016-12-29
|
||||||
|
### Fixed
|
||||||
|
- ArrayBuffer dosen't work in Webpack.
|
||||||
|
|
||||||
* Update package.json and bower.json.
|
## v0.5.5 / 2016-09-26
|
||||||
|
### Added
|
||||||
|
- TypeScript support.
|
||||||
|
- ArrayBuffer method.
|
||||||
|
### Deprecated
|
||||||
|
- Buffer method.
|
||||||
|
|
||||||
# v0.5.0 / 2015-09-23
|
## v0.5.4 / 2016-09-12
|
||||||
|
### Fixed
|
||||||
|
- CommonJS detection.
|
||||||
|
|
||||||
* Support update interface.
|
## v0.5.3 / 2016-09-08
|
||||||
|
### Added
|
||||||
|
- Some missing files to npm package.
|
||||||
|
|
||||||
# v0.4.1 / 2015-09-18
|
## v0.5.2 / 2016-06-06
|
||||||
|
### Fixed
|
||||||
|
- Shake output incorrect in the special length.
|
||||||
|
|
||||||
* Support to output Integer Array.
|
## v0.5.1 / 2015-10-27
|
||||||
* Fixed shake output incorrect when it's greater than 1088.
|
### Fixed
|
||||||
|
- Version in package.json and bower.json.
|
||||||
|
|
||||||
# v0.4.0 / 2015-09-17
|
## v0.5.0 / 2015-09-23
|
||||||
|
### Added
|
||||||
|
- Hash object with create/update interface.
|
||||||
|
|
||||||
* Support to output ArrayBuffer.
|
## v0.4.1 / 2015-09-18
|
||||||
* Add shake alogirthms.
|
### Added
|
||||||
|
- Integer array output.
|
||||||
|
### Fixed
|
||||||
|
- Shake output incorrect when it's greater than 1088.
|
||||||
|
|
||||||
# v0.3.1 / 2015-05-22
|
## v0.4.0 / 2015-09-17
|
||||||
|
### Added
|
||||||
|
- ArrayBuffer output.
|
||||||
|
- Shake alogirthms.
|
||||||
|
|
||||||
* Fixed bugs.
|
## v0.3.1 / 2015-05-22
|
||||||
|
### Fixed
|
||||||
|
- Some bugs.
|
||||||
|
|
||||||
# v0.3.0 / 2015-05-21
|
## v0.3.0 / 2015-05-21
|
||||||
|
### Added
|
||||||
|
- Integer array input.
|
||||||
|
- ArrayBuffer input.
|
||||||
|
|
||||||
* Support byte array and ArrayBuffer input.
|
## v0.2.0 / 2015-04-04
|
||||||
|
### Added
|
||||||
|
- NIST's May 2014 SHA-3 version.
|
||||||
|
### Changed
|
||||||
|
- Rename original methods to keccak.
|
||||||
|
|
||||||
# v0.2.0 / 2015-04-04
|
## v0.1.2 / 2015-02-27
|
||||||
|
### Changed
|
||||||
|
- Improve performance.
|
||||||
|
|
||||||
* Implement NIST's May 2014 SHA-3 version.
|
## v0.1.1 / 2015-02-26
|
||||||
* Rename original methods to keccak.
|
### Changed
|
||||||
|
- Improve performance.
|
||||||
|
|
||||||
# v0.1.2 / 2015-02-27
|
## v0.1.0 / 2015-02-23
|
||||||
|
### Added
|
||||||
* Improve performance.
|
- First version implementation.
|
||||||
|
|
||||||
# v0.1.1 / 2015-02-26
|
|
||||||
|
|
||||||
* Improve performance.
|
|
||||||
|
|
||||||
# v0.1.0 / 2015-02-23
|
|
||||||
|
|
||||||
* Initial release
|
|
||||||
|
|||||||
@@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
[](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
|
||||||
@@ -209,73 +217,6 @@ sha3_512(new Uint8Array([]));
|
|||||||
[UTF8](http://jsperf.com/sha3/5)
|
[UTF8](http://jsperf.com/sha3/5)
|
||||||
[ASCII](http://jsperf.com/sha3/4)
|
[ASCII](http://jsperf.com/sha3/4)
|
||||||
|
|
||||||
## Extensions
|
|
||||||
### jQuery
|
|
||||||
If you prefer jQuery style, you can add following code to add a jQuery extension.
|
|
||||||
|
|
||||||
Code
|
|
||||||
```JavaScript
|
|
||||||
jQuery.sha3_512 = sha3_512;
|
|
||||||
jQuery.sha3_384 = sha3_384;
|
|
||||||
jQuery.sha3_256 = sha3_256;
|
|
||||||
jQuery.sha3_224 = sha3_224;
|
|
||||||
jQuery.keccak_512 = keccak_512;
|
|
||||||
jQuery.keccak_384 = keccak_384;
|
|
||||||
jQuery.keccak_256 = keccak_256;
|
|
||||||
jQuery.keccak_224 = keccak_224;
|
|
||||||
```
|
|
||||||
And then you could use like this:
|
|
||||||
```JavaScript
|
|
||||||
$.sha3_512('message');
|
|
||||||
$.sha3_384('message');
|
|
||||||
$.sha3_256('message');
|
|
||||||
$.sha3_224('message');
|
|
||||||
$.keccak_512('message');
|
|
||||||
$.keccak_384('message');
|
|
||||||
$.keccak_256('message');
|
|
||||||
$.keccak_224('message');
|
|
||||||
```
|
|
||||||
### Prototype
|
|
||||||
If you prefer prototype style, you can add following code to add a prototype extension.
|
|
||||||
|
|
||||||
Code
|
|
||||||
```JavaScript
|
|
||||||
String.prototype.sha3_512 = function() {
|
|
||||||
return sha3_512(this);
|
|
||||||
};
|
|
||||||
String.prototype.sha3_384 = function() {
|
|
||||||
return sha3_384(this);
|
|
||||||
};
|
|
||||||
String.prototype.sha3_256 = function() {
|
|
||||||
return sha3_256(this);
|
|
||||||
};
|
|
||||||
String.prototype.sha3_224 = function() {
|
|
||||||
return sha3_224(this);
|
|
||||||
};
|
|
||||||
String.prototype.keccak_512 = function() {
|
|
||||||
return keccak_512(this);
|
|
||||||
};
|
|
||||||
String.prototype.keccak_384 = function() {
|
|
||||||
return keccak_384(this);
|
|
||||||
};
|
|
||||||
String.prototype.keccak_256 = function() {
|
|
||||||
return keccak_256(this);
|
|
||||||
};
|
|
||||||
String.prototype.keccak_224 = function() {
|
|
||||||
return keccak_224(this);
|
|
||||||
};
|
|
||||||
```
|
|
||||||
And then you could use like this:
|
|
||||||
```JavaScript
|
|
||||||
'message'.sha3_512();
|
|
||||||
'message'.sha3_384();
|
|
||||||
'message'.sha3_256();
|
|
||||||
'message'.sha3_224();
|
|
||||||
'message'.keccak_512();
|
|
||||||
'message'.keccak_384();
|
|
||||||
'message'.keccak_256();
|
|
||||||
'message'.keccak_224();
|
|
||||||
```
|
|
||||||
## License
|
## License
|
||||||
The project is released under the [MIT license](http://www.opensource.org/licenses/MIT).
|
The project is released under the [MIT license](http://www.opensource.org/licenses/MIT).
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "js-sha3",
|
"name": "js-sha3",
|
||||||
"version": "0.5.2",
|
"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.2",
|
"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",
|
||||||
|
|||||||
+43
-38
@@ -1,18 +1,21 @@
|
|||||||
/**
|
/**
|
||||||
* [js-sha3]{@link https://github.com/emn178/js-sha3}
|
* [js-sha3]{@link https://github.com/emn178/js-sha3}
|
||||||
*
|
*
|
||||||
* @version 0.5.2
|
* @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(module) != 'undefined';
|
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_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];
|
||||||
@@ -25,18 +28,18 @@
|
|||||||
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) {
|
||||||
@@ -47,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);
|
||||||
}
|
}
|
||||||
@@ -62,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);
|
||||||
}
|
}
|
||||||
@@ -75,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,14 +103,14 @@
|
|||||||
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,
|
||||||
@@ -117,16 +120,16 @@
|
|||||||
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];
|
||||||
@@ -150,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);
|
||||||
@@ -165,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);
|
||||||
@@ -185,14 +188,14 @@
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -212,7 +215,7 @@
|
|||||||
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,
|
||||||
@@ -226,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -240,6 +243,8 @@
|
|||||||
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();
|
||||||
|
|
||||||
@@ -247,7 +252,7 @@
|
|||||||
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;
|
||||||
@@ -255,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -458,13 +463,13 @@
|
|||||||
s[0] ^= RC[n];
|
s[0] ^= RC[n];
|
||||||
s[1] ^= RC[n + 1];
|
s[1] ^= RC[n + 1];
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
if (!root.JS_SHA3_TEST && NODE_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');
|
||||||
|
|||||||
+7
-4
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -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');
|
||||||
@@ -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