### Fixed

- Incorrect hash for `DataView` input, and for `Int8Array` with negative values in browsers.
- Throws `ArrayBuffer is not defined` in browsers without `ArrayBuffer`.

### 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:
Yi-Cyuan Chen
2026-11-18 13:31:57 +08:00
co-authored by Claude Opus 5.5
parent 9e1e7d1397
commit 2025a20cdf
13 changed files with 65 additions and 26 deletions
+8
View File
@@ -1,5 +1,13 @@
# Change Log # Change Log
## v0.9.0 / 2026-11-18
### Fixed
- Incorrect hash for `DataView` input, and for `Int8Array` with negative values in browsers.
- Throws `ArrayBuffer is not defined` in browsers without `ArrayBuffer`.
### 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.8.0 / 2026-11-17 ## v0.8.0 / 2026-11-17
### Added ### Added
- ESM builds and package `exports`. - ESM builds and package `exports`.
+1 -1
View File
@@ -89,7 +89,7 @@ sha1('中文'); // 7be2d2d20c106eee0836c9bc2b939890a78e8fb3
// Unpaired surrogates are encoded as U+FFFD, the same as TextEncoder // Unpaired surrogates are encoded as U+FFFD, the same as TextEncoder
sha1('\ud800'); // 9bdb77276c1852e1fb067820472812fcf6084024 sha1('\ud800'); // 9bdb77276c1852e1fb067820472812fcf6084024
// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` and other `ArrayBuffer` views (their underlying bytes are hashed)
sha1([]); // da39a3ee5e6b4b0d3255bfef95601890afd80709 sha1([]); // da39a3ee5e6b4b0d3255bfef95601890afd80709
sha1(new Uint8Array([])); // da39a3ee5e6b4b0d3255bfef95601890afd80709 sha1(new Uint8Array([])); // da39a3ee5e6b4b0d3255bfef95601890afd80709
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-sha1", "name": "js-sha1",
"version": "0.8.0", "version": "0.9.0",
"main": ["src/sha1.js"], "main": ["src/sha1.js"],
"ignore": [ "ignore": [
"samples", "samples",
+2 -2
View File
File diff suppressed because one or more lines are too long
+3 -3
View File
File diff suppressed because one or more lines are too long
+8 -4
View File
@@ -7,7 +7,7 @@ var sha1$1 = {exports: {}};
/* /*
* [js-sha1]{@link https://github.com/emn178/js-sha1} * [js-sha1]{@link https://github.com/emn178/js-sha1}
* *
* @version 0.8.0 * @version 0.9.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026 * @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT * @license MIT
@@ -44,7 +44,7 @@ var sha1$1 = {exports: {}};
}; };
} }
var isView = ArrayBuffer.isView; var isView = ARRAY_BUFFER ? ArrayBuffer.isView : null;
if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) { if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) { isView = function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
@@ -63,10 +63,14 @@ var sha1$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 createOutputMethod = function (outputType) { var createOutputMethod = function (outputType) {
+10 -4
View File
@@ -12,7 +12,7 @@ var sha1$1 = {exports: {}};
/* /*
* [js-sha1]{@link https://github.com/emn178/js-sha1} * [js-sha1]{@link https://github.com/emn178/js-sha1}
* *
* @version 0.8.0 * @version 0.9.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026 * @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT * @license MIT
@@ -52,7 +52,7 @@ var sha1$1 = {exports: {}};
}; };
} }
var isView = ArrayBuffer.isView; var isView = ARRAY_BUFFER ? ArrayBuffer.isView : null;
if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) { if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) { isView = function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
@@ -71,10 +71,14 @@ var sha1$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 createOutputMethod = function (outputType) { var createOutputMethod = function (outputType) {
@@ -120,6 +124,8 @@ var sha1$1 = {exports: {}};
throw new Error(INPUT_ERROR); throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) { } else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message); message = new Uint8Array(message);
} else if (ARRAY_BUFFER && isView(message) && message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
} }
} }
if (isArray(message) || isView(message) || if (isArray(message) || isView(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-sha1", "name": "js-sha1",
"version": "0.8.0", "version": "0.9.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "js-sha1", "name": "js-sha1",
"version": "0.8.0", "version": "0.9.0",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@rollup/plugin-commonjs": "^29.0.3", "@rollup/plugin-commonjs": "^29.0.3",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "js-sha1", "name": "js-sha1",
"version": "0.8.0", "version": "0.9.0",
"description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.", "description": "A simple SHA1 hash function for JavaScript supports UTF-8 encoding.",
"main": "src/sha1.js", "main": "src/sha1.js",
"module": "build/sha1.mjs", "module": "build/sha1.mjs",
+11 -5
View File
@@ -1,7 +1,7 @@
/* /*
* [js-sha1]{@link https://github.com/emn178/js-sha1} * [js-sha1]{@link https://github.com/emn178/js-sha1}
* *
* @version 0.8.0 * @version 0.9.0
* @author Chen, Yi-Cyuan [emn178@gmail.com] * @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026 * @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT * @license MIT
@@ -41,7 +41,7 @@
}; };
} }
var isView = ArrayBuffer.isView; var isView = ARRAY_BUFFER ? ArrayBuffer.isView : null;
if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) { if (ARRAY_BUFFER && (root.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW || !isView)) {
isView = function (obj) { isView = function (obj) {
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
@@ -60,11 +60,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 createOutputMethod = function (outputType) { var createOutputMethod = function (outputType) {
return function (message) { return function (message) {
@@ -109,6 +113,8 @@
throw new Error(INPUT_ERROR); throw new Error(INPUT_ERROR);
} else if (message.constructor === ArrayBuffer) { } else if (message.constructor === ArrayBuffer) {
message = new Uint8Array(message); message = new Uint8Array(message);
} else if (ARRAY_BUFFER && isView(message) && message.constructor !== Buffer) {
message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
} }
} }
if (isArray(message) || isView(message) || if (isArray(message) || isView(message) ||
+12 -2
View File
@@ -92,8 +92,12 @@ describe('environment detection', function () {
var path = require('path'); var path = require('path');
var source = fs.readFileSync(path.join(__dirname, '../src/sha1.js'), 'utf8'); var source = fs.readFileSync(path.join(__dirname, '../src/sha1.js'), 'utf8');
function run(sandbox) { function run(sandbox, script) {
vm.runInContext(source, vm.createContext(sandbox)); var context = vm.createContext(sandbox);
if (script) {
vm.runInContext(script, context);
}
vm.runInContext(source, context);
return sandbox.sha1; return sandbox.sha1;
} }
@@ -120,6 +124,12 @@ describe('environment detection', function () {
sandbox.self = sandbox; sandbox.self = sandbox;
check(run(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 DataView;'));
});
}); });
// webworker // webworker
+5
View File
@@ -55,6 +55,11 @@
testCases['Int8Array'] = { testCases['Int8Array'] = {
'2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': new Int8Array([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]) '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12': new Int8Array([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['ArrayBuffer view'] = {
'd078b824154163b1b6cc79e35aa7954c06201354': new Int8Array([-1, -128, 65, 0, -2]),
'c92920944247d80c842eaa65fd01efec1c84c342': new Uint16Array(new Uint8Array([2, 1]).buffer),
'c65f99f8c5376adadddc46d5cbcf5762f9e55eb7': new DataView(new Uint8Array([9, 72, 69, 76, 76, 79, 9]).buffer, 1, 5)
};
testCases['ArrayBuffer'] = { testCases['ArrayBuffer'] = {
'5ba93c9db0cff93f52b521d7420e43f6eda2784f': new ArrayBuffer(1) '5ba93c9db0cff93f52b521d7420e43f6eda2784f': new ArrayBuffer(1)
}; };