### 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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yi-Cyuan Chen
2026-11-17 10:22:20 +08:00
co-authored by Claude Opus 5.5
parent 9a54fb31d4
commit 91494a72ab
12 changed files with 97 additions and 29 deletions
+4
View File
@@ -1,5 +1,9 @@
# Change Log
## 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.
## v1.0.0 / 2026-07-27
### Added
- Add dedicated ESM, CommonJS, Node.js, and browser distribution entry points.
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "js-sha256",
"version": "1.0.0",
"version": "1.0.1",
"main": ["build/sha256.js"],
"ignore": [
"samples",
+19 -7
View File
@@ -1,7 +1,7 @@
/**
* [js-sha256]{@link https://github.com/emn178/js-sha256}
*
* @version 1.0.0
* @version 1.0.1
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -134,7 +134,7 @@ Sha256.prototype.update = function (message) {
var result = formatMessage(message);
message = result[0];
var isString = result[1];
var code, index = 0, i, length = message.length, blocks = this.blocks;
var code, code2, index = 0, i, length = message.length, blocks = this.blocks;
while (index < length) {
if (this.hashed) {
this.hashed = false;
@@ -157,12 +157,18 @@ Sha256.prototype.update = function (message) {
blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
} else if (code < 0xdc00 && (code2 = message.charCodeAt(index + 1)) >= 0xdc00 && code2 < 0xe000) {
++index;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
// unpaired surrogate: U+FFFD
blocks[i >>> 2] |= 0xef << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbf << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbd << SHIFT[i++ & 3];
}
}
} else {
@@ -382,7 +388,7 @@ function HmacSha256(key, is224, sharedMemory) {
var i, result = formatMessage(key);
key = result[0];
if (result[1]) {
var bytes = [], length = key.length, index = 0, code;
var bytes = [], length = key.length, index = 0, code, code2;
for (i = 0; i < length; ++i) {
code = key.charCodeAt(i);
if (code < 0x80) {
@@ -394,12 +400,18 @@ function HmacSha256(key, is224, sharedMemory) {
bytes[index++] = (0xe0 | (code >>> 12));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
} else if (code < 0xdc00 && (code2 = key.charCodeAt(i + 1)) >= 0xdc00 && code2 < 0xe000) {
++i;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
bytes[index++] = (0xf0 | (code >>> 18));
bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
// unpaired surrogate: U+FFFD
bytes[index++] = 0xef;
bytes[index++] = 0xbf;
bytes[index++] = 0xbd;
}
}
key = bytes;
+19 -7
View File
@@ -1,7 +1,7 @@
/**
* [js-sha256]{@link https://github.com/emn178/js-sha256}
*
* @version 1.0.0
* @version 1.0.1
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
@@ -138,7 +138,7 @@
var result = formatMessage(message);
message = result[0];
var isString = result[1];
var code, index = 0, i, length = message.length, blocks = this.blocks;
var code, code2, index = 0, i, length = message.length, blocks = this.blocks;
while (index < length) {
if (this.hashed) {
this.hashed = false;
@@ -161,12 +161,18 @@
blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
} else if (code < 0xdc00 && (code2 = message.charCodeAt(index + 1)) >= 0xdc00 && code2 < 0xe000) {
++index;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
// unpaired surrogate: U+FFFD
blocks[i >>> 2] |= 0xef << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbf << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbd << SHIFT[i++ & 3];
}
}
} else {
@@ -386,7 +392,7 @@
var i, result = formatMessage(key);
key = result[0];
if (result[1]) {
var bytes = [], length = key.length, index = 0, code;
var bytes = [], length = key.length, index = 0, code, code2;
for (i = 0; i < length; ++i) {
code = key.charCodeAt(i);
if (code < 0x80) {
@@ -398,12 +404,18 @@
bytes[index++] = (0xe0 | (code >>> 12));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
} else if (code < 0xdc00 && (code2 = key.charCodeAt(i + 1)) >= 0xdc00 && code2 < 0xe000) {
++i;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
bytes[index++] = (0xf0 | (code >>> 18));
bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
// unpaired surrogate: U+FFFD
bytes[index++] = 0xef;
bytes[index++] = 0xbf;
bytes[index++] = 0xbd;
}
}
key = bytes;
+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
+1 -1
View File
@@ -1,7 +1,7 @@
/**
* [js-sha256]{@link https://github.com/emn178/js-sha256}
*
* @version 1.0.0
* @version 1.0.1
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "js-sha256",
"version": "1.0.0",
"version": "1.0.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "js-sha256",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"devDependencies": {
"@playwright/test": "^1.62.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "js-sha256",
"version": "1.0.0",
"version": "1.0.1",
"description": "A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.",
"main": "build/sha256.node.cjs",
"module": "src/index.mjs",
+18 -6
View File
@@ -103,7 +103,7 @@ Sha256.prototype.update = function (message) {
var result = formatMessage(message);
message = result[0];
var isString = result[1];
var code, index = 0, i, length = message.length, blocks = this.blocks;
var code, code2, index = 0, i, length = message.length, blocks = this.blocks;
while (index < length) {
if (this.hashed) {
this.hashed = false;
@@ -126,12 +126,18 @@ Sha256.prototype.update = function (message) {
blocks[i >>> 2] |= (0xe0 | (code >>> 12)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
} else if (code < 0xdc00 && (code2 = message.charCodeAt(index + 1)) >= 0xdc00 && code2 < 0xe000) {
++index;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
blocks[i >>> 2] |= (0xf0 | (code >>> 18)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 12) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | ((code >>> 6) & 0x3f)) << SHIFT[i++ & 3];
blocks[i >>> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
} else {
// unpaired surrogate: U+FFFD
blocks[i >>> 2] |= 0xef << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbf << SHIFT[i++ & 3];
blocks[i >>> 2] |= 0xbd << SHIFT[i++ & 3];
}
}
} else {
@@ -351,7 +357,7 @@ function HmacSha256(key, is224, sharedMemory) {
var i, result = formatMessage(key);
key = result[0];
if (result[1]) {
var bytes = [], length = key.length, index = 0, code;
var bytes = [], length = key.length, index = 0, code, code2;
for (i = 0; i < length; ++i) {
code = key.charCodeAt(i);
if (code < 0x80) {
@@ -363,12 +369,18 @@ function HmacSha256(key, is224, sharedMemory) {
bytes[index++] = (0xe0 | (code >>> 12));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
} else if (code < 0xdc00 && (code2 = key.charCodeAt(i + 1)) >= 0xdc00 && code2 < 0xe000) {
++i;
code = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
bytes[index++] = (0xf0 | (code >>> 18));
bytes[index++] = (0x80 | ((code >>> 12) & 0x3f));
bytes[index++] = (0x80 | ((code >>> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
// unpaired surrogate: U+FFFD
bytes[index++] = 0xef;
bytes[index++] = 0xbf;
bytes[index++] = 0xbd;
}
}
key = bytes;
+8
View File
@@ -41,6 +41,10 @@
'865cc329d317f6d9fdbd183a3c5cc5fd4c370d11f98abbbb404bceb1e6392c7e': ['中文', '中文'],
'efeef87be5731506b69bb64a9898a456dd12c94834c36a4d8ba99e3db79ad7ed': ['aécio', 'aécio'],
'8a6e527049b9cfc7e1c84bcf356a1289c95da68a586c03de3327f3de0d3737fe': ['𠜎', '𠜎']
},
'UTF8 unpaired surrogates': {
'f029dbf2fb1aaaa143d508d2eb3f43660013daeb46896f058536c41bc114523d': ['\ud800', 'a\ud83db'],
'c01f3408e4acbf0129522609f7d26d3f20666b40e6faa0b50c7f592c7e6e739e': ['a\udc00b', '\ud800']
}
},
sha224_hmac: {
@@ -74,6 +78,10 @@
'e2280928fe813aeb7fa59aa14dd5e589041bfdf91945d19d25b9f3db': ['中文', '中文'],
'86c53dc054b16f6e006a254891bc9ff0da5df8e1a6faee3b0aaa732d': ['aécio', 'aécio'],
'e9e5991bfb84506b105f800afac1599ff807bb8e20db8ffda48997b9': ['𠜎', '𠜎']
},
'UTF8 unpaired surrogates': {
'9869bc56758479ea8e98c51119db2de42d91ce77166fd43c0e006e20': ['\ud800', 'a\ud83db'],
'5d4a0832f4e767b2ada5855f9b65645741dba4ecc89559bc695da390': ['a\udc00b', '\ud800']
}
}
};
+20
View File
@@ -29,6 +29,12 @@
'd691014feebf35b3500ef6f6738d0094cac63628a7a018a980a40292a77703d1': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
'81a1472ebdeb09406a783d607ff49ee2fde3e9f44ac1cd158ad8d6ad3c4e69fa': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。'
},
'UTF8 unpaired surrogates': {
'83d544ccc223c057d2bf80d3f2a32982c32c3c0db8e2674820da5064783fb097': '\ud800',
'05087813392efc16fe8ff448920c6328e53af865df39419436659d9ffda90f7b': 'a\ud83db',
'9b032698073e49a0c207eec31b288e75435afbbeb025cece0486f3a2f34befc7': '\ud83d\ud83d\ude00\udfff',
'44a101d453287d6360d95a104e34ac6f5b329fa9cb7b225c59e762a2a337ba63': Array(63).join('a') + '\ud800b'
},
'special length': {
'5e6b963e2b6444dab8544beab8532850cef2a9d143872a6a5384abe37e61b3db': '0123456780123456780123456780123456780123456780123456780',
'85d240a4a03a0710423fc4f701da51e8785c9eaa96d718ab1c7991d6afd60d62': '01234567801234567801234567801234567801234567801234567801',
@@ -62,6 +68,12 @@
'0dda421f3f81272418e1313673e9d74b7f2d04efc9c52c69458e12c3': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一',
'a8cb74a54e6dc6ab6110db3915ba08ffe5e1abafaea78538fa12a626': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。'
},
'UTF8 unpaired surrogates': {
'150865d98f78afe3ef15ff630e6533ce657e6a57bff43d5e79f9ec98': '\ud800',
'bb32ef954c48214329decb4899e01246c48d32ecb0864ba9923002b2': 'a\ud83db',
'9e38824afbac81e64c82a11ab7c885c2da317e3321ced52a0828d3b2': '\ud83d\ud83d\ude00\udfff',
'35409c50c0fe1461a3afc0d2a66ab2fe15053b62042aabf12eb205c2': Array(63).join('a') + '\ud800b'
},
'special length': {
'bc4a354d66f3cff4bc6dd6a88fbb0435cede7fd5fe94da0760cb1924': '0123456780123456780123456780123456780123456780123456780',
'2f148f757d1295784a7c69bf328b8bf827a536669e132234cd6f50e7': '01234567801234567801234567801234567801234567801234567801',
@@ -240,6 +252,14 @@
});
});
context('when a surrogate pair is split across updates', function () {
it('should encode each unpaired half as U+FFFD', function () {
var expected = name === 'sha256' ? ['df6bc292638d56dc79730424c8c70e0b90271d0ee49a96462e3b1c2fbc37b810', '05087813392efc16fe8ff448920c6328e53af865df39419436659d9ffda90f7b'] : ['bca4aabe305ab5d43b0143f884eb75e93e85532037344aea0fb53ae7', 'bb32ef954c48214329decb4899e01246c48d32ecb0864ba9923002b2'];
assert.equal(algorithm.update('a\ud83d').update('\ude00b').hex(), expected[0]);
assert.equal(algorithm.update('a\ud83d').update('b').hex(), expected[1]);
});
});
context('when update after finalize', function () {
it('should throw error', function () {
assert.throws(function () {