diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e7c97f..2271867 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## v0.15.0 / 2026-11-16 +### Fixed +- Unpaired UTF-16 surrogates in string input produced wrong digests and consumed the following code unit. They are now encoded as U+FFFD, matching `TextEncoder` and Node.js `crypto` (also applies to cSHAKE/KMAC function names, customization strings, and keys, TupleHash, and ParallelHash) + ## v0.14.0 / 2026-11-08 ### Fixed - Wrong digest for strings whose last UTF-8 character crosses a block boundary #62 diff --git a/bower.json b/bower.json index a43b374..5d04291 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-sha3", - "version": "0.14.0", + "version": "0.15.0", "main": ["src/sha3.js"], "ignore": [ "samples", diff --git a/build/sha3.min.js b/build/sha3.min.js index 6a018be..6b8fab9 100644 --- a/build/sha3.min.js +++ b/build/sha3.min.js @@ -1,9 +1,9 @@ /** * [js-sha3]{@link https://github.com/emn178/js-sha3} * - * @version 0.14.0 + * @version 0.15.0 * @author Chen, Yi-Cyuan [emn178@gmail.com] * @copyright Chen, Yi-Cyuan 2015-2026 * @license MIT */ -!function(){"use strict";function t(t,e,n){this.blocks=[],this.s=[],this.padding=e,this.outputBits=n,this.reset=!0,this.finalized=!1,this.block=0,this.start=0,this.blockCount=1600-(t<<1)>>5,this.byteCount=this.blockCount<<2,this.outputBlocks=n>>5,this.extraBytes=(31&n)>>3;for(var r=0;r<50;++r)this.s[r]=0}function e(e,n,r,i){t.call(this,e,n,r),this.xof=i}function n(t,n,r,i){e.call(this,t,n,r,i),this.inputActive=!1,this.inputBytesRemaining=0}function r(t,n,r,i,o){e.call(this,t,n,r,i),this.bits=t,this.blockSize=o,this.inner=null,this.innerBytes=0,this.blockNumber=0}var i="input is invalid type",o="finalize already called",a="tuple input is incomplete",u="object"==typeof window,s=u?window:{};s.JS_SHA3_NO_WINDOW&&(u=!1);var h=!u&&"object"==typeof self;!s.JS_SHA3_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node?s=global:h&&(s=self);for(var f=!s.JS_SHA3_NO_COMMON_JS&&"object"==typeof module&&module.exports,c="function"==typeof define&&define.amd,p=!s.JS_SHA3_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,l="0123456789abcdef".split(""),d=[31,7936,2031616,520093696],y=[4,1024,262144,67108864],b=[0,8,16,24],v=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770,2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648,32896,2147483648,2147483649,0,2147516424,2147483648],g=[224,256,384,512],w=[128,256],B=["hex","buffer","arrayBuffer","array","digest"],A={128:168,256:136},k=s.JS_SHA3_NO_NODE_JS||!Array.isArray?function(t){return"[object Array]"===Object.prototype.toString.call(t)}:Array.isArray,_=!p||!s.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW&&ArrayBuffer.isView?ArrayBuffer.isView:function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer},m=function(t){var e=typeof t;if("string"===e)return[t,!0];if("object"!==e||null===t)throw new Error(i);if(p&&t.constructor===ArrayBuffer)return[new Uint8Array(t),!1];if(!k(t)&&!_(t))throw new Error(i);return[t,!1]},x=function(t){return 0===m(t)[0].length},C=function(t){for(var e=[],n=0;n268435455)throw new Error("block size is invalid");var s=new r(t,e,a,n,o).bytepad(["ParallelHash",u],i);return s.encode(o,!1),s},o.update=function(t,e,n,r){return o.create(e,n,r).update(t)},I(o,function(t,e,r){return O(t,0,n,r)},t,e)},F=[{name:"keccak",padding:[1,256,65536,16777216],bits:g,createMethod:R},{name:"sha3",padding:[6,1536,393216,100663296],bits:g,createMethod:R},{name:"shake",padding:d,bits:w,createMethod:function(e,n){var r=z(e,n,"hex");return r.create=function(r){return new t(e,n,r)},r.update=function(t,e){return r.create(e).update(t)},I(r,z,e,n)}},{name:"cshake",padding:y,bits:w,createMethod:function(e,n){var r=A[e],i=M(e,0,"hex");return i.create=function(i,o,a){return x(o)&&x(a)?U["shake"+e].create(i):new t(e,n,i).bytepad([o,a],r)},i.update=function(t,e,n,r){return i.create(e,n,r).update(t)},I(i,M,e,n)}},{name:"kmac",padding:y,bits:w,createMethod:function(t,e){return J(t,e,!1)}},{name:"kmacxof",padding:y,bits:w,createMethod:function(t,e){return J(t,e,!0)}},{name:"tuplehash",padding:y,bits:w,createMethod:function(t,e){return j(t,e,!1)}},{name:"tuplehashxof",padding:y,bits:w,createMethod:function(t,e){return j(t,e,!0)}},{name:"parallelhash",padding:y,bits:w,createMethod:function(t,e){return H(t,e,!1)}},{name:"parallelhashxof",padding:y,bits:w,createMethod:function(t,e){return H(t,e,!0)}}],U={},D=[],L=0;L>2]|=r<>2]|=(192|r>>6)<>2]|=(128|63&r)<=57344?(a[n>>2]|=(224|r>>12)<>2]|=(128|r>>6&63)<>2]|=(128|63&r)<>2]|=(240|r>>18)<>2]|=(128|r>>12&63)<>2]|=(128|r>>6&63)<>2]|=(128|63&r)<>2]|=t[f]<=u){for(this.start=n-u,this.block=a[h],n=0;n>=8);r>0;)o.unshift(r),r=255&(e>>=8),++i;return n?o.push(i):o.unshift(i),t.prototype.update.call(this,o),o.length},t.prototype.encodeString=function(e){var n=m(e);e=n[0];var r=n[1],i=0,o=e.length;if(r)for(var a=0;a=57344?i+=3:(u=65536+((1023&u)<<10|1023&e.charCodeAt(++a)),i+=4)}else i=o;return i+=this.encode(8*i),t.prototype.update.call(this,e),i},t.prototype.bytepad=function(e,n){for(var r=this.encode(n),i=0;i>2]|=this.padding[3&e],this.lastByteIndex>=this.byteCount)for(t[0]=t[n],e=1;e>4&15]+l[15&t]+l[t>>12&15]+l[t>>8&15]+l[t>>20&15]+l[t>>16&15]+l[t>>28&15]+l[t>>24&15];a%e==0&&(n=C(n),T(n),o=0)}return i&&(t=n[o],u+=l[t>>4&15]+l[15&t],i>1&&(u+=l[t>>12&15]+l[t>>8&15]),i>2&&(u+=l[t>>20&15]+l[t>>16&15])),u},t.prototype.arrayBuffer=function(){this.finalize();var t,e=this.blockCount,n=this.s,r=this.outputBlocks,i=this.extraBytes,o=0,a=0,u=this.outputBits>>3;t=i?new ArrayBuffer(r+1<<2):new ArrayBuffer(u);for(var s=new Uint32Array(t);a>8&255,s[t+2]=e>>16&255,s[t+3]=e>>24&255;u%n==0&&(r=C(r),T(r))}return o&&(t=u<<2,e=r[a],s[t]=255&e,o>1&&(s[t+1]=e>>8&255),o>2&&(s[t+2]=e>>16&255)),s},(e.prototype=new t).finalize=function(){return this.finalized||this.encode(this.xof?0:this.outputBits,!0),t.prototype.finalize.call(this)},(n.prototype=new e).getMessageByteLength=function(t){var e=m(t);if(t=e[0],!e[1])return t.length;for(var n=0,r=0;r=57344?n+=3:(++r,n+=4)}return n},n.prototype.beginInput=function(t){if(this.inputActive)throw new Error(a);if("number"!=typeof t||!isFinite(t)||t<0||Math.floor(t)!==t||t>268435455)throw new Error("tuple input byte length is invalid");return this.encode(8*t,!1),0!==t&&(this.inputActive=!0,this.inputBytesRemaining=t),this},n.prototype._updateChunk=function(t,n){return e.prototype.update.call(this,t),this.inputBytesRemaining-=n,0===this.inputBytesRemaining&&(this.inputActive=!1),this},n.prototype.updateChunk=function(t){if(!this.inputActive)throw new Error("no active tuple input");var e=this.getMessageByteLength(t);if(e>this.inputBytesRemaining)throw new Error("tuple input exceeds declared length");return this._updateChunk(t,e)},n.prototype.update=function(t){var e=this.getMessageByteLength(t);return this.beginInput(e),this._updateChunk(t,e)},n.prototype.finalize=function(){if(this.inputActive)throw new Error(a);return e.prototype.finalize.call(this)},(r.prototype=new e)._finishInner=function(){e.prototype.update.call(this,this.inner.array()),this.inner=null,this.innerBytes=0,++this.blockNumber},r.prototype._updateBytes=function(e){var n=e.length;if(n)for(var r=e.subarray||e.slice,i=this.blockSize,o=0;o>>6,i[u++]=128|63&n):n<55296||n>=57344?(i[u++]=224|n>>>12,i[u++]=128|n>>>6&63,i[u++]=128|63&n):(n=65536+((1023&n)<<10|1023&t.charCodeAt(++r)),i[u++]=240|n>>>18,i[u++]=128|n>>>12&63,i[u++]=128|n>>>6&63,i[u++]=128|63&n);t=i}return this._updateBytes(t),this},r.prototype.finalize=function(){return this.finalized||(this.inner&&this._finishInner(),this.encode(this.blockNumber,!0)),e.prototype.finalize.call(this)};var T=function(t){var e,n,r,i,o,a,u,s,h,f,c,p,l,d,y,b,g,w,B,A,k,_,m,x,C,S,z,M,E,N,O,I,R,J,j,H,F,U,D,L,V,W,Y,K,P,T,q,G,Q,X,Z,$,tt,et,nt,rt,it,ot,at,ut,st,ht,ft;for(r=0;r<48;r+=2)i=t[0]^t[10]^t[20]^t[30]^t[40],o=t[1]^t[11]^t[21]^t[31]^t[41],a=t[2]^t[12]^t[22]^t[32]^t[42],u=t[3]^t[13]^t[23]^t[33]^t[43],s=t[4]^t[14]^t[24]^t[34]^t[44],h=t[5]^t[15]^t[25]^t[35]^t[45],f=t[6]^t[16]^t[26]^t[36]^t[46],c=t[7]^t[17]^t[27]^t[37]^t[47],e=(p=t[8]^t[18]^t[28]^t[38]^t[48])^(a<<1|u>>>31),n=(l=t[9]^t[19]^t[29]^t[39]^t[49])^(u<<1|a>>>31),t[0]^=e,t[1]^=n,t[10]^=e,t[11]^=n,t[20]^=e,t[21]^=n,t[30]^=e,t[31]^=n,t[40]^=e,t[41]^=n,e=i^(s<<1|h>>>31),n=o^(h<<1|s>>>31),t[2]^=e,t[3]^=n,t[12]^=e,t[13]^=n,t[22]^=e,t[23]^=n,t[32]^=e,t[33]^=n,t[42]^=e,t[43]^=n,e=a^(f<<1|c>>>31),n=u^(c<<1|f>>>31),t[4]^=e,t[5]^=n,t[14]^=e,t[15]^=n,t[24]^=e,t[25]^=n,t[34]^=e,t[35]^=n,t[44]^=e,t[45]^=n,e=s^(p<<1|l>>>31),n=h^(l<<1|p>>>31),t[6]^=e,t[7]^=n,t[16]^=e,t[17]^=n,t[26]^=e,t[27]^=n,t[36]^=e,t[37]^=n,t[46]^=e,t[47]^=n,e=f^(i<<1|o>>>31),n=c^(o<<1|i>>>31),t[8]^=e,t[9]^=n,t[18]^=e,t[19]^=n,t[28]^=e,t[29]^=n,t[38]^=e,t[39]^=n,t[48]^=e,t[49]^=n,d=t[0],y=t[1],T=t[11]<<4|t[10]>>>28,q=t[10]<<4|t[11]>>>28,M=t[20]<<3|t[21]>>>29,E=t[21]<<3|t[20]>>>29,ut=t[31]<<9|t[30]>>>23,st=t[30]<<9|t[31]>>>23,W=t[40]<<18|t[41]>>>14,Y=t[41]<<18|t[40]>>>14,J=t[2]<<1|t[3]>>>31,j=t[3]<<1|t[2]>>>31,b=t[13]<<12|t[12]>>>20,g=t[12]<<12|t[13]>>>20,G=t[22]<<10|t[23]>>>22,Q=t[23]<<10|t[22]>>>22,N=t[33]<<13|t[32]>>>19,O=t[32]<<13|t[33]>>>19,ht=t[42]<<2|t[43]>>>30,ft=t[43]<<2|t[42]>>>30,et=t[5]<<30|t[4]>>>2,nt=t[4]<<30|t[5]>>>2,H=t[14]<<6|t[15]>>>26,F=t[15]<<6|t[14]>>>26,w=t[25]<<11|t[24]>>>21,B=t[24]<<11|t[25]>>>21,X=t[34]<<15|t[35]>>>17,Z=t[35]<<15|t[34]>>>17,I=t[45]<<29|t[44]>>>3,R=t[44]<<29|t[45]>>>3,x=t[6]<<28|t[7]>>>4,C=t[7]<<28|t[6]>>>4,rt=t[17]<<23|t[16]>>>9,it=t[16]<<23|t[17]>>>9,U=t[26]<<25|t[27]>>>7,D=t[27]<<25|t[26]>>>7,A=t[36]<<21|t[37]>>>11,k=t[37]<<21|t[36]>>>11,$=t[47]<<24|t[46]>>>8,tt=t[46]<<24|t[47]>>>8,K=t[8]<<27|t[9]>>>5,P=t[9]<<27|t[8]>>>5,S=t[18]<<20|t[19]>>>12,z=t[19]<<20|t[18]>>>12,ot=t[29]<<7|t[28]>>>25,at=t[28]<<7|t[29]>>>25,L=t[38]<<8|t[39]>>>24,V=t[39]<<8|t[38]>>>24,_=t[48]<<14|t[49]>>>18,m=t[49]<<14|t[48]>>>18,t[0]=d^~b&w,t[1]=y^~g&B,t[10]=x^~S&M,t[11]=C^~z&E,t[20]=J^~H&U,t[21]=j^~F&D,t[30]=K^~T&G,t[31]=P^~q&Q,t[40]=et^~rt&ot,t[41]=nt^~it&at,t[2]=b^~w&A,t[3]=g^~B&k,t[12]=S^~M&N,t[13]=z^~E&O,t[22]=H^~U&L,t[23]=F^~D&V,t[32]=T^~G&X,t[33]=q^~Q&Z,t[42]=rt^~ot&ut,t[43]=it^~at&st,t[4]=w^~A&_,t[5]=B^~k&m,t[14]=M^~N&I,t[15]=E^~O&R,t[24]=U^~L&W,t[25]=D^~V&Y,t[34]=G^~X&$,t[35]=Q^~Z&tt,t[44]=ot^~ut&ht,t[45]=at^~st&ft,t[6]=A^~_&d,t[7]=k^~m&y,t[16]=N^~I&x,t[17]=O^~R&C,t[26]=L^~W&J,t[27]=V^~Y&j,t[36]=X^~$&K,t[37]=Z^~tt&P,t[46]=ut^~ht&et,t[47]=st^~ft&nt,t[8]=_^~d&b,t[9]=m^~y&g,t[18]=I^~x&S,t[19]=R^~C&z,t[28]=W^~J&H,t[29]=Y^~j&F,t[38]=$^~K&T,t[39]=tt^~P&q,t[48]=ht^~et&rt,t[49]=ft^~nt&it,t[0]^=v[r],t[1]^=v[r+1]};if(f)module.exports=U;else{for(L=0;L>5,this.byteCount=this.blockCount<<2,this.outputBlocks=r>>5,this.extraBytes=(31&r)>>3;for(var n=0;n<50;++n)this.s[n]=0}function e(e,r,n,i){t.call(this,e,r,n),this.xof=i}function r(t,r,n,i){e.call(this,t,r,n,i),this.inputActive=!1,this.inputBytesRemaining=0}function n(t,r,n,i,o){e.call(this,t,r,n,i),this.bits=t,this.blockSize=o,this.inner=null,this.innerBytes=0,this.blockNumber=0}var i="input is invalid type",o="finalize already called",a="tuple input is incomplete",u="object"==typeof window,s=u?window:{};s.JS_SHA3_NO_WINDOW&&(u=!1);var h=!u&&"object"==typeof self;!s.JS_SHA3_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node?s=global:h&&(s=self);for(var f=!s.JS_SHA3_NO_COMMON_JS&&"object"==typeof module&&module.exports,c="function"==typeof define&&define.amd,p=!s.JS_SHA3_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,l="0123456789abcdef".split(""),d=[31,7936,2031616,520093696],y=[4,1024,262144,67108864],b=[0,8,16,24],v=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770,2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648,32896,2147483648,2147483649,0,2147516424,2147483648],g=[224,256,384,512],w=[128,256],B=["hex","buffer","arrayBuffer","array","digest"],A={128:168,256:136},k=s.JS_SHA3_NO_NODE_JS||!Array.isArray?function(t){return"[object Array]"===Object.prototype.toString.call(t)}:Array.isArray,_=!p||!s.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW&&ArrayBuffer.isView?ArrayBuffer.isView:function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer},m=function(t){var e=typeof t;if("string"===e)return[t,!0];if("object"!==e||null===t)throw new Error(i);if(p&&t.constructor===ArrayBuffer)return[new Uint8Array(t),!1];if(!k(t)&&!_(t))throw new Error(i);return[t,!1]},x=function(t){return 0===m(t)[0].length},C=function(t){for(var e=[],r=0;r268435455)throw new Error("block size is invalid");var s=new n(t,e,a,r,o).bytepad(["ParallelHash",u],i);return s.encode(o,!1),s},o.update=function(t,e,r,n){return o.create(e,r,n).update(t)},I(o,function(t,e,n){return O(t,0,r,n)},t,e)},F=[{name:"keccak",padding:[1,256,65536,16777216],bits:g,createMethod:R},{name:"sha3",padding:[6,1536,393216,100663296],bits:g,createMethod:R},{name:"shake",padding:d,bits:w,createMethod:function(e,r){var n=z(e,r,"hex");return n.create=function(n){return new t(e,r,n)},n.update=function(t,e){return n.create(e).update(t)},I(n,z,e,r)}},{name:"cshake",padding:y,bits:w,createMethod:function(e,r){var n=A[e],i=M(e,0,"hex");return i.create=function(i,o,a){return x(o)&&x(a)?U["shake"+e].create(i):new t(e,r,i).bytepad([o,a],n)},i.update=function(t,e,r,n){return i.create(e,r,n).update(t)},I(i,M,e,r)}},{name:"kmac",padding:y,bits:w,createMethod:function(t,e){return J(t,e,!1)}},{name:"kmacxof",padding:y,bits:w,createMethod:function(t,e){return J(t,e,!0)}},{name:"tuplehash",padding:y,bits:w,createMethod:function(t,e){return j(t,e,!1)}},{name:"tuplehashxof",padding:y,bits:w,createMethod:function(t,e){return j(t,e,!0)}},{name:"parallelhash",padding:y,bits:w,createMethod:function(t,e){return H(t,e,!1)}},{name:"parallelhashxof",padding:y,bits:w,createMethod:function(t,e){return H(t,e,!0)}}],U={},D=[],L=0;L>2]|=n<>2]|=(192|n>>6)<>2]|=(128|63&n)<=57344?(u[r>>2]|=(224|n>>12)<>2]|=(128|n>>6&63)<>2]|=(128|63&n)<=56320&&i<57344?(++c,n=65536+((1023&n)<<10|1023&i),u[r>>2]|=(240|n>>18)<>2]|=(128|n>>12&63)<>2]|=(128|n>>6&63)<>2]|=(128|63&n)<>2]|=239<>2]|=191<>2]|=189<>2]|=t[c]<=s){for(this.start=r-s,this.block=u[f],r=0;r>=8);n>0;)o.unshift(n),n=255&(e>>=8),++i;return r?o.push(i):o.unshift(i),t.prototype.update.call(this,o),o.length},t.prototype.encodeString=function(e){var r=m(e);e=r[0];var n=r[1],i=0,o=e.length;if(n)for(var a=0;a=57344?i+=3:u<56320&&(u=e.charCodeAt(a+1))>=56320&&u<57344?(++a,i+=4):i+=3}else i=o;return i+=this.encode(8*i),t.prototype.update.call(this,e),i},t.prototype.bytepad=function(e,r){for(var n=this.encode(r),i=0;i>2]|=this.padding[3&e],this.lastByteIndex>=this.byteCount)for(t[0]=t[r],e=1;e>4&15]+l[15&t]+l[t>>12&15]+l[t>>8&15]+l[t>>20&15]+l[t>>16&15]+l[t>>28&15]+l[t>>24&15];a%e==0&&(r=C(r),T(r),o=0)}return i&&(t=r[o],u+=l[t>>4&15]+l[15&t],i>1&&(u+=l[t>>12&15]+l[t>>8&15]),i>2&&(u+=l[t>>20&15]+l[t>>16&15])),u},t.prototype.arrayBuffer=function(){this.finalize();var t,e=this.blockCount,r=this.s,n=this.outputBlocks,i=this.extraBytes,o=0,a=0,u=this.outputBits>>3;t=i?new ArrayBuffer(n+1<<2):new ArrayBuffer(u);for(var s=new Uint32Array(t);a>8&255,s[t+2]=e>>16&255,s[t+3]=e>>24&255;u%r==0&&(n=C(n),T(n))}return o&&(t=u<<2,e=n[a],s[t]=255&e,o>1&&(s[t+1]=e>>8&255),o>2&&(s[t+2]=e>>16&255)),s},(e.prototype=new t).finalize=function(){return this.finalized||this.encode(this.xof?0:this.outputBits,!0),t.prototype.finalize.call(this)},(r.prototype=new e).getMessageByteLength=function(t){var e=m(t);if(t=e[0],!e[1])return t.length;for(var r=0,n=0;n=57344?r+=3:i<56320&&(i=t.charCodeAt(n+1))>=56320&&i<57344?(++n,r+=4):r+=3}return r},r.prototype.beginInput=function(t){if(this.inputActive)throw new Error(a);if("number"!=typeof t||!isFinite(t)||t<0||Math.floor(t)!==t||t>268435455)throw new Error("tuple input byte length is invalid");return this.encode(8*t,!1),0!==t&&(this.inputActive=!0,this.inputBytesRemaining=t),this},r.prototype._updateChunk=function(t,r){return e.prototype.update.call(this,t),this.inputBytesRemaining-=r,0===this.inputBytesRemaining&&(this.inputActive=!1),this},r.prototype.updateChunk=function(t){if(!this.inputActive)throw new Error("no active tuple input");var e=this.getMessageByteLength(t);if(e>this.inputBytesRemaining)throw new Error("tuple input exceeds declared length");return this._updateChunk(t,e)},r.prototype.update=function(t){var e=this.getMessageByteLength(t);return this.beginInput(e),this._updateChunk(t,e)},r.prototype.finalize=function(){if(this.inputActive)throw new Error(a);return e.prototype.finalize.call(this)},(n.prototype=new e)._finishInner=function(){e.prototype.update.call(this,this.inner.array()),this.inner=null,this.innerBytes=0,++this.blockNumber},n.prototype._updateBytes=function(e){var r=e.length;if(r)for(var n=e.subarray||e.slice,i=this.blockSize,o=0;o>>6,a[s++]=128|63&r):r<55296||r>=57344?(a[s++]=224|r>>>12,a[s++]=128|r>>>6&63,a[s++]=128|63&r):r<56320&&(n=t.charCodeAt(i+1))>=56320&&n<57344?(++i,r=65536+((1023&r)<<10|1023&n),a[s++]=240|r>>>18,a[s++]=128|r>>>12&63,a[s++]=128|r>>>6&63,a[s++]=128|63&r):(a[s++]=239,a[s++]=191,a[s++]=189);t=a}return this._updateBytes(t),this},n.prototype.finalize=function(){return this.finalized||(this.inner&&this._finishInner(),this.encode(this.blockNumber,!0)),e.prototype.finalize.call(this)};var T=function(t){var e,r,n,i,o,a,u,s,h,f,c,p,l,d,y,b,g,w,B,A,k,_,m,x,C,S,z,M,E,N,O,I,R,J,j,H,F,U,D,L,V,W,Y,K,P,T,q,G,Q,X,Z,$,tt,et,rt,nt,it,ot,at,ut,st,ht,ft;for(n=0;n<48;n+=2)i=t[0]^t[10]^t[20]^t[30]^t[40],o=t[1]^t[11]^t[21]^t[31]^t[41],a=t[2]^t[12]^t[22]^t[32]^t[42],u=t[3]^t[13]^t[23]^t[33]^t[43],s=t[4]^t[14]^t[24]^t[34]^t[44],h=t[5]^t[15]^t[25]^t[35]^t[45],f=t[6]^t[16]^t[26]^t[36]^t[46],c=t[7]^t[17]^t[27]^t[37]^t[47],e=(p=t[8]^t[18]^t[28]^t[38]^t[48])^(a<<1|u>>>31),r=(l=t[9]^t[19]^t[29]^t[39]^t[49])^(u<<1|a>>>31),t[0]^=e,t[1]^=r,t[10]^=e,t[11]^=r,t[20]^=e,t[21]^=r,t[30]^=e,t[31]^=r,t[40]^=e,t[41]^=r,e=i^(s<<1|h>>>31),r=o^(h<<1|s>>>31),t[2]^=e,t[3]^=r,t[12]^=e,t[13]^=r,t[22]^=e,t[23]^=r,t[32]^=e,t[33]^=r,t[42]^=e,t[43]^=r,e=a^(f<<1|c>>>31),r=u^(c<<1|f>>>31),t[4]^=e,t[5]^=r,t[14]^=e,t[15]^=r,t[24]^=e,t[25]^=r,t[34]^=e,t[35]^=r,t[44]^=e,t[45]^=r,e=s^(p<<1|l>>>31),r=h^(l<<1|p>>>31),t[6]^=e,t[7]^=r,t[16]^=e,t[17]^=r,t[26]^=e,t[27]^=r,t[36]^=e,t[37]^=r,t[46]^=e,t[47]^=r,e=f^(i<<1|o>>>31),r=c^(o<<1|i>>>31),t[8]^=e,t[9]^=r,t[18]^=e,t[19]^=r,t[28]^=e,t[29]^=r,t[38]^=e,t[39]^=r,t[48]^=e,t[49]^=r,d=t[0],y=t[1],T=t[11]<<4|t[10]>>>28,q=t[10]<<4|t[11]>>>28,M=t[20]<<3|t[21]>>>29,E=t[21]<<3|t[20]>>>29,ut=t[31]<<9|t[30]>>>23,st=t[30]<<9|t[31]>>>23,W=t[40]<<18|t[41]>>>14,Y=t[41]<<18|t[40]>>>14,J=t[2]<<1|t[3]>>>31,j=t[3]<<1|t[2]>>>31,b=t[13]<<12|t[12]>>>20,g=t[12]<<12|t[13]>>>20,G=t[22]<<10|t[23]>>>22,Q=t[23]<<10|t[22]>>>22,N=t[33]<<13|t[32]>>>19,O=t[32]<<13|t[33]>>>19,ht=t[42]<<2|t[43]>>>30,ft=t[43]<<2|t[42]>>>30,et=t[5]<<30|t[4]>>>2,rt=t[4]<<30|t[5]>>>2,H=t[14]<<6|t[15]>>>26,F=t[15]<<6|t[14]>>>26,w=t[25]<<11|t[24]>>>21,B=t[24]<<11|t[25]>>>21,X=t[34]<<15|t[35]>>>17,Z=t[35]<<15|t[34]>>>17,I=t[45]<<29|t[44]>>>3,R=t[44]<<29|t[45]>>>3,x=t[6]<<28|t[7]>>>4,C=t[7]<<28|t[6]>>>4,nt=t[17]<<23|t[16]>>>9,it=t[16]<<23|t[17]>>>9,U=t[26]<<25|t[27]>>>7,D=t[27]<<25|t[26]>>>7,A=t[36]<<21|t[37]>>>11,k=t[37]<<21|t[36]>>>11,$=t[47]<<24|t[46]>>>8,tt=t[46]<<24|t[47]>>>8,K=t[8]<<27|t[9]>>>5,P=t[9]<<27|t[8]>>>5,S=t[18]<<20|t[19]>>>12,z=t[19]<<20|t[18]>>>12,ot=t[29]<<7|t[28]>>>25,at=t[28]<<7|t[29]>>>25,L=t[38]<<8|t[39]>>>24,V=t[39]<<8|t[38]>>>24,_=t[48]<<14|t[49]>>>18,m=t[49]<<14|t[48]>>>18,t[0]=d^~b&w,t[1]=y^~g&B,t[10]=x^~S&M,t[11]=C^~z&E,t[20]=J^~H&U,t[21]=j^~F&D,t[30]=K^~T&G,t[31]=P^~q&Q,t[40]=et^~nt&ot,t[41]=rt^~it&at,t[2]=b^~w&A,t[3]=g^~B&k,t[12]=S^~M&N,t[13]=z^~E&O,t[22]=H^~U&L,t[23]=F^~D&V,t[32]=T^~G&X,t[33]=q^~Q&Z,t[42]=nt^~ot&ut,t[43]=it^~at&st,t[4]=w^~A&_,t[5]=B^~k&m,t[14]=M^~N&I,t[15]=E^~O&R,t[24]=U^~L&W,t[25]=D^~V&Y,t[34]=G^~X&$,t[35]=Q^~Z&tt,t[44]=ot^~ut&ht,t[45]=at^~st&ft,t[6]=A^~_&d,t[7]=k^~m&y,t[16]=N^~I&x,t[17]=O^~R&C,t[26]=L^~W&J,t[27]=V^~Y&j,t[36]=X^~$&K,t[37]=Z^~tt&P,t[46]=ut^~ht&et,t[47]=st^~ft&rt,t[8]=_^~d&b,t[9]=m^~y&g,t[18]=I^~x&S,t[19]=R^~C&z,t[28]=W^~J&H,t[29]=Y^~j&F,t[38]=$^~K&T,t[39]=tt^~P&q,t[48]=ht^~et&nt,t[49]=ft^~rt&it,t[0]^=v[n],t[1]^=v[n+1]};if(f)module.exports=U;else{for(L=0;L268435455)throw new Error("block size is invalid");var s=new K(t,e,a,r,i).bytepad(["ParallelHash",o],n);return s.encode(i,!1),s},i.update=function(t,e,r,n){return i.create(e,r,n).update(t)},E(i,function(t,e,n){return M(t,0,r,n)},t,e)},J=[{name:"keccak",padding:[1,256,65536,16777216],bits:y,createMethod:O},{name:"sha3",padding:[6,1536,393216,100663296],bits:y,createMethod:O},{name:"shake",padding:c,bits:k,createMethod:function(t,e){var r=m(t,e,"hex");return r.create=function(r){return new W(t,e,r)},r.update=function(t,e){return r.create(e).update(t)},E(r,m,t,e)}},{name:"cshake",padding:p,bits:k,createMethod:function(t,e){var r=v[t],n=C(t,0,"hex");return n.create=function(n,i,a){return x(i)&&x(a)?j["shake"+t].create(n):new W(t,e,n).bytepad([i,a],r)},n.update=function(t,e,r,i){return n.create(e,r,i).update(t)},E(n,C,t,e)}},{name:"kmac",padding:p,bits:k,createMethod:function(t,e){return N(t,e,!1)}},{name:"kmacxof",padding:p,bits:k,createMethod:function(t,e){return N(t,e,!0)}},{name:"tuplehash",padding:p,bits:k,createMethod:function(t,e){return I(t,e,!1)}},{name:"tuplehashxof",padding:p,bits:k,createMethod:function(t,e){return I(t,e,!0)}},{name:"parallelhash",padding:p,bits:k,createMethod:function(t,e){return R(t,e,!1)}},{name:"parallelhashxof",padding:p,bits:k,createMethod:function(t,e){return R(t,e,!0)}}],j={},H=[],F=0;F>5,this.byteCount=this.blockCount<<2,this.outputBlocks=r>>5,this.extraBytes=(31&r)>>3;for(var n=0;n<50;++n)this.s[n]=0}function P(t,e,r,n){W.call(this,t,e,r),this.xof=n}function Y(t,e,r,n){P.call(this,t,e,r,n),this.inputActive=!1,this.inputBytesRemaining=0}function K(t,e,r,n,i){P.call(this,t,e,r,n),this.bits=t,this.blockSize=i,this.inner=null,this.innerBytes=0,this.blockNumber=0}W.prototype.update=function(t){if(this.finalized)throw new Error(n);var e=w(t);t=e[0];for(var r,i,a=e[1],o=this.blocks,s=this.byteCount,u=t.length,h=this.blockCount,f=0,c=this.s;f>2]|=i<>2]|=(192|i>>6)<>2]|=(128|63&i)<=57344?(o[r>>2]|=(224|i>>12)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<>2]|=(240|i>>18)<>2]|=(128|i>>12&63)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<>2]|=t[f]<=s){for(this.start=r-s,this.block=o[h],r=0;r>=8);r>0;)i.unshift(r),r=255&(t>>=8),++n;return e?i.push(n):i.unshift(n),W.prototype.update.call(this,i),i.length},W.prototype.encodeString=function(t){var e=w(t);t=e[0];var r=e[1],n=0,i=t.length;if(r)for(var a=0;a=57344?n+=3:(o=65536+((1023&o)<<10|1023&t.charCodeAt(++a)),n+=4)}else n=i;return n+=this.encode(8*n),W.prototype.update.call(this,t),n},W.prototype.bytepad=function(t,e){for(var r=this.encode(e),n=0;n>2]|=this.padding[3&e],this.lastByteIndex>=this.byteCount)for(t[0]=t[r],e=1;e>4&15]+f[15&t]+f[t>>12&15]+f[t>>8&15]+f[t>>20&15]+f[t>>16&15]+f[t>>28&15]+f[t>>24&15];o%e===0&&(r=B(r),q(r),a=0)}return i&&(t=r[a],s+=f[t>>4&15]+f[15&t],i>1&&(s+=f[t>>12&15]+f[t>>8&15]),i>2&&(s+=f[t>>20&15]+f[t>>16&15])),s},W.prototype.arrayBuffer=function(){this.finalize();var t,e=this.blockCount,r=this.s,n=this.outputBlocks,i=this.extraBytes,a=0,o=0,s=this.outputBits>>3;t=i?new ArrayBuffer(n+1<<2):new ArrayBuffer(s);for(var u=new Uint32Array(t);o>8&255,u[t+2]=e>>16&255,u[t+3]=e>>24&255;s%r===0&&(n=B(n),q(n))}return a&&(t=s<<2,e=n[o],u[t]=255&e,a>1&&(u[t+1]=e>>8&255),a>2&&(u[t+2]=e>>16&255)),u},P.prototype=new W,P.prototype.finalize=function(){return this.finalized||this.encode(this.xof?0:this.outputBits,!0),W.prototype.finalize.call(this)},Y.prototype=new P,Y.prototype.getMessageByteLength=function(t){var e=w(t);if(t=e[0],!e[1])return t.length;for(var r=0,n=0;n=57344?r+=3:(++n,r+=4)}return r},Y.prototype.beginInput=function(t){if(this.inputActive)throw new Error(i);if("number"!=typeof t||!isFinite(t)||t<0||Math.floor(t)!==t||t>268435455)throw new Error("tuple input byte length is invalid");return this.encode(8*t,!1),0!==t&&(this.inputActive=!0,this.inputBytesRemaining=t),this},Y.prototype._updateChunk=function(t,e){return P.prototype.update.call(this,t),this.inputBytesRemaining-=e,0===this.inputBytesRemaining&&(this.inputActive=!1),this},Y.prototype.updateChunk=function(t){if(!this.inputActive)throw new Error("no active tuple input");var e=this.getMessageByteLength(t);if(e>this.inputBytesRemaining)throw new Error("tuple input exceeds declared length");return this._updateChunk(t,e)},Y.prototype.update=function(t){var e=this.getMessageByteLength(t);return this.beginInput(e),this._updateChunk(t,e)},Y.prototype.finalize=function(){if(this.inputActive)throw new Error(i);return P.prototype.finalize.call(this)},K.prototype=new P,K.prototype._finishInner=function(){P.prototype.update.call(this,this.inner.array()),this.inner=null,this.innerBytes=0,++this.blockNumber},K.prototype._updateBytes=function(t){var e=t.length;if(e)for(var r=t.subarray||t.slice,n=this.blockSize,i=0;i>>6,a[s++]=128|63&r):r<55296||r>=57344?(a[s++]=224|r>>>12,a[s++]=128|r>>>6&63,a[s++]=128|63&r):(r=65536+((1023&r)<<10|1023&t.charCodeAt(++i)),a[s++]=240|r>>>18,a[s++]=128|r>>>12&63,a[s++]=128|r>>>6&63,a[s++]=128|63&r);t=a}return this._updateBytes(t),this},K.prototype.finalize=function(){return this.finalized||(this.inner&&this._finishInner(),this.encode(this.blockNumber,!0)),P.prototype.finalize.call(this)};var q=function(t){var e,r,n,i,a,o,s,u,h,f,c,p,l,y,k,b,v,_,g,w,x,B,A,m,C,S,z,M,E,O,N,I,R,J,j,H,F,U,D,L,T,V,W,P,Y,K,q,G,Q,X,Z,$,tt,et,rt,nt,it,at,ot,st,ut,ht,ft;for(n=0;n<48;n+=2)i=t[0]^t[10]^t[20]^t[30]^t[40],a=t[1]^t[11]^t[21]^t[31]^t[41],o=t[2]^t[12]^t[22]^t[32]^t[42],s=t[3]^t[13]^t[23]^t[33]^t[43],u=t[4]^t[14]^t[24]^t[34]^t[44],h=t[5]^t[15]^t[25]^t[35]^t[45],f=t[6]^t[16]^t[26]^t[36]^t[46],c=t[7]^t[17]^t[27]^t[37]^t[47],e=(p=t[8]^t[18]^t[28]^t[38]^t[48])^(o<<1|s>>>31),r=(l=t[9]^t[19]^t[29]^t[39]^t[49])^(s<<1|o>>>31),t[0]^=e,t[1]^=r,t[10]^=e,t[11]^=r,t[20]^=e,t[21]^=r,t[30]^=e,t[31]^=r,t[40]^=e,t[41]^=r,e=i^(u<<1|h>>>31),r=a^(h<<1|u>>>31),t[2]^=e,t[3]^=r,t[12]^=e,t[13]^=r,t[22]^=e,t[23]^=r,t[32]^=e,t[33]^=r,t[42]^=e,t[43]^=r,e=o^(f<<1|c>>>31),r=s^(c<<1|f>>>31),t[4]^=e,t[5]^=r,t[14]^=e,t[15]^=r,t[24]^=e,t[25]^=r,t[34]^=e,t[35]^=r,t[44]^=e,t[45]^=r,e=u^(p<<1|l>>>31),r=h^(l<<1|p>>>31),t[6]^=e,t[7]^=r,t[16]^=e,t[17]^=r,t[26]^=e,t[27]^=r,t[36]^=e,t[37]^=r,t[46]^=e,t[47]^=r,e=f^(i<<1|a>>>31),r=c^(a<<1|i>>>31),t[8]^=e,t[9]^=r,t[18]^=e,t[19]^=r,t[28]^=e,t[29]^=r,t[38]^=e,t[39]^=r,t[48]^=e,t[49]^=r,y=t[0],k=t[1],K=t[11]<<4|t[10]>>>28,q=t[10]<<4|t[11]>>>28,M=t[20]<<3|t[21]>>>29,E=t[21]<<3|t[20]>>>29,st=t[31]<<9|t[30]>>>23,ut=t[30]<<9|t[31]>>>23,V=t[40]<<18|t[41]>>>14,W=t[41]<<18|t[40]>>>14,J=t[2]<<1|t[3]>>>31,j=t[3]<<1|t[2]>>>31,b=t[13]<<12|t[12]>>>20,v=t[12]<<12|t[13]>>>20,G=t[22]<<10|t[23]>>>22,Q=t[23]<<10|t[22]>>>22,O=t[33]<<13|t[32]>>>19,N=t[32]<<13|t[33]>>>19,ht=t[42]<<2|t[43]>>>30,ft=t[43]<<2|t[42]>>>30,et=t[5]<<30|t[4]>>>2,rt=t[4]<<30|t[5]>>>2,H=t[14]<<6|t[15]>>>26,F=t[15]<<6|t[14]>>>26,_=t[25]<<11|t[24]>>>21,g=t[24]<<11|t[25]>>>21,X=t[34]<<15|t[35]>>>17,Z=t[35]<<15|t[34]>>>17,I=t[45]<<29|t[44]>>>3,R=t[44]<<29|t[45]>>>3,m=t[6]<<28|t[7]>>>4,C=t[7]<<28|t[6]>>>4,nt=t[17]<<23|t[16]>>>9,it=t[16]<<23|t[17]>>>9,U=t[26]<<25|t[27]>>>7,D=t[27]<<25|t[26]>>>7,w=t[36]<<21|t[37]>>>11,x=t[37]<<21|t[36]>>>11,$=t[47]<<24|t[46]>>>8,tt=t[46]<<24|t[47]>>>8,P=t[8]<<27|t[9]>>>5,Y=t[9]<<27|t[8]>>>5,S=t[18]<<20|t[19]>>>12,z=t[19]<<20|t[18]>>>12,at=t[29]<<7|t[28]>>>25,ot=t[28]<<7|t[29]>>>25,L=t[38]<<8|t[39]>>>24,T=t[39]<<8|t[38]>>>24,B=t[48]<<14|t[49]>>>18,A=t[49]<<14|t[48]>>>18,t[0]=y^~b&_,t[1]=k^~v&g,t[10]=m^~S&M,t[11]=C^~z&E,t[20]=J^~H&U,t[21]=j^~F&D,t[30]=P^~K&G,t[31]=Y^~q&Q,t[40]=et^~nt&at,t[41]=rt^~it&ot,t[2]=b^~_&w,t[3]=v^~g&x,t[12]=S^~M&O,t[13]=z^~E&N,t[22]=H^~U&L,t[23]=F^~D&T,t[32]=K^~G&X,t[33]=q^~Q&Z,t[42]=nt^~at&st,t[43]=it^~ot&ut,t[4]=_^~w&B,t[5]=g^~x&A,t[14]=M^~O&I,t[15]=E^~N&R,t[24]=U^~L&V,t[25]=D^~T&W,t[34]=G^~X&$,t[35]=Q^~Z&tt,t[44]=at^~st&ht,t[45]=ot^~ut&ft,t[6]=w^~B&y,t[7]=x^~A&k,t[16]=O^~I&m,t[17]=N^~R&C,t[26]=L^~V&J,t[27]=T^~W&j,t[36]=X^~$&P,t[37]=Z^~tt&Y,t[46]=st^~ht&et,t[47]=ut^~ft&rt,t[8]=B^~y&b,t[9]=A^~k&v,t[18]=I^~m&S,t[19]=R^~C&z,t[28]=V^~J&H,t[29]=W^~j&F,t[38]=$^~P&K,t[39]=tt^~Y&q,t[48]=ht^~et&nt,t[49]=ft^~rt&it,t[0]^=d[n],t[1]^=d[n+1]};if(u)r.exports=j;else for(F=0;F268435455)throw new Error("block size is invalid");var s=new K(t,e,a,r,i).bytepad(["ParallelHash",o],n);return s.encode(i,!1),s},i.update=function(t,e,r,n){return i.create(e,r,n).update(t)},E(i,function(t,e,n){return M(t,0,r,n)},t,e)},J=[{name:"keccak",padding:[1,256,65536,16777216],bits:y,createMethod:O},{name:"sha3",padding:[6,1536,393216,100663296],bits:y,createMethod:O},{name:"shake",padding:c,bits:k,createMethod:function(t,e){var r=m(t,e,"hex");return r.create=function(r){return new W(t,e,r)},r.update=function(t,e){return r.create(e).update(t)},E(r,m,t,e)}},{name:"cshake",padding:p,bits:k,createMethod:function(t,e){var r=v[t],n=C(t,0,"hex");return n.create=function(n,i,a){return x(i)&&x(a)?j["shake"+t].create(n):new W(t,e,n).bytepad([i,a],r)},n.update=function(t,e,r,i){return n.create(e,r,i).update(t)},E(n,C,t,e)}},{name:"kmac",padding:p,bits:k,createMethod:function(t,e){return N(t,e,!1)}},{name:"kmacxof",padding:p,bits:k,createMethod:function(t,e){return N(t,e,!0)}},{name:"tuplehash",padding:p,bits:k,createMethod:function(t,e){return I(t,e,!1)}},{name:"tuplehashxof",padding:p,bits:k,createMethod:function(t,e){return I(t,e,!0)}},{name:"parallelhash",padding:p,bits:k,createMethod:function(t,e){return R(t,e,!1)}},{name:"parallelhashxof",padding:p,bits:k,createMethod:function(t,e){return R(t,e,!0)}}],j={},H=[],F=0;F>5,this.byteCount=this.blockCount<<2,this.outputBlocks=r>>5,this.extraBytes=(31&r)>>3;for(var n=0;n<50;++n)this.s[n]=0}function P(t,e,r,n){W.call(this,t,e,r),this.xof=n}function Y(t,e,r,n){P.call(this,t,e,r,n),this.inputActive=!1,this.inputBytesRemaining=0}function K(t,e,r,n,i){P.call(this,t,e,r,n),this.bits=t,this.blockSize=i,this.inner=null,this.innerBytes=0,this.blockNumber=0}W.prototype.update=function(t){if(this.finalized)throw new Error(n);var e=w(t);t=e[0];for(var r,i,a,o=e[1],s=this.blocks,u=this.byteCount,h=t.length,f=this.blockCount,c=0,p=this.s;c>2]|=i<>2]|=(192|i>>6)<>2]|=(128|63&i)<=57344?(s[r>>2]|=(224|i>>12)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<=56320&&a<57344?(++c,i=65536+((1023&i)<<10|1023&a),s[r>>2]|=(240|i>>18)<>2]|=(128|i>>12&63)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<>2]|=239<>2]|=191<>2]|=189<>2]|=t[c]<=u){for(this.start=r-u,this.block=s[f],r=0;r>=8);r>0;)i.unshift(r),r=255&(t>>=8),++n;return e?i.push(n):i.unshift(n),W.prototype.update.call(this,i),i.length},W.prototype.encodeString=function(t){var e=w(t);t=e[0];var r=e[1],n=0,i=t.length;if(r)for(var a=0;a=57344?n+=3:o<56320&&(o=t.charCodeAt(a+1))>=56320&&o<57344?(++a,n+=4):n+=3}else n=i;return n+=this.encode(8*n),W.prototype.update.call(this,t),n},W.prototype.bytepad=function(t,e){for(var r=this.encode(e),n=0;n>2]|=this.padding[3&e],this.lastByteIndex>=this.byteCount)for(t[0]=t[r],e=1;e>4&15]+f[15&t]+f[t>>12&15]+f[t>>8&15]+f[t>>20&15]+f[t>>16&15]+f[t>>28&15]+f[t>>24&15];o%e===0&&(r=B(r),q(r),a=0)}return i&&(t=r[a],s+=f[t>>4&15]+f[15&t],i>1&&(s+=f[t>>12&15]+f[t>>8&15]),i>2&&(s+=f[t>>20&15]+f[t>>16&15])),s},W.prototype.arrayBuffer=function(){this.finalize();var t,e=this.blockCount,r=this.s,n=this.outputBlocks,i=this.extraBytes,a=0,o=0,s=this.outputBits>>3;t=i?new ArrayBuffer(n+1<<2):new ArrayBuffer(s);for(var u=new Uint32Array(t);o>8&255,u[t+2]=e>>16&255,u[t+3]=e>>24&255;s%r===0&&(n=B(n),q(n))}return a&&(t=s<<2,e=n[o],u[t]=255&e,a>1&&(u[t+1]=e>>8&255),a>2&&(u[t+2]=e>>16&255)),u},P.prototype=new W,P.prototype.finalize=function(){return this.finalized||this.encode(this.xof?0:this.outputBits,!0),W.prototype.finalize.call(this)},Y.prototype=new P,Y.prototype.getMessageByteLength=function(t){var e=w(t);if(t=e[0],!e[1])return t.length;for(var r=0,n=0;n=57344?r+=3:i<56320&&(i=t.charCodeAt(n+1))>=56320&&i<57344?(++n,r+=4):r+=3}return r},Y.prototype.beginInput=function(t){if(this.inputActive)throw new Error(i);if("number"!=typeof t||!isFinite(t)||t<0||Math.floor(t)!==t||t>268435455)throw new Error("tuple input byte length is invalid");return this.encode(8*t,!1),0!==t&&(this.inputActive=!0,this.inputBytesRemaining=t),this},Y.prototype._updateChunk=function(t,e){return P.prototype.update.call(this,t),this.inputBytesRemaining-=e,0===this.inputBytesRemaining&&(this.inputActive=!1),this},Y.prototype.updateChunk=function(t){if(!this.inputActive)throw new Error("no active tuple input");var e=this.getMessageByteLength(t);if(e>this.inputBytesRemaining)throw new Error("tuple input exceeds declared length");return this._updateChunk(t,e)},Y.prototype.update=function(t){var e=this.getMessageByteLength(t);return this.beginInput(e),this._updateChunk(t,e)},Y.prototype.finalize=function(){if(this.inputActive)throw new Error(i);return P.prototype.finalize.call(this)},K.prototype=new P,K.prototype._finishInner=function(){P.prototype.update.call(this,this.inner.array()),this.inner=null,this.innerBytes=0,++this.blockNumber},K.prototype._updateBytes=function(t){var e=t.length;if(e)for(var r=t.subarray||t.slice,n=this.blockSize,i=0;i>>6,o[u++]=128|63&r):r<55296||r>=57344?(o[u++]=224|r>>>12,o[u++]=128|r>>>6&63,o[u++]=128|63&r):r<56320&&(i=t.charCodeAt(a+1))>=56320&&i<57344?(++a,r=65536+((1023&r)<<10|1023&i),o[u++]=240|r>>>18,o[u++]=128|r>>>12&63,o[u++]=128|r>>>6&63,o[u++]=128|63&r):(o[u++]=239,o[u++]=191,o[u++]=189);t=o}return this._updateBytes(t),this},K.prototype.finalize=function(){return this.finalized||(this.inner&&this._finishInner(),this.encode(this.blockNumber,!0)),P.prototype.finalize.call(this)};var q=function(t){var e,r,n,i,a,o,s,u,h,f,c,p,l,y,k,b,v,_,g,w,x,B,A,m,C,S,z,M,E,O,N,I,R,J,j,H,F,U,D,L,T,V,W,P,Y,K,q,G,Q,X,Z,$,tt,et,rt,nt,it,at,ot,st,ut,ht,ft;for(n=0;n<48;n+=2)i=t[0]^t[10]^t[20]^t[30]^t[40],a=t[1]^t[11]^t[21]^t[31]^t[41],o=t[2]^t[12]^t[22]^t[32]^t[42],s=t[3]^t[13]^t[23]^t[33]^t[43],u=t[4]^t[14]^t[24]^t[34]^t[44],h=t[5]^t[15]^t[25]^t[35]^t[45],f=t[6]^t[16]^t[26]^t[36]^t[46],c=t[7]^t[17]^t[27]^t[37]^t[47],e=(p=t[8]^t[18]^t[28]^t[38]^t[48])^(o<<1|s>>>31),r=(l=t[9]^t[19]^t[29]^t[39]^t[49])^(s<<1|o>>>31),t[0]^=e,t[1]^=r,t[10]^=e,t[11]^=r,t[20]^=e,t[21]^=r,t[30]^=e,t[31]^=r,t[40]^=e,t[41]^=r,e=i^(u<<1|h>>>31),r=a^(h<<1|u>>>31),t[2]^=e,t[3]^=r,t[12]^=e,t[13]^=r,t[22]^=e,t[23]^=r,t[32]^=e,t[33]^=r,t[42]^=e,t[43]^=r,e=o^(f<<1|c>>>31),r=s^(c<<1|f>>>31),t[4]^=e,t[5]^=r,t[14]^=e,t[15]^=r,t[24]^=e,t[25]^=r,t[34]^=e,t[35]^=r,t[44]^=e,t[45]^=r,e=u^(p<<1|l>>>31),r=h^(l<<1|p>>>31),t[6]^=e,t[7]^=r,t[16]^=e,t[17]^=r,t[26]^=e,t[27]^=r,t[36]^=e,t[37]^=r,t[46]^=e,t[47]^=r,e=f^(i<<1|a>>>31),r=c^(a<<1|i>>>31),t[8]^=e,t[9]^=r,t[18]^=e,t[19]^=r,t[28]^=e,t[29]^=r,t[38]^=e,t[39]^=r,t[48]^=e,t[49]^=r,y=t[0],k=t[1],K=t[11]<<4|t[10]>>>28,q=t[10]<<4|t[11]>>>28,M=t[20]<<3|t[21]>>>29,E=t[21]<<3|t[20]>>>29,st=t[31]<<9|t[30]>>>23,ut=t[30]<<9|t[31]>>>23,V=t[40]<<18|t[41]>>>14,W=t[41]<<18|t[40]>>>14,J=t[2]<<1|t[3]>>>31,j=t[3]<<1|t[2]>>>31,b=t[13]<<12|t[12]>>>20,v=t[12]<<12|t[13]>>>20,G=t[22]<<10|t[23]>>>22,Q=t[23]<<10|t[22]>>>22,O=t[33]<<13|t[32]>>>19,N=t[32]<<13|t[33]>>>19,ht=t[42]<<2|t[43]>>>30,ft=t[43]<<2|t[42]>>>30,et=t[5]<<30|t[4]>>>2,rt=t[4]<<30|t[5]>>>2,H=t[14]<<6|t[15]>>>26,F=t[15]<<6|t[14]>>>26,_=t[25]<<11|t[24]>>>21,g=t[24]<<11|t[25]>>>21,X=t[34]<<15|t[35]>>>17,Z=t[35]<<15|t[34]>>>17,I=t[45]<<29|t[44]>>>3,R=t[44]<<29|t[45]>>>3,m=t[6]<<28|t[7]>>>4,C=t[7]<<28|t[6]>>>4,nt=t[17]<<23|t[16]>>>9,it=t[16]<<23|t[17]>>>9,U=t[26]<<25|t[27]>>>7,D=t[27]<<25|t[26]>>>7,w=t[36]<<21|t[37]>>>11,x=t[37]<<21|t[36]>>>11,$=t[47]<<24|t[46]>>>8,tt=t[46]<<24|t[47]>>>8,P=t[8]<<27|t[9]>>>5,Y=t[9]<<27|t[8]>>>5,S=t[18]<<20|t[19]>>>12,z=t[19]<<20|t[18]>>>12,at=t[29]<<7|t[28]>>>25,ot=t[28]<<7|t[29]>>>25,L=t[38]<<8|t[39]>>>24,T=t[39]<<8|t[38]>>>24,B=t[48]<<14|t[49]>>>18,A=t[49]<<14|t[48]>>>18,t[0]=y^~b&_,t[1]=k^~v&g,t[10]=m^~S&M,t[11]=C^~z&E,t[20]=J^~H&U,t[21]=j^~F&D,t[30]=P^~K&G,t[31]=Y^~q&Q,t[40]=et^~nt&at,t[41]=rt^~it&ot,t[2]=b^~_&w,t[3]=v^~g&x,t[12]=S^~M&O,t[13]=z^~E&N,t[22]=H^~U&L,t[23]=F^~D&T,t[32]=K^~G&X,t[33]=q^~Q&Z,t[42]=nt^~at&st,t[43]=it^~ot&ut,t[4]=_^~w&B,t[5]=g^~x&A,t[14]=M^~O&I,t[15]=E^~N&R,t[24]=U^~L&V,t[25]=D^~T&W,t[34]=G^~X&$,t[35]=Q^~Z&tt,t[44]=at^~st&ht,t[45]=ot^~ut&ft,t[6]=w^~B&y,t[7]=x^~A&k,t[16]=O^~I&m,t[17]=N^~R&C,t[26]=L^~V&J,t[27]=T^~W&j,t[36]=X^~$&P,t[37]=Z^~tt&Y,t[46]=st^~ht&et,t[47]=ut^~ft&rt,t[8]=B^~y&b,t[9]=A^~k&v,t[18]=I^~m&S,t[19]=R^~C&z,t[28]=V^~J&H,t[29]=W^~j&F,t[38]=$^~P&K,t[39]=tt^~Y&q,t[48]=ht^~et&nt,t[49]=ft^~rt&it,t[0]^=d[n],t[1]^=d[n+1]};if(u)r.exports=j;else for(F=0;F> 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 { @@ -393,9 +399,12 @@ var sha3$1 = {exports: {}}; bytes += 2; } else if (code < 0xd800 || code >= 0xe000) { bytes += 3; - } else { - code = 0x10000 + (((code & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff)); + } else if (code < 0xdc00 && (code = str.charCodeAt(i + 1)) >= 0xdc00 && code < 0xe000) { + ++i; bytes += 4; + } else { + // unpaired surrogate: U+FFFD + bytes += 3; } } } else { @@ -573,9 +582,12 @@ var sha3$1 = {exports: {}}; bytes += 2; } else if (code < 0xd800 || code >= 0xe000) { bytes += 3; - } else { + } else if (code < 0xdc00 && (code = message.charCodeAt(i + 1)) >= 0xdc00 && code < 0xe000) { ++i; bytes += 4; + } else { + // unpaired surrogate: U+FFFD + bytes += 3; } } return bytes; @@ -681,7 +693,7 @@ var sha3$1 = {exports: {}}; var result = formatMessage(message); message = result[0]; if (result[1]) { - var bytes = [], length = message.length, index = 0, code, i; + var bytes = [], length = message.length, index = 0, code, code2, i; for (i = 0; i < length; ++i) { code = message.charCodeAt(i); if (code < 0x80) { @@ -693,12 +705,18 @@ var sha3$1 = {exports: {}}; bytes[index++] = (0xe0 | (code >>> 12)); bytes[index++] = (0x80 | ((code >>> 6) & 0x3f)); bytes[index++] = (0x80 | (code & 0x3f)); - } else { - code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++i) & 0x3ff)); + } else if (code < 0xdc00 && (code2 = message.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; } } message = bytes; diff --git a/package-lock.json b/package-lock.json index c77bed0..6215b5f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "js-sha3", - "version": "0.14.0", + "version": "0.15.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "js-sha3", - "version": "0.14.0", + "version": "0.15.0", "license": "MIT", "devDependencies": { "@rollup/plugin-commonjs": "^28.0.5", diff --git a/package.json b/package.json index 611ee29..0daf3c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-sha3", - "version": "0.14.0", + "version": "0.15.0", "description": "A simple SHA-3 / Keccak / SHAKE / cSHAKE / KMAC / TupleHash / ParallelHash hash function for JavaScript supports UTF-8 encoding.", "main": "src/sha3.js", "module": "build/sha3.mjs", diff --git a/src/sha3.js b/src/sha3.js index 6ffaf95..87c3e18 100644 --- a/src/sha3.js +++ b/src/sha3.js @@ -1,7 +1,7 @@ /** * [js-sha3]{@link https://github.com/emn178/js-sha3} * - * @version 0.14.0 + * @version 0.15.0 * @author Chen, Yi-Cyuan [emn178@gmail.com] * @copyright Chen, Yi-Cyuan 2015-2026 * @license MIT @@ -300,7 +300,7 @@ message = result[0]; var isString = result[1]; var blocks = this.blocks, byteCount = this.byteCount, length = message.length, - blockCount = this.blockCount, index = 0, s = this.s, i, code; + blockCount = this.blockCount, index = 0, s = this.s, i, code, code2; while (index < length) { if (this.reset) { @@ -322,12 +322,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 { @@ -385,9 +391,12 @@ bytes += 2; } else if (code < 0xd800 || code >= 0xe000) { bytes += 3; - } else { - code = 0x10000 + (((code & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff)); + } else if (code < 0xdc00 && (code = str.charCodeAt(i + 1)) >= 0xdc00 && code < 0xe000) { + ++i; bytes += 4; + } else { + // unpaired surrogate: U+FFFD + bytes += 3; } } } else { @@ -565,9 +574,12 @@ bytes += 2; } else if (code < 0xd800 || code >= 0xe000) { bytes += 3; - } else { + } else if (code < 0xdc00 && (code = message.charCodeAt(i + 1)) >= 0xdc00 && code < 0xe000) { ++i; bytes += 4; + } else { + // unpaired surrogate: U+FFFD + bytes += 3; } } return bytes; @@ -673,7 +685,7 @@ var result = formatMessage(message); message = result[0]; if (result[1]) { - var bytes = [], length = message.length, index = 0, code, i; + var bytes = [], length = message.length, index = 0, code, code2, i; for (i = 0; i < length; ++i) { code = message.charCodeAt(i); if (code < 0x80) { @@ -685,12 +697,18 @@ bytes[index++] = (0xe0 | (code >>> 12)); bytes[index++] = (0x80 | ((code >>> 6) & 0x3f)); bytes[index++] = (0x80 | (code & 0x3f)); - } else { - code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++i) & 0x3ff)); + } else if (code < 0xdc00 && (code2 = message.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; } } message = bytes; diff --git a/tests/test-cshake.js b/tests/test-cshake.js index 73cfa6a..1ff437f 100644 --- a/tests/test-cshake.js +++ b/tests/test-cshake.js @@ -46,6 +46,13 @@ n: new Uint8Array([]), s: new Uint8Array([]), output: '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26' + }, + { + input: 'a\ud83db', + bits: 256, + n: '\ud800', + s: '\udc00x', + output: 'e097e791962538e81d2d450b3f1774cffc79d67a32959daa313cfcca60a9abd8' } ] }, @@ -73,6 +80,13 @@ n: '', s: '', output: '46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762fd75dc4ddd8c0f200cb05019d67b592f6fc821c49479ab48640292eacb3b7c4be' + }, + { + input: 'a\ud83db', + bits: 512, + n: '\ud800', + s: '\udc00x', + output: '428745fb92620c2c18e5c4d17cd4adec84552210ca030e8bb30b01ee3da50cc84afecea24fe62ee72ac3c0ecce8ef1342d1eafe8bd2222ccb48c834e4a1ab882' } ] } diff --git a/tests/test-kmac.js b/tests/test-kmac.js index 3583c4b..77d6e76 100644 --- a/tests/test-kmac.js +++ b/tests/test-kmac.js @@ -44,6 +44,13 @@ key: key, s: 'My Tagged Application', output: '1f5b4e6cca02209e0dcb5ca635b89a15e271ecc760071dfd805faa38f9729230' + }, + { + input: 'a\ud83db', + bits: 256, + key: '\ud800', + s: '\udc00x', + output: '6ced45b8c304287973b0af2866e7d8679daf4ad6827b5072fc5c5d7fa44cb131' } ] }, @@ -78,6 +85,13 @@ key: [], s: [8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173, 255, 41, 197, 8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173, 255, 41, 197, 8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173, 255, 41, 197, 8, 79, 237, 8, 185, 120, 175, 77, 125, 25, 106, 116, 70, 168, 107, 88, 0, 158, 99, 107, 97, 29, 177, 98, 17, 182, 90, 154, 173], output: '031801b0b50ebeef772fbe7a279bc144' + }, + { + input: 'a\ud83db', + bits: 512, + key: '\ud800', + s: '\udc00x', + output: '8831d05374e0a1ee9daba2b44fb15c28b932a3cbd38df54f4981152d4a8d4a7eb6be7f44a67fb65be06fd2312216ad6df76de46e4a3767c0ab56f22a5264d012' } ] }, @@ -105,6 +119,13 @@ key: key, s: 'My Tagged Application', output: '47026c7cd793084aa0283c253ef658490c0db61438b8326fe9bddf281b83ae0f' + }, + { + input: 'a\ud83db', + bits: 256, + key: '\ud800', + s: '\udc00x', + output: 'ee996214db121dc0704999849ea2bc73ca3171d5cbda3f536ed84b3763675f7b' } ] }, @@ -132,6 +153,13 @@ key: key, s: 'My Tagged Application', output: 'd5be731c954ed7732846bb59dbe3a8e30f83e77a4bff4459f2f1c2b4ecebb8ce67ba01c62e8ab8578d2d499bd1bb276768781190020a306a97de281dcc30305d' + }, + { + input: 'a\ud83db', + bits: 512, + key: '\ud800', + s: '\udc00x', + output: '80b28e96b077368d5148398424be5ffb4450b15e2e852443bb74a9140799d49471609ce2bd2fcd04294539cf27439aa51f6619d224eacfcc98c1de27e2b59b02' } ] } diff --git a/tests/test-parallelhash.js b/tests/test-parallelhash.js index da23852..662c19f 100644 --- a/tests/test-parallelhash.js +++ b/tests/test-parallelhash.js @@ -188,6 +188,15 @@ expect(hash.array().length).to.be(32); }); + it('should encode unpaired surrogates as U+FFFD', function () { + // U+FFFD = ef bf bd + expect(parallelhash128('a\uD83Db', 2, 256, '\uDC00x')).to.be( + parallelhash128([0x61, 0xef, 0xbf, 0xbd, 0x62], 2, 256, [0xef, 0xbf, 0xbd, 0x78])); + var hash = parallelhash128.create(2, 256, ''); + hash.update('a\uD83D').update('\uDE00'); // surrogate pair split across updates + expect(hash.hex()).to.be(parallelhash128([0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd], 2, 256, '')); + }); + it('should keep absorbing when update is shorter than remaining block space', function () { var expected = parallelhash128(d1, 8, 256, ''); var hash = parallelhash128.create(8, 256, ''); diff --git a/tests/test-shake.js b/tests/test-shake.js index 9943122..f363bca 100644 --- a/tests/test-shake.js +++ b/tests/test-shake.js @@ -8,6 +8,13 @@ }); }); + context('with unpaired surrogates', function () { + it('should be equal', function () { + expect(shake128('\ud800', 256)).to.be('e222dd713a77e63b0ba5f39b311e77489f3c40fccc063ceb82c5c6ea860987c0'); + expect(shake128('a\ud83db', 256)).to.be('b45a441a78730b4c83adef9b362a99a6a2ca5a64460727eb32d817d3de34d36e'); + }); + }); + context('with 8 output', function () { it('should be equal', function () { expect(shake128('', 8)).to.be('7f'); @@ -56,6 +63,13 @@ }); }); + context('with unpaired surrogates', function () { + it('should be equal', function () { + expect(shake256('\ud800', 512)).to.be('36451a2c4c562eafae4f5d1cc0f030e59722e7efcb49194fe7d9dbe41f0adc9acbed8950d7bd20c1b5354072c9bb857ab5f982ea80aa005dd5d98102f57d6862'); + expect(shake256('a\ud83db', 512)).to.be('3859bbc4c5b31a5a173793a85ba90c9c189055adf8a12847c6051ee0890a1e8e63062acccfeedd38d053f8ad4080dd5af4356b6f62ad6b020f41b2f0bbfbd1af'); + }); + }); + context('with 8 output', function () { it('should be equal', function () { expect(shake256('', 8)).to.be('46'); diff --git a/tests/test-tuplehash.js b/tests/test-tuplehash.js index d1d0179..759e110 100644 --- a/tests/test-tuplehash.js +++ b/tests/test-tuplehash.js @@ -242,6 +242,15 @@ expect(hash.hex()).to.be(expected); }); + it('should encode unpaired surrogates as U+FFFD', function () { + // U+FFFD = ef bf bd + expect(tuplehash128(['\uD800', 'a\uD83Db'], 256, '\uDC00x')).to.be( + tuplehash128([[0xef, 0xbf, 0xbd], [0x61, 0xef, 0xbf, 0xbd, 0x62]], 256, [0xef, 0xbf, 0xbd, 0x78])); + var hash = tuplehash128.create(256, ''); + hash.beginInput(5).updateChunk('a\uD83D').updateChunk('b'); // 1 + 3 + 1 = 5 UTF-8 bytes + expect(hash.hex()).to.be(tuplehash128([[0x61, 0xef, 0xbf, 0xbd, 0x62]], 256, '')); + }); + it('should reject update and beginInput after finalize', function () { var hash = tuplehash128.create(256, '').update([0x00]); hash.hex(); diff --git a/tests/test.js b/tests/test.js index c6f62e5..fed98bc 100644 --- a/tests/test.js +++ b/tests/test.js @@ -28,6 +28,12 @@ '639bc16dfb4baf0e8b32ec1c0e6ae0904a780cf68bb922f503cf96c3eded9ea096a3ae7c0f4addfbf37caf8cc2769cd10a496f5711855abf61d0286b3052e498': Array(72).join('a') + 'é', '6d2a97560295a71d222a59af1b745d2d0e6bf0d776fd3c3e2073ad88a0c60e57b4eedf3f4cd26e36e967343bad190074bd2b5ed1ad2312ee68a2ef773cf94697': Array(72).join('a') + '𠜎' }, + 'UTF-8 unpaired surrogates': { + '9c8c356561ab2057fad388e665738bd6f163deecaca45e7dd01807acf7e7703af2260a55c3f957957ff65143f0a5093be196574d68bb219f9e42e1c7294a5951': '\ud800', + 'a3da1e1a56685cd0358aa99994da39199236e198366a160ca9a3332c33be0058116bf5b8f32e82cf20be24afd1da3a0b681ac905adbfa165861644fe6543c655': 'a\ud83db', + '82c7e9c3c2fc8f66921465653484192c0d16d177abac80519844f8d43eb4e86a860e1e8e1a63cd6f583e918d79de91d8beea7ef4c8afca1c805b9e861632bbb3': '\ud83d\ud83d\ude00\udfff', + 'e4a54fd1066d0d8ace85215ca4e7927a9b6adc86bcb6666fd11b749a590704c5012ae3865666525ed47b1cfa9f0ad98843cdbc1e8ae0cb6d1384af3714e8e0d8': Array(72).join('a') + '\ud800b' + }, 'UTF-8 more than 128 bytes': { 'accb127bb24b0ffbb7550dc637222d2f78538a8a186c98bc5efdad685b9b396639f34148bf0b94ed470f0e9c3665dc3b4c1cb321bacd32dd317a646295e073d9': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', '5b70eaad083f1b86fd535b6812e02f5f2876a4bd8b43aede8d62ae71bb1743ebd919dc41be56d73ba45b67b2876ff215d0575788560e7b0c92b879f8a2fc3111': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' @@ -59,6 +65,12 @@ '2adacd2e5baa621c6e2b34105246d27f70432668be218b90d8c05a71e128f1c7e01cf9f622274114f31a0ac9bc874a51': Array(104).join('a') + 'é', '44ae4704e24fc0b8eb245df40b822016aa9762a0e72626da760010c3b5d8e8d210c86aa6a925a990a02608ea10d3210c': Array(104).join('a') + '𠜎' }, + 'UTF-8 unpaired surrogates': { + '781192fb48ef5d7c063d63040aa8e43c1e10173fd9d8d6eb804a73ea71bfe30cac86ba0ba1d33ca519c01d3824755e60': '\ud800', + '218b21f7f11fb2928ba289172b6beb9dfa40d45bacabccbc90346a8c913807e6126c7f7a3e50742a17b68156652b1cb1': 'a\ud83db', + '40c2e07a2b9374bf0f1aa578f83d831d94835ffcbdc0bcf4d66034861e3cf99e47c02474d752b68e8e1ed39d5e26c802': '\ud83d\ud83d\ude00\udfff', + '1c948ff204d4cdd4c7cdbc3ab1dde9b730e7fabe65637c1164773ca9b3ad813da82cd66e8c674aa4766bea1e9b2adaf8': Array(104).join('a') + '\ud800b' + }, 'UTF-8 more than 128 bytes': { '7d0f80fe5c79a04a2a37a30a440e0cc068eb78fe6c3182246ede29645c144b5d33c44607cb2c3111ba77ffc66107f1cd': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', 'e344b95c6a961a27793eff00fa5103ef78b4180fe41c93fc60a31aff49b3b5e95a92c84fda9a6c80fa403b7df58db59f': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' @@ -82,6 +94,12 @@ 'be0c47012a6b606f80c1a0d1b61d07d001b210234be8dd47e6cbc094160432dd': Array(136).join('a') + 'é', 'a3e416e00eb9a844202cda999bde4a5af531ebe428796e4d56762d49877eb9dd': Array(136).join('a') + '𠜎' }, + 'UTF-8 unpaired surrogates': { + 'f285a8cfb6d23fe0c75e93e0d7e0bab68e93da8ec40f4df7400f21ac1b127ad9': '\ud800', + 'dde34180c763f225ab94a63a6bbc1d3278d7eaf0e1baca46f9141398a52e1efe': 'a\ud83db', + '78a19219a02c5d11207cea833a6e3057c753a48fa8b21e83ced981cc413aec64': '\ud83d\ud83d\ude00\udfff', + '54870c9243dca46e64cfcaf34d76555a34cac09ea91ddfa29fa05420017d5dce': Array(136).join('a') + '\ud800b' + }, 'UTF-8 more than 128 bytes': { '4b2f36e4320b86e6ead0ad001e47e6d9e7fcf0044cd5a5fd65490a633c0372a4': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', '558a7f843b1ac5e7a8bbef90357876bcce0612992d0dfa2907e95521612f507f': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' @@ -105,6 +123,12 @@ 'b5e1dd79fbbf42a1831f55f15eb4a945b3a40fb8e382ddffb00dd2c5': Array(144).join('a') + 'é', '95ef8807f0c48a6d5209eadb4250ac0fbbf1c9a7a4b19195e200b901': Array(144).join('a') + '𠜎' }, + 'UTF-8 unpaired surrogates': { + '56986fcda0236e08ae963a62b68e7601a44a0e39fe9eb08edde02040': '\ud800', + '93508b059bb7831dba2e73ce7d8fc6f235d78016281f6a9f789a1f8e': 'a\ud83db', + 'bab367e583c59babd135c9c928527bb2c7cd8eafdf824b498502a4a5': '\ud83d\ud83d\ude00\udfff', + 'db43660839a00f35f3444ce5d068a8f84ea7826fa12ce06e87011293': Array(144).join('a') + '\ud800b' + }, 'UTF-8 more than 128 bytes': { '135c13deb71fdf6fb77b52b720c43ddd6ce7467f9147a74248557114': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', 'bd05581e02445c53e05aad2014f6a3819d77a9dff918b8c6bf60bd06': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' @@ -128,6 +152,12 @@ '99e3f901f7b8f09cd99ed422fcdd2a999598a8ab9c32730b9d99b442242fdcd2974618735777f57b8973f329e170487eb56c0c17524bea7a720edabde7b1e020': Array(72).join('a') + 'é', '0aa818b80fc628c435cb9f90975e40573e7e5f9efb7101c90c982ade2daa3ac324b235e2065af8702353d99dcec1627248e2519d246d196246f5ff1f538878a6': Array(72).join('a') + '𠜎' }, + 'UTF-8 unpaired surrogates': { + '13f5ba5aa5cda2fb9a980ab461a23b66a965c8d3fcd72811e8842565360bd2a8df7a843de5a216fac45de94dc0ccdd8d89fa2ca11db73e7d07463a3268574237': '\ud800', + '909efc2ba5951f617acb150e98aee6722e74108ab03a2cad308352913ccc2bf36fea028d44eb8293d266d599ae30f0e4b61b98785705dd0aa2cb832a72aafd5d': 'a\ud83db', + '668653b333ae96dcd650cde2667fe037cf0999974f5a16bd008aa9e76efa0eac4e6abe14064b91f0ff03cec268322c997fef6e8f692d8d81e5aba2ececc818ef': '\ud83d\ud83d\ude00\udfff', + 'b3c2cf6bd8e3b6e7606813d66055cabbace0c36b923256f526fad49a839cefc2902916fd448d88e3c68ac9d9229564baa7264ae11b43545602f26d2e4f43ef82': Array(72).join('a') + '\ud800b' + }, 'UTF-8 more than 128 bytes': { '6a67c28aa1946ca1be8382b861aac4aaf20052f495db9b6902d13adfa603eaba5d169f8896b86d461b2949283eb98e503c3f0640188ea7d6731526fc06568d37': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', 'd04ff5b0e85e9968be2a4d4e133c15c7ccee7497198bb651599a97d11d00bca6048d329ab75aa454566cd532648fa1cb4551985d9d645de9fa43a311a9ee8e4d': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' @@ -151,6 +181,12 @@ 'ca105aaf06261204de3b068a2fd2d8587ac8267417a41490e6ee8aa81453e89e8bb5cc0d1fbe9116c8b50c27dd2428d9': Array(104).join('a') + 'é', '1712217829147c6acb2bb14ff6bf245e15fb28629a5b68b7aead07c48a87ab5c2bbcfc9b06d31a594d848e76247801cf': Array(104).join('a') + '𠜎' }, + 'UTF-8 unpaired surrogates': { + '1bf3476755b8cffdf70c075cf5be9a1d64d0c72aebd126e3393fc4fe38a9f976414762d6d34065e7978aa52465ed5e95': '\ud800', + 'c2343abdd720e7692144ffcc582c13be056ecbe38327f1dd7f4fb94271fa2030d0a95099064cf237cd84c2b4f93d734a': 'a\ud83db', + 'a653fa9f36e8fb58d4fbed1db4516c708c00729c53ef3c6251324be3b77996bfa4ea5cd2c3f2b8fb2b25d2a72565fedd': '\ud83d\ud83d\ude00\udfff', + '08300203440edc8b8a23da393bec920d8390b559730f95d5fb8dd153f6eaa3ea5db3bfa76c01d7dd69504fa017740011': Array(104).join('a') + '\ud800b' + }, 'UTF-8 more than 128 bytes': { 'a3b043a2f69e4326a05d478fa4c8aa2bd7612453d775af37665a0b96ef2207cdc74c50cdba1629796a5136fe77300b05': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', '66414c090cc3fe9c396d313cbaa100aefd335e851838b29382568b7f57357ada7c54b8fa8c17f859945bba88b2c2e332': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' @@ -174,6 +210,12 @@ '520699913aab3b7e02e624bdcff04e2830be50cd244a41e04bf8f22de65f90c4': Array(136).join('a') + 'é', 'ac7e0a188a9129385e9f8de8d975193c602b8505c4081a182d6c41a1b9b0092e': Array(136).join('a') + '𠜎' }, + 'UTF-8 unpaired surrogates': { + '395c830292afe91c21fe5a68e8bcded8ac367805c31fc216eed6147bf6213fdc': '\ud800', + '8c9cb248c7f59da84cfc93499a7a3d5b702fc9b972d0e205e6772019ac5b4bf9': 'a\ud83db', + 'f0b1d5713e6fb1ea6a8c2e0dabf57f8ec5d343bd9a3628a02e24ed6c95d7290d': '\ud83d\ud83d\ude00\udfff', + '436bc3f8fd588e5cbee8482f5b540c22a0426cdc3f42815dca493f9084dd64cb': Array(136).join('a') + '\ud800b' + }, 'UTF-8 more than 128 bytes': { 'd1021d2d4c5c7e88098c40f422af68493b4b64c913cbd68220bf5e6127c37a88': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', 'ffabf9bba2127c4928d360c9905cb4911f0ec21b9c3b89f3b242bccc68389e36': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' @@ -197,6 +239,12 @@ 'bc835bf25312b764f5ac035058d7cd4f0e46fed1b207872156ada8b6': Array(144).join('a') + 'é', '83ca71f6261c89d3458ceeb65f8593aa26542b33cfade266d1806769': Array(144).join('a') + '𠜎' }, + 'UTF-8 unpaired surrogates': { + 'b41fd6584717f2604f9c965c9805ad4eb7bfb5c228b470bedbb0c400': '\ud800', + '668be9af7377b7ef9c1073499e4423b0f360e30d0a9c6ff577790720': 'a\ud83db', + 'b5103455e85cbe4e0998353c9a3634ca5e9534cb2723e0298125fe8e': '\ud83d\ud83d\ude00\udfff', + 'c4df113ff02de6863ee56ad9e5e0b351e2e24cc4bfef4d528396da49': Array(144).join('a') + '\ud800b' + }, 'UTF-8 more than 128 bytes': { 'd59eef8f394ef7d96967bb0bde578785c033f7f0a21913d6ba41ed1b': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', '27123a2a3860d1041d4769778c4b078732bf4300f7e1c56536ab2644': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。'