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';
|
||||
|
||||
Reference in New Issue
Block a user