### Fixed

- Incorrect hash for `DataView` input, and for `Int8Array` with negative values in browsers.

### 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:59 +08:00
co-authored by Claude Opus 5.5
parent e57611d11f
commit 0f3bc529d4
13 changed files with 38 additions and 17 deletions
+7
View File
@@ -1,5 +1,12 @@
# Change Log
## v1.1.0 / 2026-11-18
### Fixed
- Incorrect hash for `DataView` input, and for `Int8Array` with negative values in browsers.
### 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.
## v1.0.1 / 2026-11-17
### Fixed
- Unpaired UTF-16 surrogates in string input (including HMAC keys) produced wrong digests and consumed the following code unit in the browser/core implementation. They are now encoded as U+FFFD, matching `TextEncoder` and the Node.js implementation.
+1 -1
View File
@@ -110,7 +110,7 @@ sha224('The quick brown fox jumps over the lazy dog.'); // 619cba8e8e05826e9b8c5
sha256('中文'); // 72726d8818f693066ceb69afa364218b692e62ea92b385782363780f47529c21
sha224('中文'); // dfbab71afdf54388af4d55f8bd3de8c9b15e0eb916bf9125f4a959d4
// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` input
// It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` and other `ArrayBuffer` views (their underlying bytes are hashed)
sha256([]); // e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
sha256(new Uint8Array([211, 212])); // 182889f925ae4e5cc37118ded6ed87f7bdc7cab5ec5e78faef2e50048999473f
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "js-sha256",
"version": "1.0.1",
"version": "1.1.0",
"main": ["build/sha256.js"],
"ignore": [
"samples",
+3 -2
View File
@@ -1,7 +1,7 @@
/**
* [js-sha256]{@link https://github.com/emn178/js-sha256}
*
* @version 1.0.1
* @version 1.1.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -25,7 +25,8 @@ var formatMessage = function (message) {
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
} else if (ArrayBuffer.isView(message)) {
return [message, false];
// read the underlying bytes of any view (Int8Array, DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
}
}
throw new Error(INPUT_ERROR);
+3 -2
View File
@@ -1,7 +1,7 @@
/**
* [js-sha256]{@link https://github.com/emn178/js-sha256}
*
* @version 1.0.1
* @version 1.1.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -29,7 +29,8 @@
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
} else if (ArrayBuffer.isView(message)) {
return [message, false];
// read the underlying bytes of any view (Int8Array, DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
}
}
throw new Error(INPUT_ERROR);
+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
+3 -2
View File
@@ -1,7 +1,7 @@
/**
* [js-sha256]{@link https://github.com/emn178/js-sha256}
*
* @version 1.0.1
* @version 1.1.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -27,7 +27,8 @@ var formatMessage = function (message) {
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
} else if (ArrayBuffer.isView(message)) {
return [message, false];
// read the underlying bytes of any view (Int8Array, DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
}
}
throw new Error(INPUT_ERROR);
Vendored
+1 -1
View File
@@ -1,4 +1,4 @@
type Message = string | number[] | ArrayBuffer | Uint8Array;
type Message = string | number[] | ArrayBuffer | ArrayBufferView;
interface Hasher {
/**
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "js-sha256",
"version": "1.0.1",
"version": "1.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "js-sha256",
"version": "1.0.1",
"version": "1.1.0",
"license": "MIT",
"devDependencies": {
"@playwright/test": "^1.62.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "js-sha256",
"version": "1.0.1",
"version": "1.1.0",
"description": "A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.",
"main": "build/sha256.node.cjs",
"module": "src/index.mjs",
+2 -1
View File
@@ -15,7 +15,8 @@ export var formatMessage = function (message) {
if (message.constructor === ArrayBuffer) {
return [new Uint8Array(message), false];
} else if (ArrayBuffer.isView(message)) {
return [message, false];
// read the underlying bytes of any view (Int8Array, DataView, Uint16Array, ...)
return [new Uint8Array(message.buffer, message.byteOffset, message.byteLength), false];
}
}
throw new Error(INPUT_ERROR);
+10
View File
@@ -100,6 +100,11 @@
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': new ArrayBuffer(0),
'6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d': new ArrayBuffer(1)
};
testCases.sha256['ArrayBuffer view'] = {
'8885cb2424931045cbc3f70dec12f60f965993ebeb8636ec49e5011335227349': new Int8Array([-1, -128, 65, 0, -2]),
'25dfd29c09617dcc9852281c030e5b3037a338a4712a42a21c907f259c6412a0': new Uint16Array(new Uint8Array([2, 1]).buffer),
'3733cd977ff8eb18b987357e22ced99f46097f31ecb239e878ae63760e83e4d5': new DataView(new Uint8Array([9, 72, 69, 76, 76, 79, 9]).buffer, 1, 5)
};
testCases.sha224.Uint8Array = {
'e17541396a3ecd1cd5a2b968b84e597e8eae3b0ea3127963bf48dd3b': new Uint8Array([211, 212]),
'730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525': 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])
@@ -112,6 +117,11 @@
'fff9292b4201617bdc4d3053fce02734166a683d7d858a7f5f59b073': new ArrayBuffer(1),
};
testCases.sha224['ArrayBuffer view'] = {
'd1cb7c6fa5082db7f0dd9748e3e5018e071e3ec6719ed5f58973b145': new Int8Array([-1, -128, 65, 0, -2]),
'0ad90774f63f37bfe02760a853cf4f1fe8d1911849f47c9afe479f7e': new Uint16Array(new Uint8Array([2, 1]).buffer),
'ddb584a8a86bf5f4b700262bf5901f473d133869fac0ad86215ed2ec': new DataView(new Uint8Array([9, 72, 69, 76, 76, 79, 9]).buffer, 1, 5)
};
testCases.sha256.Buffer = {
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855': Buffer.from([]),
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592': 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]))