mirror of
https://github.com/emn178/js-sha3.git
synced 2026-08-13 03:51:54 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59caf17eca | ||
|
|
d6325442bd | ||
|
|
f293b5d508 | ||
|
|
dbb0ea401f | ||
|
|
5aecbd6968 | ||
|
|
b39d0910cb | ||
|
|
1c00d97268 | ||
|
|
fb7e6403d9 |
@@ -1,2 +0,0 @@
|
||||
/node_modules/
|
||||
/tests/
|
||||
+2
-2
@@ -1,3 +1,3 @@
|
||||
/node_modules/
|
||||
/covreporter/
|
||||
/my_test/
|
||||
/coverage/
|
||||
/.nyc_output/
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
/node_modules/
|
||||
/coverage/
|
||||
/.nyc_output/
|
||||
/tests/
|
||||
.travis.yml
|
||||
.npmignore
|
||||
bower.json
|
||||
+3
-5
@@ -1,12 +1,10 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.12.15"
|
||||
- "4.5"
|
||||
- "6.5.0"
|
||||
- "6.11.4"
|
||||
- "8.6.0"
|
||||
before_install:
|
||||
- npm install coveralls
|
||||
- npm install mocha-lcov-reporter
|
||||
script: npm run-script coveralls
|
||||
after_success: npm run coveralls
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
|
||||
@@ -1,5 +1,35 @@
|
||||
# Change Log
|
||||
|
||||
## v0.9.2 / 2023-09-16
|
||||
### Fixed
|
||||
- don't modify global Array and ArrayBuffer prototypes. #33
|
||||
- refactor: simplify formatMessage internal logic. #34
|
||||
|
||||
## v0.9.1 / 2023-08-31
|
||||
### Fixed
|
||||
- cSHAKE empty Array bug. #24
|
||||
|
||||
## v0.9.0 / 2023-08-30
|
||||
### Fixed
|
||||
- cSHAKE bug. #24
|
||||
- dependencies and security issues.
|
||||
|
||||
## v0.8.0 / 2018-08-05
|
||||
### Added
|
||||
- TypeScript definitions.
|
||||
|
||||
### Changed
|
||||
- throw error if update after finalize
|
||||
|
||||
## v0.7.0 / 2017-12-01
|
||||
### Added
|
||||
- AMD support.
|
||||
- support for web worker. #13
|
||||
|
||||
### Changed
|
||||
- throw error if input type is incorrect when cSHAKE and KMAC.
|
||||
- freeze hash after finalize.
|
||||
|
||||
## v0.6.1 / 2017-07-03
|
||||
### Fixed
|
||||
- Typo on variable kmac_256 type definition. #12
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
Copyright 2015-2017 Chen, Yi-Cyuan
|
||||
Copyright 2015-2023 Chen, Yi-Cyuan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.
|
||||
|
||||
## Notice
|
||||
* v0.8.0+ will throw an error if try to update hash after finalize.
|
||||
* 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.
|
||||
|
||||
@@ -54,10 +55,11 @@ kmac128('key', 'Message to hash', 256, 'customization');
|
||||
kmac256('key', 'Message to hash', 512, 'customization');
|
||||
|
||||
// Support ArrayBuffer output
|
||||
var buffer = keccak224.buffer('Message to hash');
|
||||
var arrayBuffer = keccak224.arrayBuffer('Message to hash');
|
||||
|
||||
// Support Array output
|
||||
var buffer = keccak224.array('Message to hash');
|
||||
var bytes = keccak224.digest('Message to hash');
|
||||
var bytes = keccak224.array('Message to hash');
|
||||
|
||||
// update hash
|
||||
sha3_512.update('Message ').update('to ').update('hash').hex();
|
||||
@@ -82,26 +84,46 @@ var hash = cshake128.create(256, 'function name', 'customization');
|
||||
// specify kmac key, output bits and customization when creating
|
||||
var hash = kmac128.create('key', 256, 'customization');
|
||||
```
|
||||
### Node.js
|
||||
If you use node.js, you should require the module first:
|
||||
```JavaScript
|
||||
sha3_512 = require('js-sha3').sha3_512;
|
||||
sha3_384 = require('js-sha3').sha3_384;
|
||||
sha3_256 = require('js-sha3').sha3_256;
|
||||
sha3_224 = require('js-sha3').sha3_224;
|
||||
keccak512 = require('js-sha3').keccak512;
|
||||
keccak384 = require('js-sha3').keccak384;
|
||||
keccak256 = require('js-sha3').keccak256;
|
||||
keccak224 = require('js-sha3').keccak224;
|
||||
shake128 = require('js-sha3').shake128;
|
||||
shake256 = require('js-sha3').shake256;
|
||||
cshake128 = require('js-sha3').cshake128;
|
||||
cshake256 = require('js-sha3').cshake256;
|
||||
kmac128 = require('js-sha3').kmac128;
|
||||
kmac256 = require('js-sha3').kmac256;
|
||||
const {
|
||||
sha3_512,
|
||||
sha3_384,
|
||||
sha3_256,
|
||||
sha3_224,
|
||||
keccak512,
|
||||
keccak384,
|
||||
keccak256,
|
||||
keccak224,
|
||||
shake128,
|
||||
shake256,
|
||||
cshake128,
|
||||
cshake256,
|
||||
kmac128,
|
||||
kmac25
|
||||
} = require('js-sha3');
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
If you use TypeScript, you can import like this:
|
||||
```TypeScript
|
||||
import { sha3_512 } from 'js-sha3';
|
||||
import {
|
||||
sha3_512,
|
||||
sha3_384,
|
||||
sha3_256,
|
||||
sha3_224,
|
||||
keccak512,
|
||||
keccak384,
|
||||
keccak256,
|
||||
keccak224,
|
||||
shake128,
|
||||
shake256,
|
||||
cshake128,
|
||||
cshake256,
|
||||
kmac128,
|
||||
kmac256
|
||||
} from 'js-sha3';
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "js-sha3",
|
||||
"version": "0.6.1",
|
||||
"version": "0.9.2",
|
||||
"main": ["src/sha3.js"],
|
||||
"ignore": [
|
||||
"samples",
|
||||
|
||||
Vendored
+3
-3
File diff suppressed because one or more lines are too long
Vendored
+149
@@ -42,6 +42,34 @@ interface Hash {
|
||||
*/
|
||||
(message: Message): string;
|
||||
|
||||
/**
|
||||
* Hash and return hex string.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
hex(message: Message): string;
|
||||
|
||||
/**
|
||||
* Hash and return ArrayBuffer.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
arrayBuffer(message: Message): ArrayBuffer;
|
||||
|
||||
/**
|
||||
* Hash and return integer array.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
digest(message: Message): number[];
|
||||
|
||||
/**
|
||||
* Hash and return integer array.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
*/
|
||||
array(message: Message): number[];
|
||||
|
||||
/**
|
||||
* Create a hash object.
|
||||
*/
|
||||
@@ -64,10 +92,43 @@ interface ShakeHash {
|
||||
*/
|
||||
(message: Message, outputBits: number): string;
|
||||
|
||||
/**
|
||||
* Hash and return hex string.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
*/
|
||||
hex(message: Message, outputBits: number): string;
|
||||
|
||||
/**
|
||||
* Hash and return ArrayBuffer.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
*/
|
||||
arrayBuffer(message: Message, outputBits: number): ArrayBuffer;
|
||||
|
||||
/**
|
||||
* Hash and return integer array.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
*/
|
||||
digest(message: Message, outputBits: number): number[];
|
||||
|
||||
/**
|
||||
* Hash and return integer array.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
*/
|
||||
array(message: Message, outputBits: number): number[];
|
||||
|
||||
/**
|
||||
* Create a hash object.
|
||||
*
|
||||
* @param outputBits The length of output.
|
||||
* @param outputBits The length of output.
|
||||
*/
|
||||
create(outputBits: number): Hasher;
|
||||
|
||||
@@ -91,6 +152,54 @@ interface CshakeHash {
|
||||
*/
|
||||
(message: Message, outputBits: number, functionName: Message, customization: Message): string;
|
||||
|
||||
/**
|
||||
* Hash and return hex string.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
* @param functionName The function name string.
|
||||
* @param customization The customization string.
|
||||
*/
|
||||
hex(message: Message, outputBits: number, functionName: Message, customization: Message): string;
|
||||
|
||||
/**
|
||||
* Hash and return ArrayBuffer.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
* @param functionName The function name string.
|
||||
* @param customization The customization string.
|
||||
*/
|
||||
arrayBuffer(message: Message, outputBits: number, functionName: Message, customization: Message): ArrayBuffer;
|
||||
|
||||
/**
|
||||
* Hash and return integer array.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
* @param functionName The function name string.
|
||||
* @param customization The customization string.
|
||||
*/
|
||||
digest(message: Message, outputBits: number, functionName: Message, customization: Message): number[];
|
||||
|
||||
/**
|
||||
* Hash and return integer array.
|
||||
*
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
* @param functionName The function name string.
|
||||
* @param customization The customization string.
|
||||
*/
|
||||
array(message: Message, outputBits: number, functionName: Message, customization: Message): number[];
|
||||
|
||||
/**
|
||||
* Create a hash object.
|
||||
*
|
||||
* @param outputBits The length of output.
|
||||
* @param outputBits The length of output.
|
||||
*/
|
||||
create(outputBits: number): Hasher;
|
||||
|
||||
/**
|
||||
* Create a hash object.
|
||||
*
|
||||
@@ -122,6 +231,46 @@ interface KmacHash {
|
||||
*/
|
||||
(key: Message, message: Message, outputBits: number, customization: Message): string;
|
||||
|
||||
/**
|
||||
* Hash and return hex string.
|
||||
*
|
||||
* @param key The key string.
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
* @param customization The customization string.
|
||||
*/
|
||||
hex(key: Message, message: Message, outputBits: number, customization: Message): string;
|
||||
|
||||
/**
|
||||
* Hash and return ArrayBuffer.
|
||||
*
|
||||
* @param key The key string.
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
* @param customization The customization string.
|
||||
*/
|
||||
arrayBuffer(key: Message, message: Message, outputBits: number, customization: Message): ArrayBuffer;
|
||||
|
||||
/**
|
||||
* Hash and return integer array.
|
||||
*
|
||||
* @param key The key string.
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
* @param customization The customization string.
|
||||
*/
|
||||
digest(key: Message, message: Message, outputBits: number, customization: Message): number[];
|
||||
|
||||
/**
|
||||
* Hash and return integer array.
|
||||
*
|
||||
* @param key The key string.
|
||||
* @param message The message you want to hash.
|
||||
* @param outputBits The length of output.
|
||||
* @param customization The customization string.
|
||||
*/
|
||||
array(key: Message, message: Message, outputBits: number, customization: Message): number[];
|
||||
|
||||
/**
|
||||
* Create a hash object.
|
||||
*
|
||||
|
||||
Generated
+1894
File diff suppressed because it is too large
Load Diff
+14
-10
@@ -1,21 +1,20 @@
|
||||
{
|
||||
"name": "js-sha3",
|
||||
"version": "0.6.1",
|
||||
"version": "0.9.2",
|
||||
"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",
|
||||
"uglifyjs": "~2.4.10"
|
||||
"mocha": "~10.2.0",
|
||||
"nyc": "^15.1.0",
|
||||
"tiny-worker": "^2.3.0",
|
||||
"uglify-js": "^3.1.9"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha tests/node-test.js -r jscoverage",
|
||||
"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"
|
||||
"test": "nyc mocha tests/node-test.js",
|
||||
"report": "nyc --reporter=html --reporter=text mocha tests/node-test.js",
|
||||
"coveralls": "nyc report --reporter=text-lcov | coveralls",
|
||||
"build": "uglifyjs src/sha3.js -c -m --comments --output build/sha3.min.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -37,5 +36,10 @@
|
||||
"homepage": "https://github.com/emn178/js-sha3",
|
||||
"bugs": {
|
||||
"url": "https://github.com/emn178/js-sha3/issues"
|
||||
},
|
||||
"nyc": {
|
||||
"exclude": [
|
||||
"tests"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
+100
-71
@@ -1,21 +1,31 @@
|
||||
/**
|
||||
* [js-sha3]{@link https://github.com/emn178/js-sha3}
|
||||
*
|
||||
* @version 0.6.1
|
||||
* @version 0.9.2
|
||||
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
||||
* @copyright Chen, Yi-Cyuan 2015-2017
|
||||
* @copyright Chen, Yi-Cyuan 2015-2023
|
||||
* @license MIT
|
||||
*/
|
||||
/*jslint bitwise: true */
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var root = typeof window === 'object' ? window : {};
|
||||
var INPUT_ERROR = 'input is invalid type';
|
||||
var FINALIZE_ERROR = 'finalize already called';
|
||||
var WINDOW = typeof window === 'object';
|
||||
var root = WINDOW ? window : {};
|
||||
if (root.JS_SHA3_NO_WINDOW) {
|
||||
WINDOW = false;
|
||||
}
|
||||
var WEB_WORKER = !WINDOW && typeof self === 'object';
|
||||
var NODE_JS = !root.JS_SHA3_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
|
||||
if (NODE_JS) {
|
||||
root = global;
|
||||
} else if (WEB_WORKER) {
|
||||
root = self;
|
||||
}
|
||||
var COMMON_JS = !root.JS_SHA3_NO_COMMON_JS && typeof module === 'object' && module.exports;
|
||||
var AMD = typeof define === 'function' && define.amd;
|
||||
var ARRAY_BUFFER = !root.JS_SHA3_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
|
||||
var HEX_CHARS = '0123456789abcdef'.split('');
|
||||
var SHAKE_PADDING = [31, 7936, 2031616, 520093696];
|
||||
@@ -24,24 +34,53 @@
|
||||
var PADDING = [6, 1536, 393216, 100663296];
|
||||
var SHIFT = [0, 8, 16, 24];
|
||||
var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649,
|
||||
0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0,
|
||||
2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771,
|
||||
2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648,
|
||||
2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648];
|
||||
0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0,
|
||||
2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771,
|
||||
2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648,
|
||||
2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648];
|
||||
var BITS = [224, 256, 384, 512];
|
||||
var SHAKE_BITS = [128, 256];
|
||||
var OUTPUT_TYPES = ['hex', 'buffer', 'arrayBuffer', 'array'];
|
||||
var OUTPUT_TYPES = ['hex', 'buffer', 'arrayBuffer', 'array', 'digest'];
|
||||
var CSHAKE_BYTEPAD = {
|
||||
'128': 168,
|
||||
'256': 136
|
||||
};
|
||||
|
||||
if (root.JS_SHA3_NO_NODE_JS || !Array.isArray) {
|
||||
Array.isArray = function (obj) {
|
||||
return Object.prototype.toString.call(obj) === '[object Array]';
|
||||
};
|
||||
|
||||
var isArray = root.JS_SHA3_NO_NODE_JS || !Array.isArray
|
||||
? function (obj) {
|
||||
return Object.prototype.toString.call(obj) === '[object Array]';
|
||||
}
|
||||
: Array.isArray;
|
||||
|
||||
var isView = (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView))
|
||||
? function (obj) {
|
||||
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
|
||||
}
|
||||
: ArrayBuffer.isView;
|
||||
|
||||
// [message: string, isString: bool]
|
||||
var formatMessage = function (message) {
|
||||
var type = typeof message;
|
||||
if (type === 'string') {
|
||||
return [message, true];
|
||||
}
|
||||
if (type !== 'object' || message === null) {
|
||||
throw new Error(INPUT_ERROR);
|
||||
}
|
||||
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
|
||||
return [new Uint8Array(message), false];
|
||||
}
|
||||
if (!isArray(message) && !isView(message)) {
|
||||
throw new Error(INPUT_ERROR);
|
||||
}
|
||||
return [message, false];
|
||||
}
|
||||
|
||||
var empty = function (message) {
|
||||
return formatMessage(message)[0].length === 0;
|
||||
};
|
||||
|
||||
var createOutputMethod = function (bits, padding, outputType) {
|
||||
return function (message) {
|
||||
return new Keccak(bits, padding, bits).update(message)[outputType]();
|
||||
@@ -100,7 +139,7 @@
|
||||
var w = CSHAKE_BYTEPAD[bits];
|
||||
var method = createCshakeOutputMethod(bits, padding, 'hex');
|
||||
method.create = function (outputBits, n, s) {
|
||||
if (!n && !s) {
|
||||
if (empty(n) && empty(s)) {
|
||||
return methods['shake' + bits].create(outputBits);
|
||||
} else {
|
||||
return new Keccak(bits, padding, outputBits).bytepad([n, s], w);
|
||||
@@ -125,18 +164,18 @@
|
||||
};
|
||||
|
||||
var algorithms = [
|
||||
{name: 'keccak', padding: KECCAK_PADDING, bits: BITS, createMethod: createMethod},
|
||||
{name: 'sha3', padding: PADDING, bits: BITS, createMethod: createMethod},
|
||||
{name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod},
|
||||
{name: 'cshake', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createCshakeMethod},
|
||||
{name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createKmacMethod}
|
||||
{ name: 'keccak', padding: KECCAK_PADDING, bits: BITS, createMethod: createMethod },
|
||||
{ name: 'sha3', padding: PADDING, bits: BITS, createMethod: createMethod },
|
||||
{ name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod },
|
||||
{ name: 'cshake', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createCshakeMethod },
|
||||
{ name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createKmacMethod }
|
||||
];
|
||||
|
||||
var methods = {}, methodNames = [];
|
||||
|
||||
for (var i = 0; i < algorithms.length; ++i) {
|
||||
var algorithm = algorithms[i];
|
||||
var bits = algorithm.bits;
|
||||
var bits = algorithm.bits;
|
||||
for (var j = 0; j < bits.length; ++j) {
|
||||
var methodName = algorithm.name + '_' + bits[j];
|
||||
methodNames.push(methodName);
|
||||
@@ -155,6 +194,7 @@
|
||||
this.padding = padding;
|
||||
this.outputBits = outputBits;
|
||||
this.reset = true;
|
||||
this.finalized = false;
|
||||
this.block = 0;
|
||||
this.start = 0;
|
||||
this.blockCount = (1600 - (bits << 1)) >> 5;
|
||||
@@ -168,19 +208,13 @@
|
||||
}
|
||||
|
||||
Keccak.prototype.update = function (message) {
|
||||
var notString = typeof message !== 'string';
|
||||
if (notString && message.constructor === root.ArrayBuffer) {
|
||||
message = new Uint8Array(message);
|
||||
if (this.finalized) {
|
||||
throw new Error(FINALIZE_ERROR);
|
||||
}
|
||||
var length = message.length;
|
||||
if (notString) {
|
||||
if (typeof length !== 'number' ||
|
||||
!Array.isArray(message) &&
|
||||
!(ARRAY_BUFFER && ArrayBuffer.isView(message))) {
|
||||
throw 'input is invalid type';
|
||||
}
|
||||
}
|
||||
var blocks = this.blocks, byteCount = this.byteCount,
|
||||
var result = formatMessage(message);
|
||||
message = result[0];
|
||||
var isString = result[1];
|
||||
var blocks = this.blocks, byteCount = this.byteCount, length = message.length,
|
||||
blockCount = this.blockCount, index = 0, s = this.s, i, code;
|
||||
|
||||
while (index < length) {
|
||||
@@ -191,11 +225,7 @@
|
||||
blocks[i] = 0;
|
||||
}
|
||||
}
|
||||
if (notString) {
|
||||
for (i = this.start; index < length && i < byteCount; ++index) {
|
||||
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
|
||||
}
|
||||
} else {
|
||||
if (isString) {
|
||||
for (i = this.start; index < length && i < byteCount; ++index) {
|
||||
code = message.charCodeAt(index);
|
||||
if (code < 0x80) {
|
||||
@@ -215,6 +245,10 @@
|
||||
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (i = this.start; index < length && i < byteCount; ++index) {
|
||||
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
|
||||
}
|
||||
}
|
||||
this.lastByteIndex = i;
|
||||
if (i >= byteCount) {
|
||||
@@ -253,23 +287,11 @@
|
||||
};
|
||||
|
||||
Keccak.prototype.encodeString = function (str) {
|
||||
str = str || '';
|
||||
var notString = typeof str !== 'string';
|
||||
if (notString && str.constructor === root.ArrayBuffer) {
|
||||
str = new Uint8Array(str);
|
||||
}
|
||||
var length = str.length;
|
||||
if (notString) {
|
||||
if (typeof length !== 'number' ||
|
||||
!Array.isArray(str) &&
|
||||
!(ARRAY_BUFFER && ArrayBuffer.isView(str))) {
|
||||
throw 'input is invalid type';
|
||||
}
|
||||
}
|
||||
var bytes = 0;
|
||||
if (notString) {
|
||||
bytes = length;
|
||||
} else {
|
||||
var result = formatMessage(str);
|
||||
str = result[0];
|
||||
var isString = result[1];
|
||||
var bytes = 0, length = str.length;
|
||||
if (isString) {
|
||||
for (var i = 0; i < str.length; ++i) {
|
||||
var code = str.charCodeAt(i);
|
||||
if (code < 0x80) {
|
||||
@@ -283,6 +305,8 @@
|
||||
bytes += 4;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bytes = length;
|
||||
}
|
||||
bytes += this.encode(bytes * 8);
|
||||
this.update(str);
|
||||
@@ -291,10 +315,10 @@
|
||||
|
||||
Keccak.prototype.bytepad = function (strs, w) {
|
||||
var bytes = this.encode(w);
|
||||
for (var i = 0;i < strs.length;++i) {
|
||||
for (var i = 0; i < strs.length; ++i) {
|
||||
bytes += this.encodeString(strs[i]);
|
||||
}
|
||||
var paddingBytes = w - bytes % w;
|
||||
var paddingBytes = (w - bytes % w) % w;
|
||||
var zeros = [];
|
||||
zeros.length = paddingBytes;
|
||||
this.update(zeros);
|
||||
@@ -302,6 +326,10 @@
|
||||
};
|
||||
|
||||
Keccak.prototype.finalize = function () {
|
||||
if (this.finalized) {
|
||||
return;
|
||||
}
|
||||
this.finalized = true;
|
||||
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) {
|
||||
@@ -321,15 +349,15 @@
|
||||
this.finalize();
|
||||
|
||||
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;
|
||||
while (j < outputBlocks) {
|
||||
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];
|
||||
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) {
|
||||
f(s);
|
||||
@@ -338,9 +366,7 @@
|
||||
}
|
||||
if (extraBytes) {
|
||||
block = s[i];
|
||||
if (extraBytes > 0) {
|
||||
hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F];
|
||||
}
|
||||
hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F];
|
||||
if (extraBytes > 1) {
|
||||
hex += HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F];
|
||||
}
|
||||
@@ -355,7 +381,7 @@
|
||||
this.finalize();
|
||||
|
||||
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 buffer;
|
||||
if (extraBytes) {
|
||||
@@ -385,7 +411,7 @@
|
||||
this.finalize();
|
||||
|
||||
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;
|
||||
while (j < outputBlocks) {
|
||||
for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) {
|
||||
@@ -403,9 +429,7 @@
|
||||
if (extraBytes) {
|
||||
offset = j << 2;
|
||||
block = s[i];
|
||||
if (extraBytes > 0) {
|
||||
array[offset] = block & 0xFF;
|
||||
}
|
||||
array[offset] = block & 0xFF;
|
||||
if (extraBytes > 1) {
|
||||
array[offset + 1] = (block >> 8) & 0xFF;
|
||||
}
|
||||
@@ -429,9 +453,9 @@
|
||||
|
||||
var f = function (s) {
|
||||
var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9,
|
||||
b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17,
|
||||
b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33,
|
||||
b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49;
|
||||
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) {
|
||||
c0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40];
|
||||
c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41];
|
||||
@@ -615,8 +639,13 @@
|
||||
if (COMMON_JS) {
|
||||
module.exports = methods;
|
||||
} else {
|
||||
for (var i = 0; i < methodNames.length; ++i) {
|
||||
for (i = 0; i < methodNames.length; ++i) {
|
||||
root[methodNames[i]] = methods[methodNames[i]];
|
||||
}
|
||||
if (AMD) {
|
||||
define(function () {
|
||||
return methods;
|
||||
});
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
+137
-67
@@ -1,80 +1,150 @@
|
||||
expect = require('expect.js');
|
||||
Worker = require("tiny-worker");
|
||||
|
||||
function unset() {
|
||||
delete require.cache[require.resolve('../src/sha3.js')];
|
||||
delete require.cache[require.resolve('./test.js')];
|
||||
sha3_512 = null;
|
||||
sha3_384 = null;
|
||||
sha3_256 = null;
|
||||
sha3_224 = null;
|
||||
keccak512 = null;
|
||||
keccak384 = null;
|
||||
keccak256 = null;
|
||||
keccak224 = null;
|
||||
shake128 = null;
|
||||
shake256 = null;
|
||||
kmac128 = null;
|
||||
kmac256 = null;
|
||||
BUFFER = undefined;
|
||||
JS_SHA3_NO_WINDOW = undefined;
|
||||
JS_SHA3_NO_NODE_JS = undefined;
|
||||
JS_SHA3_NO_COMMON_JS = undefined;
|
||||
JS_SHA3_NO_ARRAY_BUFFER = undefined;
|
||||
JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW = undefined;
|
||||
window = undefined;
|
||||
}
|
||||
|
||||
function requireToGlobal() {
|
||||
var sha3 = require('../src/sha3.js');
|
||||
keccak512 = sha3.keccak512;
|
||||
keccak384 = sha3.keccak384;
|
||||
keccak256 = sha3.keccak256;
|
||||
keccak224 = sha3.keccak224;
|
||||
sha3_512 = sha3.sha3_512;
|
||||
sha3_384 = sha3.sha3_384;
|
||||
sha3_256 = sha3.sha3_256;
|
||||
sha3_224 = sha3.sha3_224;
|
||||
shake128 = sha3.shake128;
|
||||
shake256 = sha3.shake256;
|
||||
cshake128 = sha3.cshake128;
|
||||
cshake256 = sha3.cshake256;
|
||||
kmac128 = sha3.kmac128;
|
||||
kmac256 = sha3.kmac256;
|
||||
}
|
||||
|
||||
function runCommonJsTest() {
|
||||
requireToGlobal();
|
||||
require('./test.js');
|
||||
unset();
|
||||
}
|
||||
|
||||
function runWindowTest(extra) {
|
||||
window = global;
|
||||
require('../src/sha3.js');
|
||||
require('./test.js');
|
||||
if (extra) {
|
||||
require('./test-shake.js');
|
||||
require('./test-cshake.js');
|
||||
require('./test-kmac.js');
|
||||
}
|
||||
unset();
|
||||
}
|
||||
|
||||
// Node.js env
|
||||
var sha3 = require('../src/sha3.js');
|
||||
keccak512 = sha3.keccak512;
|
||||
keccak384 = sha3.keccak384;
|
||||
keccak256 = sha3.keccak256;
|
||||
keccak224 = sha3.keccak224;
|
||||
sha3_512 = sha3.sha3_512;
|
||||
sha3_384 = sha3.sha3_384;
|
||||
sha3_256 = sha3.sha3_256;
|
||||
sha3_224 = sha3.sha3_224;
|
||||
shake128 = sha3.shake128;
|
||||
shake256 = sha3.shake256;
|
||||
cshake128 = sha3.cshake128;
|
||||
cshake256 = sha3.cshake256;
|
||||
kmac128 = sha3.kmac128;
|
||||
kmac256 = sha3.kmac256;
|
||||
require('./test.js');
|
||||
require('./test-shake.js');
|
||||
require('./test-cshake.js');
|
||||
require('./test-kmac.js');
|
||||
|
||||
delete require.cache[require.resolve('../src/sha3.js')];
|
||||
delete require.cache[require.resolve('./test.js')];
|
||||
delete require.cache[require.resolve('./test-shake.js')];
|
||||
delete require.cache[require.resolve('./test-cshake.js')];
|
||||
delete require.cache[require.resolve('./test-kmac.js')];
|
||||
BUFFER = true;
|
||||
runCommonJsTest();
|
||||
|
||||
// Webpack browser env
|
||||
JS_SHA3_NO_NODE_JS = true;
|
||||
window = global;
|
||||
expect = require('expect.js');
|
||||
var sha3 = require('../src/sha3.js');
|
||||
keccak512 = sha3.keccak512;
|
||||
keccak384 = sha3.keccak384;
|
||||
keccak256 = sha3.keccak256;
|
||||
keccak224 = sha3.keccak224;
|
||||
sha3_512 = sha3.sha3_512;
|
||||
sha3_384 = sha3.sha3_384;
|
||||
sha3_256 = sha3.sha3_256;
|
||||
sha3_224 = sha3.sha3_224;
|
||||
shake128 = sha3.shake128;
|
||||
shake256 = sha3.shake256;
|
||||
cshake128 = sha3.cshake128;
|
||||
cshake256 = sha3.cshake256;
|
||||
kmac128 = sha3.kmac128;
|
||||
kmac256 = sha3.kmac256;
|
||||
require('./test.js');
|
||||
require('./test-shake.js');
|
||||
require('./test-cshake.js');
|
||||
require('./test-kmac.js');
|
||||
|
||||
delete require.cache[require.resolve('../src/sha3.js')];
|
||||
delete require.cache[require.resolve('./test.js')];
|
||||
delete require.cache[require.resolve('./test-shake.js')];
|
||||
delete require.cache[require.resolve('./test-cshake.js')];
|
||||
delete require.cache[require.resolve('./test-kmac.js')];
|
||||
sha3_512 = null;
|
||||
sha3_384 = null;
|
||||
sha3_256 = null;
|
||||
sha3_224 = null;
|
||||
keccak512 = null;
|
||||
keccak384 = null;
|
||||
keccak256 = null;
|
||||
keccak224 = null;
|
||||
shake128 = null;
|
||||
shake256 = null;
|
||||
kmac128 = null;
|
||||
kmac256 = null;
|
||||
runCommonJsTest();
|
||||
|
||||
// browser env
|
||||
JS_SHA3_NO_NODE_JS = true;
|
||||
JS_SHA3_NO_COMMON_JS = true;
|
||||
runWindowTest(true);
|
||||
|
||||
// browser env and no array buffer
|
||||
JS_SHA3_NO_NODE_JS = true;
|
||||
JS_SHA3_NO_COMMON_JS = true;
|
||||
JS_SHA3_NO_ARRAY_BUFFER = true;
|
||||
runWindowTest();
|
||||
|
||||
// browser env and no isView
|
||||
JS_SHA3_NO_NODE_JS = true;
|
||||
JS_SHA3_NO_COMMON_JS = true;
|
||||
JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW = true;
|
||||
runWindowTest();
|
||||
|
||||
// browser AMD
|
||||
JS_SHA3_NO_NODE_JS = true;
|
||||
JS_SHA3_NO_COMMON_JS = true;
|
||||
JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW = false;
|
||||
window = global;
|
||||
define = function (func) {
|
||||
sha3 = func();
|
||||
keccak512 = sha3.keccak512;
|
||||
keccak384 = sha3.keccak384;
|
||||
keccak256 = sha3.keccak256;
|
||||
keccak224 = sha3.keccak224;
|
||||
sha3_512 = sha3.sha3_512;
|
||||
sha3_384 = sha3.sha3_384;
|
||||
sha3_256 = sha3.sha3_256;
|
||||
sha3_224 = sha3.sha3_224;
|
||||
shake128 = sha3.shake128;
|
||||
shake256 = sha3.shake256;
|
||||
cshake128 = sha3.cshake128;
|
||||
cshake256 = sha3.cshake256;
|
||||
kmac128 = sha3.kmac128;
|
||||
kmac256 = sha3.kmac256;
|
||||
require('./test.js');
|
||||
};
|
||||
define.amd = true;
|
||||
|
||||
require('../src/sha3.js');
|
||||
require('./test.js');
|
||||
require('./test-shake.js');
|
||||
require('./test-cshake.js');
|
||||
require('./test-kmac.js');
|
||||
unset();
|
||||
|
||||
// webworker
|
||||
WORKER = 'tests/worker.js';
|
||||
SOURCE = 'src/sha3.js';
|
||||
|
||||
require('./worker-test.js');
|
||||
|
||||
delete require.cache[require.resolve('./worker-test.js')];
|
||||
|
||||
// cover webworker
|
||||
JS_SHA3_NO_WINDOW = true;
|
||||
JS_SHA3_NO_NODE_JS = true;
|
||||
WORKER = './worker.js';
|
||||
SOURCE = '../src/sha3.js';
|
||||
window = global;
|
||||
self = global;
|
||||
|
||||
Worker = function (file) {
|
||||
require(file);
|
||||
currentWorker = this;
|
||||
|
||||
this.postMessage = function (data) {
|
||||
onmessage({data: data});
|
||||
};
|
||||
}
|
||||
|
||||
postMessage = function (data) {
|
||||
currentWorker.onmessage({data: data});
|
||||
}
|
||||
|
||||
importScripts = function () {};
|
||||
|
||||
requireToGlobal();
|
||||
require('./worker-test.js');
|
||||
|
||||
@@ -25,6 +25,27 @@
|
||||
n: '',
|
||||
s: '',
|
||||
output: '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26'
|
||||
},
|
||||
{
|
||||
input: [],
|
||||
bits: 256,
|
||||
n: [],
|
||||
s: [],
|
||||
output: '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26'
|
||||
},
|
||||
{
|
||||
input: [],
|
||||
bits: 256,
|
||||
n: new ArrayBuffer(0),
|
||||
s: new ArrayBuffer(0),
|
||||
output: '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26'
|
||||
},
|
||||
{
|
||||
input: [],
|
||||
bits: 256,
|
||||
n: new Uint8Array([]),
|
||||
s: new Uint8Array([]),
|
||||
output: '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26'
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -94,6 +94,13 @@
|
||||
key: [0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F],
|
||||
s: 'My Tagged Application',
|
||||
output: 'b58618f71f92e1d56c1b8c55ddd7cd188b97b4ca4d99831eb2699a837da2e4d970fbacfde50033aea585f1a2708510c32d07880801bd182898fe476876fc8965'
|
||||
},
|
||||
{
|
||||
input: [],
|
||||
bits: 128,
|
||||
key: [],
|
||||
s: [8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173, 255, 41, 197, 8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173, 255, 41, 197, 8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173, 255, 41, 197, 8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173],
|
||||
output: '031801b0b50ebeef772fbe7a279bc144'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
it('should be equal', function () {
|
||||
expect(shake128('', 16)).to.be('7f9c');
|
||||
expect(shake128('', 24)).to.be('7f9c2b');
|
||||
expect(shake128.array('', 8)).to.eql([0x7f]);
|
||||
expect(shake128.array('', 16)).to.eql([0x7f, 0x9c]);
|
||||
expect(shake128.array('', 24)).to.eql([0x7f, 0x9c, 0x2b]);
|
||||
});
|
||||
|
||||
+183
-150
@@ -9,63 +9,6 @@
|
||||
return hex;
|
||||
};
|
||||
|
||||
function runTestCases(methods, testCases) {
|
||||
methods.forEach(function (method) {
|
||||
describe('#' + method.name, function () {
|
||||
var methodTestCases = testCases[method.name];
|
||||
for (var testCaseName in methodTestCases) {
|
||||
(function (testCaseName) {
|
||||
var testCase = methodTestCases[testCaseName];
|
||||
context('when ' + testCaseName, function () {
|
||||
for (var hash in testCase) {
|
||||
(function (message, hash) {
|
||||
it('should be equal', function () {
|
||||
expect(method.call(message)).to.be(hash);
|
||||
});
|
||||
})(testCase[hash], hash);
|
||||
}
|
||||
});
|
||||
})(testCaseName);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var methods = [
|
||||
{
|
||||
name: 'sha3_512',
|
||||
call: sha3_512
|
||||
},
|
||||
{
|
||||
name: 'sha3_384',
|
||||
call: sha3_384
|
||||
},
|
||||
{
|
||||
name: 'sha3_256',
|
||||
call: sha3_256
|
||||
},
|
||||
{
|
||||
name: 'sha3_224',
|
||||
call: sha3_224
|
||||
},
|
||||
{
|
||||
name: 'keccak512',
|
||||
call: keccak512
|
||||
},
|
||||
{
|
||||
name: 'keccak384',
|
||||
call: keccak384
|
||||
},
|
||||
{
|
||||
name: 'keccak256',
|
||||
call: keccak256
|
||||
},
|
||||
{
|
||||
name: 'keccak224',
|
||||
call: keccak224
|
||||
}
|
||||
];
|
||||
|
||||
var testCases = {
|
||||
sha3_512: {
|
||||
'ascii': {
|
||||
@@ -92,14 +35,6 @@
|
||||
'Array': {
|
||||
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': [],
|
||||
'01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450': [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]
|
||||
},
|
||||
'Uint8Array': {
|
||||
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': new Uint8Array([]),
|
||||
'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),
|
||||
'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: {
|
||||
@@ -237,104 +172,202 @@
|
||||
}
|
||||
};
|
||||
|
||||
runTestCases(methods, testCases);
|
||||
if (!(typeof JS_SHA3_NO_ARRAY_BUFFER === 'boolean' && JS_SHA3_NO_ARRAY_BUFFER)) {
|
||||
testCases.sha3_512.Uint8Array = {
|
||||
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': new Uint8Array([]),
|
||||
'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])
|
||||
};
|
||||
testCases.sha3_512.ArrayBuffer = {
|
||||
'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
|
||||
};
|
||||
}
|
||||
|
||||
describe('sha3_512', 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');
|
||||
});
|
||||
});
|
||||
if (typeof BUFFER === 'boolean' && BUFFER) {
|
||||
testCases.sha3_512.Buffer = {
|
||||
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': Buffer.from([]),
|
||||
'01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450': Buffer.from(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]))
|
||||
};
|
||||
}
|
||||
|
||||
context('#hex', function () {
|
||||
it('should be equal', function () {
|
||||
expect(sha3_512.hex('')).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26');
|
||||
});
|
||||
});
|
||||
var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}];
|
||||
|
||||
context('#update', function () {
|
||||
it('should be equal', function () {
|
||||
expect(sha3_512.update('').hex()).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26');
|
||||
expect(sha3_512.update('The quick brown fox ').update('jumps over the lazy dog').hex()).to.be('01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450');
|
||||
});
|
||||
});
|
||||
|
||||
context('#create', 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 hash = sha3_512.create();
|
||||
for (var i = 0; i < bytes.length; ++i) {
|
||||
hash.update([bytes[i]]);
|
||||
function runTestCases(name, algorithm) {
|
||||
var methods = [
|
||||
{
|
||||
name: name,
|
||||
call: algorithm,
|
||||
},
|
||||
{
|
||||
name: name + '.hex',
|
||||
call: algorithm.hex
|
||||
},
|
||||
{
|
||||
name: name + '.array',
|
||||
call: function (message) {
|
||||
return algorithm.array(message).toHexString();
|
||||
}
|
||||
expect(hash.hex()).to.be('01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450');
|
||||
},
|
||||
{
|
||||
name: name + '.digest',
|
||||
call: function (message) {
|
||||
return algorithm.digest(message).toHexString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: name + '.arrayBuffer',
|
||||
call: function (message) {
|
||||
return algorithm.arrayBuffer(message).toHexString();
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
var classMethods = [
|
||||
{
|
||||
name: 'create',
|
||||
call: function (message) {
|
||||
return algorithm.create().update(message).toString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'update',
|
||||
call: function (message) {
|
||||
return algorithm.update(message).toString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'hex',
|
||||
call: function (message) {
|
||||
return algorithm.update(message).hex();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'array',
|
||||
call: function (message) {
|
||||
return algorithm.update(message).array().toHexString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'digest',
|
||||
call: function (message) {
|
||||
return algorithm.update(message).digest().toHexString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'arrayBuffer',
|
||||
call: function (message) {
|
||||
return algorithm.update(message).arrayBuffer().toHexString();
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'finalize',
|
||||
call: function (message) {
|
||||
var hash = algorithm.update(message);
|
||||
hash.hex();
|
||||
hash.finalize();
|
||||
return hash.hex();
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
var subTestCases = testCases[name];
|
||||
|
||||
describe(name, function () {
|
||||
methods.forEach(function (method) {
|
||||
describe('#' + method.name, function () {
|
||||
for (var testCaseName in subTestCases) {
|
||||
(function (testCaseName) {
|
||||
var testCase = subTestCases[testCaseName];
|
||||
context('when ' + testCaseName, function () {
|
||||
for (var hash in testCase) {
|
||||
(function (message, hash) {
|
||||
it('should be equal', function () {
|
||||
expect(method.call(message)).to.be(hash);
|
||||
});
|
||||
})(testCase[hash], hash);
|
||||
}
|
||||
});
|
||||
})(testCaseName);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
classMethods.forEach(function (method) {
|
||||
describe('#' + method.name, function () {
|
||||
for (var testCaseName in subTestCases) {
|
||||
(function (testCaseName) {
|
||||
var testCase = subTestCases[testCaseName];
|
||||
context('when ' + testCaseName, function () {
|
||||
for (var hash in testCase) {
|
||||
(function (message, hash) {
|
||||
it('should be equal', function () {
|
||||
expect(method.call(message)).to.be(hash);
|
||||
});
|
||||
})(testCase[hash], hash);
|
||||
}
|
||||
});
|
||||
})(testCaseName);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#' + name, function () {
|
||||
errorTestCases.forEach(function (testCase) {
|
||||
context('when ' + testCase, function () {
|
||||
it('should throw error', function () {
|
||||
expect(function () {
|
||||
algorithm(testCase);
|
||||
}).to.throwError(/input is invalid type/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('when update after finalize', function () {
|
||||
it('should throw error', function () {
|
||||
expect(function () {
|
||||
var hash = algorithm.update('any');
|
||||
hash.hex();
|
||||
hash.update('any');
|
||||
}).to.throwError(/finalize already called/);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
runTestCases('sha3_512', sha3_512);
|
||||
runTestCases('sha3_384', sha3_384);
|
||||
runTestCases('sha3_256', sha3_256);
|
||||
runTestCases('sha3_224', sha3_224);
|
||||
runTestCases('keccak512', keccak512);
|
||||
runTestCases('keccak384', keccak384);
|
||||
runTestCases('keccak256', keccak256);
|
||||
runTestCases('keccak224', keccak224);
|
||||
|
||||
describe('#keccak512', function () {
|
||||
context('when special length', function () {
|
||||
it('should be equal', function () {
|
||||
expect(keccak512('012345678901234567890123456789012345678901234567890123456789012345678901')).to.be('90b1d032c3bf06dcc78a46fe52054bab1250600224bfc6dfbfb40a7877c55e89bb982799a2edf198568a4166f6736678b45e76b12fac813cfdf0a76714e5eae8');
|
||||
expect(keccak512('01234567890123456789012345678901234567890123456789012345678901234567890')).to.be('3173e7abc754a0b2909410d78986428a9183e996864af02f421d273d9fa1b4e4a5b14e2998b20767712f53a01ff8f6ae2c3e71e51e2c0f24257b03e6da09eb77');
|
||||
});
|
||||
});
|
||||
|
||||
context('when Array', function () {
|
||||
it('should be equal', function () {
|
||||
expect(keccak512([])).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
||||
expect(keccak512([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])).to.be('d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609');
|
||||
});
|
||||
});
|
||||
|
||||
context('when Uint8Array', function () {
|
||||
it('should be equal', function () {
|
||||
expect(keccak512(new Uint8Array([]))).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
||||
expect(keccak512(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]))).to.be('d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609');
|
||||
});
|
||||
});
|
||||
|
||||
context('when ArrayBuffer', function () {
|
||||
it('should be equal', function () {
|
||||
expect(keccak512(new ArrayBuffer(0))).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
||||
});
|
||||
});
|
||||
|
||||
context('when output ArrayBuffer', function () {
|
||||
it('should be equal', function () {
|
||||
expect(keccak512.arrayBuffer('').toHexString()).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
||||
expect(keccak512.buffer('').toHexString()).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
||||
});
|
||||
});
|
||||
|
||||
context('when output Array', function () {
|
||||
it('should be equal', function () {
|
||||
expect(keccak512.array('').toHexString()).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e');
|
||||
});
|
||||
});
|
||||
|
||||
context('when incorrect input', function () {
|
||||
it('should throw error', function () {
|
||||
expect(function () {
|
||||
keccak512(1);
|
||||
}).to.throwError();
|
||||
});
|
||||
});
|
||||
|
||||
context('#encodeString', function () {
|
||||
context('when incorrect input', function () {
|
||||
it('should throw error', function () {
|
||||
expect(function () {
|
||||
keccak512.create().encodeString(1);
|
||||
}).to.throwError();
|
||||
errorTestCases.forEach(function (testCase) {
|
||||
context('when ' + testCase, function () {
|
||||
it('should throw error', function () {
|
||||
expect(function () {
|
||||
keccak512.create().encodeString(testCase);
|
||||
}).to.throwError(/input is invalid type/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('when ArrayBuffer', function () {
|
||||
it('should throw error', function () {
|
||||
expect(keccak512.create().encodeString(new ArrayBuffer(0))).to.be(2);
|
||||
if (!(typeof JS_SHA3_NO_ARRAY_BUFFER === 'boolean' && JS_SHA3_NO_ARRAY_BUFFER)) {
|
||||
context('when ArrayBuffer', function () {
|
||||
it('should throw error', function () {
|
||||
expect(keccak512.create().encodeString(new ArrayBuffer(0))).to.be(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
context('when Uint8Array', function () {
|
||||
it('should throw error', function () {
|
||||
expect(keccak512.create().encodeString(new Uint8Array(0))).to.be(2);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
context('when UTF-8', function () {
|
||||
it('should throw error', function () {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
(function (Worker, WORKER, SOURCE) {
|
||||
var cases = {
|
||||
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': '',
|
||||
'01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450': 'The quick brown fox jumps over the lazy dog',
|
||||
'18f4f4bd419603f95538837003d9d254c26c23765565162247483f65c50303597bc9ce4d289f21d1c2f1f458828e33dc442100331b35e7eb031b5d38ba6460f8': 'The quick brown fox jumps over the lazy dog.'
|
||||
};
|
||||
|
||||
describe('#sha3_512', function () {
|
||||
Object.keys(cases).forEach(function (hash) {
|
||||
it('should be equal', function (done) {
|
||||
var worker = new Worker(WORKER);
|
||||
worker.onmessage = function(event) {
|
||||
expect(event.data).to.be(hash);
|
||||
if (worker.terminate) {
|
||||
worker.terminate();
|
||||
}
|
||||
done();
|
||||
};
|
||||
worker.postMessage(SOURCE);
|
||||
worker.postMessage(cases[hash]);
|
||||
});
|
||||
});
|
||||
});
|
||||
})(Worker, WORKER, SOURCE);
|
||||
@@ -0,0 +1,26 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>SHA3</title>
|
||||
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
|
||||
<script src="../node_modules/mocha/mocha.js"></script>
|
||||
<script src="../node_modules/expect.js/index.js"></script>
|
||||
<script src="../src/sha3.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script>
|
||||
WORKER = 'worker.js';
|
||||
SOURCE = '../src/sha3.js';
|
||||
mocha.setup('bdd');
|
||||
</script>
|
||||
<script src="worker-test.js"></script>
|
||||
<script>
|
||||
mocha.checkLeaks();
|
||||
mocha.run();
|
||||
</script>
|
||||
<script>
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,12 @@
|
||||
var imported = false;
|
||||
onmessage = function(e) {
|
||||
if (imported) {
|
||||
postMessage(sha3_512(e.data));
|
||||
if (typeof exports !== 'undefined') {
|
||||
imported = false;
|
||||
}
|
||||
} else {
|
||||
imported = true;
|
||||
importScripts(e.data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user