mirror of
https://github.com/emn178/js-sha256.git
synced 2026-11-17 15:38:56 +08:00
### 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:
co-authored by
Claude Opus 5.5
parent
9a54fb31d4
commit
91494a72ab
+19
-7
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user