Compare commits

..
1 Commits
Author SHA1 Message Date
Yi-Cyuan ChenandClaude Opus 5.5 9c5eb82a0d ### Fixed
- Incorrect hash for `DataView` input, and for `Int8Array` with negative values.
- Throws `ArrayBuffer is not defined` in browsers without `ArrayBuffer`.
- Throws `global is not defined` in browsers with a `process` polyfill (e.g. BrowserFS) and Electron renderers.

### Changed
- Multi-byte typed arrays such as `Uint16Array`, `Uint32Array` and `Float32Array` are now hashed by their underlying bytes, the same as Node.js `crypto` and Web Crypto. Previously each element was treated as one byte.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 13:32:01 +08:00
12 changed files with 90 additions and 21 deletions
+9
View File
@@ -1,5 +1,14 @@
# Change Log # Change Log
## v0.16.0 / 2026-11-18
### Fixed
- Incorrect hash for `DataView` input, and for `Int8Array` with negative values.
- Throws `ArrayBuffer is not defined` in browsers without `ArrayBuffer`.
- Throws `global is not defined` in browsers with a `process` polyfill (e.g. BrowserFS) and Electron renderers.
### Changed
- Multi-byte typed arrays such as `Uint16Array`, `Uint32Array` and `Float32Array` are now hashed by their underlying bytes, the same as Node.js `crypto` and Web Crypto. Previously each element was treated as one byte.
## v0.15.0 / 2026-11-16 ## v0.15.0 / 2026-11-16
### Fixed ### Fixed
- Unpaired UTF-16 surrogates in string input produced wrong digests and consumed the following code unit. They are now encoded as U+FFFD, matching `TextEncoder` and Node.js `crypto` (also applies to cSHAKE/KMAC function names, customization strings, and keys, TupleHash, and ParallelHash) - Unpaired UTF-16 surrogates in string input produced wrong digests and consumed the following code unit. They are now encoded as U+FFFD, matching `TextEncoder` and Node.js `crypto` (also applies to cSHAKE/KMAC function names, customization strings, and keys, TupleHash, and ParallelHash)
+1 -1
View File
@@ -310,7 +310,7 @@ keccak224('中文');
// f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd // f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd
``` ```
It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` input: It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` and other `ArrayBuffer` views as input (their underlying bytes are hashed):
Code Code
```JavaScript ```JavaScript
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.15.0", "version": "0.16.0",
"main": ["src/sha3.js"], "main": ["src/sha3.js"],
"ignore": [ "ignore": [
"samples", "samples",
+2 -2
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
File diff suppressed because one or more lines are too long
+9 -5
View File
@@ -9,7 +9,7 @@ var sha3$1 = {exports: {}};
/** /**
* [js-sha3]{@link https://github.com/emn178/js-sha3} * [js-sha3]{@link https://github.com/emn178/js-sha3}
* *
* @version 0.15.0 * @version 0.16.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2015-2026 * @copyright Chen, Yi-Cyuan 2015-2026
* @license MIT * @license MIT
@@ -32,7 +32,7 @@ var sha3$1 = {exports: {}};
WINDOW = false; WINDOW = false;
} }
var WEB_WORKER = !WINDOW && typeof self === 'object'; var WEB_WORKER = !WINDOW && typeof self === 'object';
var NODE_JS = !root.JS_SHA3_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; var NODE_JS = !root.JS_SHA3_NO_NODE_JS && !WINDOW && typeof process === 'object' && process.versions && process.versions.node && process.type != 'renderer';
if (NODE_JS) { if (NODE_JS) {
root = commonjsGlobal; root = commonjsGlobal;
} else if (WEB_WORKER) { } else if (WEB_WORKER) {
@@ -66,7 +66,7 @@ var sha3$1 = {exports: {}};
} }
: Array.isArray; : Array.isArray;
var isView = (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) var isView = !ARRAY_BUFFER ? null : (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)
? function (obj) { ? function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
} }
@@ -84,10 +84,14 @@ var sha3$1 = {exports: {}};
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false]; return [new Uint8Array(message), false];
} }
if (!isArray(message) && !isView(message)) { if (isArray(message)) {
return [message, false];
}
if (!ARRAY_BUFFER || !isView(message)) {
throw new Error(INPUT_ERROR); throw new Error(INPUT_ERROR);
} }
return [message, false]; // read the underlying bytes of any view (Int8Array, DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
}; };
var empty = function (message) { var empty = function (message) {
Vendored
+1 -1
View File
@@ -1,4 +1,4 @@
type Message = string | number[] | ArrayBuffer | Uint8Array; type Message = string | number[] | ArrayBuffer | ArrayBufferView;
interface Hasher { interface Hasher {
/** /**
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.15.0", "version": "0.16.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "js-sha3", "name": "js-sha3",
"version": "0.15.0", "version": "0.16.0",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@rollup/plugin-commonjs": "^28.0.5", "@rollup/plugin-commonjs": "^28.0.5",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-sha3", "name": "js-sha3",
"version": "0.15.0", "version": "0.16.0",
"description": "A simple SHA-3 / Keccak / SHAKE / cSHAKE / KMAC / TupleHash / ParallelHash hash function for JavaScript supports UTF-8 encoding.", "description": "A simple SHA-3 / Keccak / SHAKE / cSHAKE / KMAC / TupleHash / ParallelHash hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha3.js", "main": "src/sha3.js",
"module": "build/sha3.mjs", "module": "build/sha3.mjs",
+10 -6
View File
@@ -1,7 +1,7 @@
/** /**
* [js-sha3]{@link https://github.com/emn178/js-sha3} * [js-sha3]{@link https://github.com/emn178/js-sha3}
* *
* @version 0.15.0 * @version 0.16.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2015-2026 * @copyright Chen, Yi-Cyuan 2015-2026
* @license MIT * @license MIT
@@ -23,7 +23,7 @@
WINDOW = false; WINDOW = false;
} }
var WEB_WORKER = !WINDOW && typeof self === 'object'; var WEB_WORKER = !WINDOW && typeof self === 'object';
var NODE_JS = !root.JS_SHA3_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; var NODE_JS = !root.JS_SHA3_NO_NODE_JS && !WINDOW && typeof process === 'object' && process.versions && process.versions.node && process.type != 'renderer';
if (NODE_JS) { if (NODE_JS) {
root = global; root = global;
} else if (WEB_WORKER) { } else if (WEB_WORKER) {
@@ -58,7 +58,7 @@
} }
: Array.isArray; : Array.isArray;
var isView = (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) var isView = !ARRAY_BUFFER ? null : (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)
? function (obj) { ? function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
} }
@@ -76,11 +76,15 @@
if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false]; return [new Uint8Array(message), false];
} }
if (!isArray(message) && !isView(message)) { if (isArray(message)) {
throw new Error(INPUT_ERROR);
}
return [message, false]; return [message, false];
} }
if (!ARRAY_BUFFER || !isView(message)) {
throw new Error(INPUT_ERROR);
}
// read the underlying bytes of any view (Int8Array, DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
};
var empty = function (message) { var empty = function (message) {
return formatMessage(message)[0].length === 0; return formatMessage(message)[0].length === 0;
+47
View File
@@ -147,6 +147,53 @@ define.amd = true;
require('../src/sha3.js'); require('../src/sha3.js');
unset(); unset();
// environment detection in isolated contexts (no Node.js globals)
describe('environment detection', function () {
var vm = require('vm');
var fs = require('fs');
var path = require('path');
var source = fs.readFileSync(path.join(__dirname, '../src/sha3.js'), 'utf8');
function run(sandbox, script) {
var context = vm.createContext(sandbox);
if (script) {
vm.runInContext(script, context);
}
vm.runInContext(source, context);
return sandbox;
}
function check(sandbox) {
expect(sandbox.sha3_512('abc')).to.be('b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0');
expect(sandbox.keccak256('abc')).to.be('4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45');
expect(sandbox.shake128('abc', 256)).to.be('5881092dd818bf5cf8a3ddb793fbcba74097d5c526a6d35f97b83351940f2cc8');
}
it('should not treat a browser with a process polyfill as Node.js', function () {
var sandbox = { process: { versions: { node: '18.0.0' }, env: {} } };
sandbox.window = sandbox.self = sandbox;
check(run(sandbox));
});
it('should not treat an Electron renderer as Node.js', function () {
var sandbox = { process: { type: 'renderer', versions: { node: '20.0.0' } } };
sandbox.window = sandbox.self = sandbox;
check(run(sandbox));
});
it('should expose to the global scope in a web worker', function () {
var sandbox = {};
sandbox.self = sandbox;
check(run(sandbox));
});
it('should load in a browser without ArrayBuffer', function () {
var sandbox = {};
sandbox.window = sandbox;
check(run(sandbox, 'delete ArrayBuffer; delete Uint8Array; delete Uint32Array; delete DataView;'));
});
});
// webworker // webworker
WORKER = 'tests/worker.js'; WORKER = 'tests/worker.js';
SOURCE = 'src/sha3.js'; SOURCE = 'src/sha3.js';
+5
View File
@@ -257,6 +257,11 @@
'a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26': new 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]) '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 view'] = {
'd9ba1ec23c0d273be9e6689ccb573478226fa909ab53dc0dd8ff3ff1d342a5246b69dcbad5cecba577aaabc5595939e6f3e8ae30778f8a35f88d265c4ca683f7': new Int8Array([-1, -128, 65, 0, -2]),
'db986a889a9a39d6f1db1250ceebd35b943b15d7c0466979f2314801992f380bc7b0c76d20234dfbd1e2afcd13cc858f4a66c04b24fab037845e2c3a9b45af43': new Uint16Array(new Uint8Array([2, 1]).buffer),
'8143d62fff557e3c37e15a7e7e1be8dd031401cd55da9ca74237e70f8c9d0f20bad8a2af7b22986e56ee6a704ee365f79f83fe7fbfe0c359d7caecc8030f6af5': new DataView(new Uint8Array([9, 72, 69, 76, 76, 79, 9]).buffer, 1, 5)
};
testCases.sha3_512.ArrayBuffer = { testCases.sha3_512.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 '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