mirror of
https://github.com/emn178/js-sha3.git
synced 2026-11-18 16:38:46 +08:00
### 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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
290d679fbd
commit
9c5eb82a0d
@@ -147,6 +147,53 @@ define.amd = true;
|
||||
require('../src/sha3.js');
|
||||
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
|
||||
WORKER = 'tests/worker.js';
|
||||
SOURCE = 'src/sha3.js';
|
||||
|
||||
@@ -257,6 +257,11 @@
|
||||
'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 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 = {
|
||||
'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
|
||||
|
||||
Reference in New Issue
Block a user