From 91494a72ab17eb0abc07d6bc143a692648240519 Mon Sep 17 00:00:00 2001 From: Yi-Cyuan Chen Date: Tue, 17 Nov 2026 10:22:20 +0800 Subject: [PATCH] ### 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 --- CHANGELOG.md | 4 ++++ bower.json | 2 +- build/sha256.cjs | 26 +++++++++++++++++++------- build/sha256.js | 26 +++++++++++++++++++------- build/sha256.min.js | 4 ++-- build/sha256.min.mjs | 4 ++-- build/sha256.node.cjs | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/core.mjs | 24 ++++++++++++++++++------ tests/hmac-test.js | 8 ++++++++ tests/test.js | 20 ++++++++++++++++++++ 12 files changed, 97 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad766fc..9e38e69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/bower.json b/bower.json index 760b217..a17df47 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-sha256", - "version": "1.0.0", + "version": "1.0.1", "main": ["build/sha256.js"], "ignore": [ "samples", diff --git a/build/sha256.cjs b/build/sha256.cjs index e3a0863..3eac954 100644 --- a/build/sha256.cjs +++ b/build/sha256.cjs @@ -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; diff --git a/build/sha256.js b/build/sha256.js index edb15da..75f5b0f 100644 --- a/build/sha256.js +++ b/build/sha256.js @@ -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; diff --git a/build/sha256.min.js b/build/sha256.min.js index 520a555..40699fb 100644 --- a/build/sha256.min.js +++ b/build/sha256.min.js @@ -1,9 +1,9 @@ /** * [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 */ -!function(t,h){"object"==typeof exports&&"undefined"!=typeof module?module.exports=h():"function"==typeof define&&define.amd?define(h):(t="undefined"!=typeof globalThis?globalThis:t||self).sha256=h()}(this,function(){"use strict";var t="undefined"!=typeof ArrayBuffer,h=function(h){if("string"===typeof h)return[h,!0];if(Array.isArray(h))return[h,!1];if(t&&h){if(h.constructor===ArrayBuffer)return[new Uint8Array(h),!1];if(ArrayBuffer.isView(h))return[h,!1]}throw new Error("input is invalid type")},i="0123456789abcdef".split(""),s=[-2147483648,8388608,32768,128],e=[24,16,8,0],r=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],n=["hex","array","digest","arrayBuffer"],o=[],a=function(t,h){return function(i){return new c(h,!0).update(i)[t]()}},f=function(t){var h=a("hex",t);h.create=function(){return new c(t)},h.update=function(t){return h.create().update(t)};for(var i=0;i>>6,o[f++]=128|63&n):n<55296||n>=57344?(o[f++]=224|n>>>12,o[f++]=128|n>>>6&63,o[f++]=128|63&n):(n=65536+((1023&n)<<10|1023&t.charCodeAt(++e)),o[f++]=240|n>>>18,o[f++]=128|n>>>12&63,o[f++]=128|n>>>6&63,o[f++]=128|63&n);t=o}t.length>64&&(t=new c(i,!0).update(t).array());var u=[],l=[];for(e=0;e<64;++e){var y=t[e]||0;u[e]=92^y,l[e]=54^y}c.call(this,i,s),this.update(l),this.oKeyPad=u,this.inner=!0,this.sharedMemory=s}c.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var i=h(t);t=i[0];for(var s,r,n=i[1],o=0,a=t.length,f=this.blocks;o>>2]|=s<>>2]|=(192|s>>>6)<>>2]|=(128|63&s)<=57344?(f[r>>>2]|=(224|s>>>12)<>>2]|=(128|s>>>6&63)<>>2]|=(128|63&s)<>>2]|=(240|s>>>18)<>>2]|=(128|s>>>12&63)<>>2]|=(128|s>>>6&63)<>>2]|=(128|63&s)<>>2]|=t[o]<=64?(this.block=f[16],this.start=r-64,this.hash(),this.hashed=!0):this.start=r}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296|0,this.bytes=this.bytes%4294967296),this},c.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,h=this.lastByteIndex;t[16]=this.block,t[h>>>2]|=s[3&h],this.block=t[16],h>=56&&(this.hashed||this.hash(),t[0]=this.block,t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.hBytes<<3|this.bytes>>>29,t[15]=this.bytes<<3,this.hash()}},c.prototype.hash=function(){var t,h,i,s,e,n,o,a,f,u=this.h0,l=this.h1,c=this.h2,y=this.h3,p=this.h4,d=this.h5,b=this.h6,v=this.h7,w=this.blocks;for(t=16;t<64;++t)h=((e=w[t-15])>>>7|e<<25)^(e>>>18|e<<14)^e>>>3,i=((e=w[t-2])>>>17|e<<15)^(e>>>19|e<<13)^e>>>10,w[t]=w[t-16]+h+w[t-7]+i|0;for(f=l&c,t=0;t<64;t+=4)this.first?(this.is224?(n=300032,v=(e=w[0]-1413257819)-150054599|0,y=e+24177077|0):(n=704751109,v=(e=w[0]-210244248)-1521486534|0,y=e+143694565|0),this.first=!1):(h=(u>>>2|u<<30)^(u>>>13|u<<19)^(u>>>22|u<<10),s=(n=u&l)^u&c^f,v=y+(e=v+(i=(p>>>6|p<<26)^(p>>>11|p<<21)^(p>>>25|p<<7))+(p&d^~p&b)+r[t]+w[t])|0,y=e+(h+s)|0),h=(y>>>2|y<<30)^(y>>>13|y<<19)^(y>>>22|y<<10),s=(o=y&u)^y&l^n,b=c+(e=b+(i=(v>>>6|v<<26)^(v>>>11|v<<21)^(v>>>25|v<<7))+(v&p^~v&d)+r[t+1]+w[t+1])|0,h=((c=e+(h+s)|0)>>>2|c<<30)^(c>>>13|c<<19)^(c>>>22|c<<10),s=(a=c&y)^c&u^o,d=l+(e=d+(i=(b>>>6|b<<26)^(b>>>11|b<<21)^(b>>>25|b<<7))+(b&v^~b&p)+r[t+2]+w[t+2])|0,h=((l=e+(h+s)|0)>>>2|l<<30)^(l>>>13|l<<19)^(l>>>22|l<<10),s=(f=l&c)^l&y^a,p=u+(e=p+(i=(d>>>6|d<<26)^(d>>>11|d<<21)^(d>>>25|d<<7))+(d&b^~d&v)+r[t+3]+w[t+3])|0,u=e+(h+s)|0,this.chromeBugWorkAround=!0;this.h0=this.h0+u|0,this.h1=this.h1+l|0,this.h2=this.h2+c|0,this.h3=this.h3+y|0,this.h4=this.h4+p|0,this.h5=this.h5+d|0,this.h6=this.h6+b|0,this.h7=this.h7+v|0},c.prototype.hex=function(){this.finalize();var t=this.h0,h=this.h1,s=this.h2,e=this.h3,r=this.h4,n=this.h5,o=this.h6,a=this.h7,f=i[t>>>28&15]+i[t>>>24&15]+i[t>>>20&15]+i[t>>>16&15]+i[t>>>12&15]+i[t>>>8&15]+i[t>>>4&15]+i[15&t]+i[h>>>28&15]+i[h>>>24&15]+i[h>>>20&15]+i[h>>>16&15]+i[h>>>12&15]+i[h>>>8&15]+i[h>>>4&15]+i[15&h]+i[s>>>28&15]+i[s>>>24&15]+i[s>>>20&15]+i[s>>>16&15]+i[s>>>12&15]+i[s>>>8&15]+i[s>>>4&15]+i[15&s]+i[e>>>28&15]+i[e>>>24&15]+i[e>>>20&15]+i[e>>>16&15]+i[e>>>12&15]+i[e>>>8&15]+i[e>>>4&15]+i[15&e]+i[r>>>28&15]+i[r>>>24&15]+i[r>>>20&15]+i[r>>>16&15]+i[r>>>12&15]+i[r>>>8&15]+i[r>>>4&15]+i[15&r]+i[n>>>28&15]+i[n>>>24&15]+i[n>>>20&15]+i[n>>>16&15]+i[n>>>12&15]+i[n>>>8&15]+i[n>>>4&15]+i[15&n]+i[o>>>28&15]+i[o>>>24&15]+i[o>>>20&15]+i[o>>>16&15]+i[o>>>12&15]+i[o>>>8&15]+i[o>>>4&15]+i[15&o];return this.is224||(f+=i[a>>>28&15]+i[a>>>24&15]+i[a>>>20&15]+i[a>>>16&15]+i[a>>>12&15]+i[a>>>8&15]+i[a>>>4&15]+i[15&a]),f},c.prototype.toString=c.prototype.hex,c.prototype.digest=function(){this.finalize();var t=this.h0,h=this.h1,i=this.h2,s=this.h3,e=this.h4,r=this.h5,n=this.h6,o=this.h7,a=[t>>>24&255,t>>>16&255,t>>>8&255,255&t,h>>>24&255,h>>>16&255,h>>>8&255,255&h,i>>>24&255,i>>>16&255,i>>>8&255,255&i,s>>>24&255,s>>>16&255,s>>>8&255,255&s,e>>>24&255,e>>>16&255,e>>>8&255,255&e,r>>>24&255,r>>>16&255,r>>>8&255,255&r,n>>>24&255,n>>>16&255,n>>>8&255,255&n];return this.is224||a.push(o>>>24&255,o>>>16&255,o>>>8&255,255&o),a},c.prototype.array=c.prototype.digest,c.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(this.is224?28:32),h=new DataView(t);return h.setUint32(0,this.h0),h.setUint32(4,this.h1),h.setUint32(8,this.h2),h.setUint32(12,this.h3),h.setUint32(16,this.h4),h.setUint32(20,this.h5),h.setUint32(24,this.h6),this.is224||h.setUint32(28,this.h7),t},y.prototype=new c,y.prototype.finalize=function(){if(c.prototype.finalize.call(this),this.inner){this.inner=!1;var t=this.array();c.call(this,this.is224,this.sharedMemory),this.update(this.oKeyPad),this.update(t),c.prototype.finalize.call(this)}};var p=f(),d=f(!0);p.sha256=p,p.sha224=d,p.hmac=l(),d.hmac=l(!0);const b="object"==typeof globalThis?globalThis:"object"==typeof self?self:"object"==typeof window?window:"object"==typeof global?global:void 0;return b&&(b.sha224=d),p}); +!function(t,h){"object"==typeof exports&&"undefined"!=typeof module?module.exports=h():"function"==typeof define&&define.amd?define(h):(t="undefined"!=typeof globalThis?globalThis:t||self).sha256=h()}(this,function(){"use strict";var t="undefined"!=typeof ArrayBuffer,h=function(h){if("string"===typeof h)return[h,!0];if(Array.isArray(h))return[h,!1];if(t&&h){if(h.constructor===ArrayBuffer)return[new Uint8Array(h),!1];if(ArrayBuffer.isView(h))return[h,!1]}throw new Error("input is invalid type")},i="0123456789abcdef".split(""),s=[-2147483648,8388608,32768,128],e=[24,16,8,0],r=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],n=["hex","array","digest","arrayBuffer"],o=[],a=function(t,h){return function(i){return new c(h,!0).update(i)[t]()}},f=function(t){var h=a("hex",t);h.create=function(){return new c(t)},h.update=function(t){return h.create().update(t)};for(var i=0;i>>6,a[u++]=128|63&n):n<55296||n>=57344?(a[u++]=224|n>>>12,a[u++]=128|n>>>6&63,a[u++]=128|63&n):n<56320&&(o=t.charCodeAt(e+1))>=56320&&o<57344?(++e,n=65536+((1023&n)<<10|1023&o),a[u++]=240|n>>>18,a[u++]=128|n>>>12&63,a[u++]=128|n>>>6&63,a[u++]=128|63&n):(a[u++]=239,a[u++]=191,a[u++]=189);t=a}t.length>64&&(t=new c(i,!0).update(t).array());var l=[],y=[];for(e=0;e<64;++e){var p=t[e]||0;l[e]=92^p,y[e]=54^p}c.call(this,i,s),this.update(y),this.oKeyPad=l,this.inner=!0,this.sharedMemory=s}c.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var i=h(t);t=i[0];for(var s,r,n,o=i[1],a=0,f=t.length,u=this.blocks;a>>2]|=s<>>2]|=(192|s>>>6)<>>2]|=(128|63&s)<=57344?(u[n>>>2]|=(224|s>>>12)<>>2]|=(128|s>>>6&63)<>>2]|=(128|63&s)<=56320&&r<57344?(++a,s=65536+((1023&s)<<10|1023&r),u[n>>>2]|=(240|s>>>18)<>>2]|=(128|s>>>12&63)<>>2]|=(128|s>>>6&63)<>>2]|=(128|63&s)<>>2]|=239<>>2]|=191<>>2]|=189<>>2]|=t[a]<=64?(this.block=u[16],this.start=n-64,this.hash(),this.hashed=!0):this.start=n}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296|0,this.bytes=this.bytes%4294967296),this},c.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,h=this.lastByteIndex;t[16]=this.block,t[h>>>2]|=s[3&h],this.block=t[16],h>=56&&(this.hashed||this.hash(),t[0]=this.block,t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.hBytes<<3|this.bytes>>>29,t[15]=this.bytes<<3,this.hash()}},c.prototype.hash=function(){var t,h,i,s,e,n,o,a,f,u=this.h0,l=this.h1,c=this.h2,y=this.h3,p=this.h4,d=this.h5,b=this.h6,v=this.h7,w=this.blocks;for(t=16;t<64;++t)h=((e=w[t-15])>>>7|e<<25)^(e>>>18|e<<14)^e>>>3,i=((e=w[t-2])>>>17|e<<15)^(e>>>19|e<<13)^e>>>10,w[t]=w[t-16]+h+w[t-7]+i|0;for(f=l&c,t=0;t<64;t+=4)this.first?(this.is224?(n=300032,v=(e=w[0]-1413257819)-150054599|0,y=e+24177077|0):(n=704751109,v=(e=w[0]-210244248)-1521486534|0,y=e+143694565|0),this.first=!1):(h=(u>>>2|u<<30)^(u>>>13|u<<19)^(u>>>22|u<<10),s=(n=u&l)^u&c^f,v=y+(e=v+(i=(p>>>6|p<<26)^(p>>>11|p<<21)^(p>>>25|p<<7))+(p&d^~p&b)+r[t]+w[t])|0,y=e+(h+s)|0),h=(y>>>2|y<<30)^(y>>>13|y<<19)^(y>>>22|y<<10),s=(o=y&u)^y&l^n,b=c+(e=b+(i=(v>>>6|v<<26)^(v>>>11|v<<21)^(v>>>25|v<<7))+(v&p^~v&d)+r[t+1]+w[t+1])|0,h=((c=e+(h+s)|0)>>>2|c<<30)^(c>>>13|c<<19)^(c>>>22|c<<10),s=(a=c&y)^c&u^o,d=l+(e=d+(i=(b>>>6|b<<26)^(b>>>11|b<<21)^(b>>>25|b<<7))+(b&v^~b&p)+r[t+2]+w[t+2])|0,h=((l=e+(h+s)|0)>>>2|l<<30)^(l>>>13|l<<19)^(l>>>22|l<<10),s=(f=l&c)^l&y^a,p=u+(e=p+(i=(d>>>6|d<<26)^(d>>>11|d<<21)^(d>>>25|d<<7))+(d&b^~d&v)+r[t+3]+w[t+3])|0,u=e+(h+s)|0,this.chromeBugWorkAround=!0;this.h0=this.h0+u|0,this.h1=this.h1+l|0,this.h2=this.h2+c|0,this.h3=this.h3+y|0,this.h4=this.h4+p|0,this.h5=this.h5+d|0,this.h6=this.h6+b|0,this.h7=this.h7+v|0},c.prototype.hex=function(){this.finalize();var t=this.h0,h=this.h1,s=this.h2,e=this.h3,r=this.h4,n=this.h5,o=this.h6,a=this.h7,f=i[t>>>28&15]+i[t>>>24&15]+i[t>>>20&15]+i[t>>>16&15]+i[t>>>12&15]+i[t>>>8&15]+i[t>>>4&15]+i[15&t]+i[h>>>28&15]+i[h>>>24&15]+i[h>>>20&15]+i[h>>>16&15]+i[h>>>12&15]+i[h>>>8&15]+i[h>>>4&15]+i[15&h]+i[s>>>28&15]+i[s>>>24&15]+i[s>>>20&15]+i[s>>>16&15]+i[s>>>12&15]+i[s>>>8&15]+i[s>>>4&15]+i[15&s]+i[e>>>28&15]+i[e>>>24&15]+i[e>>>20&15]+i[e>>>16&15]+i[e>>>12&15]+i[e>>>8&15]+i[e>>>4&15]+i[15&e]+i[r>>>28&15]+i[r>>>24&15]+i[r>>>20&15]+i[r>>>16&15]+i[r>>>12&15]+i[r>>>8&15]+i[r>>>4&15]+i[15&r]+i[n>>>28&15]+i[n>>>24&15]+i[n>>>20&15]+i[n>>>16&15]+i[n>>>12&15]+i[n>>>8&15]+i[n>>>4&15]+i[15&n]+i[o>>>28&15]+i[o>>>24&15]+i[o>>>20&15]+i[o>>>16&15]+i[o>>>12&15]+i[o>>>8&15]+i[o>>>4&15]+i[15&o];return this.is224||(f+=i[a>>>28&15]+i[a>>>24&15]+i[a>>>20&15]+i[a>>>16&15]+i[a>>>12&15]+i[a>>>8&15]+i[a>>>4&15]+i[15&a]),f},c.prototype.toString=c.prototype.hex,c.prototype.digest=function(){this.finalize();var t=this.h0,h=this.h1,i=this.h2,s=this.h3,e=this.h4,r=this.h5,n=this.h6,o=this.h7,a=[t>>>24&255,t>>>16&255,t>>>8&255,255&t,h>>>24&255,h>>>16&255,h>>>8&255,255&h,i>>>24&255,i>>>16&255,i>>>8&255,255&i,s>>>24&255,s>>>16&255,s>>>8&255,255&s,e>>>24&255,e>>>16&255,e>>>8&255,255&e,r>>>24&255,r>>>16&255,r>>>8&255,255&r,n>>>24&255,n>>>16&255,n>>>8&255,255&n];return this.is224||a.push(o>>>24&255,o>>>16&255,o>>>8&255,255&o),a},c.prototype.array=c.prototype.digest,c.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(this.is224?28:32),h=new DataView(t);return h.setUint32(0,this.h0),h.setUint32(4,this.h1),h.setUint32(8,this.h2),h.setUint32(12,this.h3),h.setUint32(16,this.h4),h.setUint32(20,this.h5),h.setUint32(24,this.h6),this.is224||h.setUint32(28,this.h7),t},y.prototype=new c,y.prototype.finalize=function(){if(c.prototype.finalize.call(this),this.inner){this.inner=!1;var t=this.array();c.call(this,this.is224,this.sharedMemory),this.update(this.oKeyPad),this.update(t),c.prototype.finalize.call(this)}};var p=f(),d=f(!0);p.sha256=p,p.sha224=d,p.hmac=l(),d.hmac=l(!0);const b="object"==typeof globalThis?globalThis:"object"==typeof self?self:"object"==typeof window?window:"object"==typeof global?global:void 0;return b&&(b.sha224=d),p}); diff --git a/build/sha256.min.mjs b/build/sha256.min.mjs index 6684513..9860f7a 100644 --- a/build/sha256.min.mjs +++ b/build/sha256.min.mjs @@ -1,9 +1,9 @@ /** * [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 */ -var t="undefined"!=typeof ArrayBuffer,h=function(h){if("string"===typeof h)return[h,!0];if(Array.isArray(h))return[h,!1];if(t&&h){if(h.constructor===ArrayBuffer)return[new Uint8Array(h),!1];if(ArrayBuffer.isView(h))return[h,!1]}throw new Error("input is invalid type")},i="0123456789abcdef".split(""),s=[-2147483648,8388608,32768,128],r=[24,16,8,0],e=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],n=["hex","array","digest","arrayBuffer"],a=[],o=function(t,h){return function(i){return new y(h,!0).update(i)[t]()}},f=function(t){var h=o("hex",t);h.create=function(){return new y(t)},h.update=function(t){return h.create().update(t)};for(var i=0;i>>6,a[f++]=128|63&n):n<55296||n>=57344?(a[f++]=224|n>>>12,a[f++]=128|n>>>6&63,a[f++]=128|63&n):(n=65536+((1023&n)<<10|1023&t.charCodeAt(++r)),a[f++]=240|n>>>18,a[f++]=128|n>>>12&63,a[f++]=128|n>>>6&63,a[f++]=128|63&n);t=a}t.length>64&&(t=new y(i,!0).update(t).array());var u=[],c=[];for(r=0;r<64;++r){var l=t[r]||0;u[r]=92^l,c[r]=54^l}y.call(this,i,s),this.update(c),this.oKeyPad=u,this.inner=!0,this.sharedMemory=s}y.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var i=h(t);t=i[0];for(var s,e,n=i[1],a=0,o=t.length,f=this.blocks;a>>2]|=s<>>2]|=(192|s>>>6)<>>2]|=(128|63&s)<=57344?(f[e>>>2]|=(224|s>>>12)<>>2]|=(128|s>>>6&63)<>>2]|=(128|63&s)<>>2]|=(240|s>>>18)<>>2]|=(128|s>>>12&63)<>>2]|=(128|s>>>6&63)<>>2]|=(128|63&s)<>>2]|=t[a]<=64?(this.block=f[16],this.start=e-64,this.hash(),this.hashed=!0):this.start=e}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296|0,this.bytes=this.bytes%4294967296),this},y.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,h=this.lastByteIndex;t[16]=this.block,t[h>>>2]|=s[3&h],this.block=t[16],h>=56&&(this.hashed||this.hash(),t[0]=this.block,t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.hBytes<<3|this.bytes>>>29,t[15]=this.bytes<<3,this.hash()}},y.prototype.hash=function(){var t,h,i,s,r,n,a,o,f,u=this.h0,c=this.h1,y=this.h2,l=this.h3,p=this.h4,d=this.h5,b=this.h6,v=this.h7,w=this.blocks;for(t=16;t<64;++t)h=((r=w[t-15])>>>7|r<<25)^(r>>>18|r<<14)^r>>>3,i=((r=w[t-2])>>>17|r<<15)^(r>>>19|r<<13)^r>>>10,w[t]=w[t-16]+h+w[t-7]+i|0;for(f=c&y,t=0;t<64;t+=4)this.first?(this.is224?(n=300032,v=(r=w[0]-1413257819)-150054599|0,l=r+24177077|0):(n=704751109,v=(r=w[0]-210244248)-1521486534|0,l=r+143694565|0),this.first=!1):(h=(u>>>2|u<<30)^(u>>>13|u<<19)^(u>>>22|u<<10),s=(n=u&c)^u&y^f,v=l+(r=v+(i=(p>>>6|p<<26)^(p>>>11|p<<21)^(p>>>25|p<<7))+(p&d^~p&b)+e[t]+w[t])|0,l=r+(h+s)|0),h=(l>>>2|l<<30)^(l>>>13|l<<19)^(l>>>22|l<<10),s=(a=l&u)^l&c^n,b=y+(r=b+(i=(v>>>6|v<<26)^(v>>>11|v<<21)^(v>>>25|v<<7))+(v&p^~v&d)+e[t+1]+w[t+1])|0,h=((y=r+(h+s)|0)>>>2|y<<30)^(y>>>13|y<<19)^(y>>>22|y<<10),s=(o=y&l)^y&u^a,d=c+(r=d+(i=(b>>>6|b<<26)^(b>>>11|b<<21)^(b>>>25|b<<7))+(b&v^~b&p)+e[t+2]+w[t+2])|0,h=((c=r+(h+s)|0)>>>2|c<<30)^(c>>>13|c<<19)^(c>>>22|c<<10),s=(f=c&y)^c&l^o,p=u+(r=p+(i=(d>>>6|d<<26)^(d>>>11|d<<21)^(d>>>25|d<<7))+(d&b^~d&v)+e[t+3]+w[t+3])|0,u=r+(h+s)|0,this.chromeBugWorkAround=!0;this.h0=this.h0+u|0,this.h1=this.h1+c|0,this.h2=this.h2+y|0,this.h3=this.h3+l|0,this.h4=this.h4+p|0,this.h5=this.h5+d|0,this.h6=this.h6+b|0,this.h7=this.h7+v|0},y.prototype.hex=function(){this.finalize();var t=this.h0,h=this.h1,s=this.h2,r=this.h3,e=this.h4,n=this.h5,a=this.h6,o=this.h7,f=i[t>>>28&15]+i[t>>>24&15]+i[t>>>20&15]+i[t>>>16&15]+i[t>>>12&15]+i[t>>>8&15]+i[t>>>4&15]+i[15&t]+i[h>>>28&15]+i[h>>>24&15]+i[h>>>20&15]+i[h>>>16&15]+i[h>>>12&15]+i[h>>>8&15]+i[h>>>4&15]+i[15&h]+i[s>>>28&15]+i[s>>>24&15]+i[s>>>20&15]+i[s>>>16&15]+i[s>>>12&15]+i[s>>>8&15]+i[s>>>4&15]+i[15&s]+i[r>>>28&15]+i[r>>>24&15]+i[r>>>20&15]+i[r>>>16&15]+i[r>>>12&15]+i[r>>>8&15]+i[r>>>4&15]+i[15&r]+i[e>>>28&15]+i[e>>>24&15]+i[e>>>20&15]+i[e>>>16&15]+i[e>>>12&15]+i[e>>>8&15]+i[e>>>4&15]+i[15&e]+i[n>>>28&15]+i[n>>>24&15]+i[n>>>20&15]+i[n>>>16&15]+i[n>>>12&15]+i[n>>>8&15]+i[n>>>4&15]+i[15&n]+i[a>>>28&15]+i[a>>>24&15]+i[a>>>20&15]+i[a>>>16&15]+i[a>>>12&15]+i[a>>>8&15]+i[a>>>4&15]+i[15&a];return this.is224||(f+=i[o>>>28&15]+i[o>>>24&15]+i[o>>>20&15]+i[o>>>16&15]+i[o>>>12&15]+i[o>>>8&15]+i[o>>>4&15]+i[15&o]),f},y.prototype.toString=y.prototype.hex,y.prototype.digest=function(){this.finalize();var t=this.h0,h=this.h1,i=this.h2,s=this.h3,r=this.h4,e=this.h5,n=this.h6,a=this.h7,o=[t>>>24&255,t>>>16&255,t>>>8&255,255&t,h>>>24&255,h>>>16&255,h>>>8&255,255&h,i>>>24&255,i>>>16&255,i>>>8&255,255&i,s>>>24&255,s>>>16&255,s>>>8&255,255&s,r>>>24&255,r>>>16&255,r>>>8&255,255&r,e>>>24&255,e>>>16&255,e>>>8&255,255&e,n>>>24&255,n>>>16&255,n>>>8&255,255&n];return this.is224||o.push(a>>>24&255,a>>>16&255,a>>>8&255,255&a),o},y.prototype.array=y.prototype.digest,y.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(this.is224?28:32),h=new DataView(t);return h.setUint32(0,this.h0),h.setUint32(4,this.h1),h.setUint32(8,this.h2),h.setUint32(12,this.h3),h.setUint32(16,this.h4),h.setUint32(20,this.h5),h.setUint32(24,this.h6),this.is224||h.setUint32(28,this.h7),t},l.prototype=new y,l.prototype.finalize=function(){if(y.prototype.finalize.call(this),this.inner){this.inner=!1;var t=this.array();y.call(this,this.is224,this.sharedMemory),this.update(this.oKeyPad),this.update(t),y.prototype.finalize.call(this)}};var p=f(),d=f(!0);p.sha256=p,p.sha224=d,p.hmac=c(),d.hmac=c(!0);export{p as default,d as sha224,p as sha256}; +var t="undefined"!=typeof ArrayBuffer,h=function(h){if("string"===typeof h)return[h,!0];if(Array.isArray(h))return[h,!1];if(t&&h){if(h.constructor===ArrayBuffer)return[new Uint8Array(h),!1];if(ArrayBuffer.isView(h))return[h,!1]}throw new Error("input is invalid type")},i="0123456789abcdef".split(""),s=[-2147483648,8388608,32768,128],r=[24,16,8,0],e=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],n=["hex","array","digest","arrayBuffer"],a=[],o=function(t,h){return function(i){return new y(h,!0).update(i)[t]()}},f=function(t){var h=o("hex",t);h.create=function(){return new y(t)},h.update=function(t){return h.create().update(t)};for(var i=0;i>>6,o[u++]=128|63&n):n<55296||n>=57344?(o[u++]=224|n>>>12,o[u++]=128|n>>>6&63,o[u++]=128|63&n):n<56320&&(a=t.charCodeAt(r+1))>=56320&&a<57344?(++r,n=65536+((1023&n)<<10|1023&a),o[u++]=240|n>>>18,o[u++]=128|n>>>12&63,o[u++]=128|n>>>6&63,o[u++]=128|63&n):(o[u++]=239,o[u++]=191,o[u++]=189);t=o}t.length>64&&(t=new y(i,!0).update(t).array());var c=[],l=[];for(r=0;r<64;++r){var p=t[r]||0;c[r]=92^p,l[r]=54^p}y.call(this,i,s),this.update(l),this.oKeyPad=c,this.inner=!0,this.sharedMemory=s}y.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var i=h(t);t=i[0];for(var s,e,n,a=i[1],o=0,f=t.length,u=this.blocks;o>>2]|=s<>>2]|=(192|s>>>6)<>>2]|=(128|63&s)<=57344?(u[n>>>2]|=(224|s>>>12)<>>2]|=(128|s>>>6&63)<>>2]|=(128|63&s)<=56320&&e<57344?(++o,s=65536+((1023&s)<<10|1023&e),u[n>>>2]|=(240|s>>>18)<>>2]|=(128|s>>>12&63)<>>2]|=(128|s>>>6&63)<>>2]|=(128|63&s)<>>2]|=239<>>2]|=191<>>2]|=189<>>2]|=t[o]<=64?(this.block=u[16],this.start=n-64,this.hash(),this.hashed=!0):this.start=n}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296|0,this.bytes=this.bytes%4294967296),this},y.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,h=this.lastByteIndex;t[16]=this.block,t[h>>>2]|=s[3&h],this.block=t[16],h>=56&&(this.hashed||this.hash(),t[0]=this.block,t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.hBytes<<3|this.bytes>>>29,t[15]=this.bytes<<3,this.hash()}},y.prototype.hash=function(){var t,h,i,s,r,n,a,o,f,u=this.h0,c=this.h1,y=this.h2,l=this.h3,p=this.h4,d=this.h5,b=this.h6,v=this.h7,w=this.blocks;for(t=16;t<64;++t)h=((r=w[t-15])>>>7|r<<25)^(r>>>18|r<<14)^r>>>3,i=((r=w[t-2])>>>17|r<<15)^(r>>>19|r<<13)^r>>>10,w[t]=w[t-16]+h+w[t-7]+i|0;for(f=c&y,t=0;t<64;t+=4)this.first?(this.is224?(n=300032,v=(r=w[0]-1413257819)-150054599|0,l=r+24177077|0):(n=704751109,v=(r=w[0]-210244248)-1521486534|0,l=r+143694565|0),this.first=!1):(h=(u>>>2|u<<30)^(u>>>13|u<<19)^(u>>>22|u<<10),s=(n=u&c)^u&y^f,v=l+(r=v+(i=(p>>>6|p<<26)^(p>>>11|p<<21)^(p>>>25|p<<7))+(p&d^~p&b)+e[t]+w[t])|0,l=r+(h+s)|0),h=(l>>>2|l<<30)^(l>>>13|l<<19)^(l>>>22|l<<10),s=(a=l&u)^l&c^n,b=y+(r=b+(i=(v>>>6|v<<26)^(v>>>11|v<<21)^(v>>>25|v<<7))+(v&p^~v&d)+e[t+1]+w[t+1])|0,h=((y=r+(h+s)|0)>>>2|y<<30)^(y>>>13|y<<19)^(y>>>22|y<<10),s=(o=y&l)^y&u^a,d=c+(r=d+(i=(b>>>6|b<<26)^(b>>>11|b<<21)^(b>>>25|b<<7))+(b&v^~b&p)+e[t+2]+w[t+2])|0,h=((c=r+(h+s)|0)>>>2|c<<30)^(c>>>13|c<<19)^(c>>>22|c<<10),s=(f=c&y)^c&l^o,p=u+(r=p+(i=(d>>>6|d<<26)^(d>>>11|d<<21)^(d>>>25|d<<7))+(d&b^~d&v)+e[t+3]+w[t+3])|0,u=r+(h+s)|0,this.chromeBugWorkAround=!0;this.h0=this.h0+u|0,this.h1=this.h1+c|0,this.h2=this.h2+y|0,this.h3=this.h3+l|0,this.h4=this.h4+p|0,this.h5=this.h5+d|0,this.h6=this.h6+b|0,this.h7=this.h7+v|0},y.prototype.hex=function(){this.finalize();var t=this.h0,h=this.h1,s=this.h2,r=this.h3,e=this.h4,n=this.h5,a=this.h6,o=this.h7,f=i[t>>>28&15]+i[t>>>24&15]+i[t>>>20&15]+i[t>>>16&15]+i[t>>>12&15]+i[t>>>8&15]+i[t>>>4&15]+i[15&t]+i[h>>>28&15]+i[h>>>24&15]+i[h>>>20&15]+i[h>>>16&15]+i[h>>>12&15]+i[h>>>8&15]+i[h>>>4&15]+i[15&h]+i[s>>>28&15]+i[s>>>24&15]+i[s>>>20&15]+i[s>>>16&15]+i[s>>>12&15]+i[s>>>8&15]+i[s>>>4&15]+i[15&s]+i[r>>>28&15]+i[r>>>24&15]+i[r>>>20&15]+i[r>>>16&15]+i[r>>>12&15]+i[r>>>8&15]+i[r>>>4&15]+i[15&r]+i[e>>>28&15]+i[e>>>24&15]+i[e>>>20&15]+i[e>>>16&15]+i[e>>>12&15]+i[e>>>8&15]+i[e>>>4&15]+i[15&e]+i[n>>>28&15]+i[n>>>24&15]+i[n>>>20&15]+i[n>>>16&15]+i[n>>>12&15]+i[n>>>8&15]+i[n>>>4&15]+i[15&n]+i[a>>>28&15]+i[a>>>24&15]+i[a>>>20&15]+i[a>>>16&15]+i[a>>>12&15]+i[a>>>8&15]+i[a>>>4&15]+i[15&a];return this.is224||(f+=i[o>>>28&15]+i[o>>>24&15]+i[o>>>20&15]+i[o>>>16&15]+i[o>>>12&15]+i[o>>>8&15]+i[o>>>4&15]+i[15&o]),f},y.prototype.toString=y.prototype.hex,y.prototype.digest=function(){this.finalize();var t=this.h0,h=this.h1,i=this.h2,s=this.h3,r=this.h4,e=this.h5,n=this.h6,a=this.h7,o=[t>>>24&255,t>>>16&255,t>>>8&255,255&t,h>>>24&255,h>>>16&255,h>>>8&255,255&h,i>>>24&255,i>>>16&255,i>>>8&255,255&i,s>>>24&255,s>>>16&255,s>>>8&255,255&s,r>>>24&255,r>>>16&255,r>>>8&255,255&r,e>>>24&255,e>>>16&255,e>>>8&255,255&e,n>>>24&255,n>>>16&255,n>>>8&255,255&n];return this.is224||o.push(a>>>24&255,a>>>16&255,a>>>8&255,255&a),o},y.prototype.array=y.prototype.digest,y.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(this.is224?28:32),h=new DataView(t);return h.setUint32(0,this.h0),h.setUint32(4,this.h1),h.setUint32(8,this.h2),h.setUint32(12,this.h3),h.setUint32(16,this.h4),h.setUint32(20,this.h5),h.setUint32(24,this.h6),this.is224||h.setUint32(28,this.h7),t},l.prototype=new y,l.prototype.finalize=function(){if(y.prototype.finalize.call(this),this.inner){this.inner=!1;var t=this.array();y.call(this,this.is224,this.sharedMemory),this.update(this.oKeyPad),this.update(t),y.prototype.finalize.call(this)}};var p=f(),d=f(!0);p.sha256=p,p.sha224=d,p.hmac=c(),d.hmac=c(!0);export{p as default,d as sha224,p as sha256}; diff --git a/build/sha256.node.cjs b/build/sha256.node.cjs index 1fa7fb2..5936401 100644 --- a/build/sha256.node.cjs +++ b/build/sha256.node.cjs @@ -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 diff --git a/package-lock.json b/package-lock.json index c500ac4..57772b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 1723932..7c7d796 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/core.mjs b/src/core.mjs index c5db7dc..66e4430 100644 --- a/src/core.mjs +++ b/src/core.mjs @@ -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; diff --git a/tests/hmac-test.js b/tests/hmac-test.js index fd053fd..1a89281 100644 --- a/tests/hmac-test.js +++ b/tests/hmac-test.js @@ -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'] } } }; diff --git a/tests/test.js b/tests/test.js index 77360ca..ce36018 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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 () {