mirror of
https://github.com/emn178/js-sha3.git
synced 2026-08-13 03:51:54 +08:00
- ParallelHash128, ParallelHash256, ParallelHashXOF128, and ParallelHashXOF256 (NIST SP 800-185)
9 lines
13 KiB
JavaScript
9 lines
13 KiB
JavaScript
/**
|
|
* [js-sha3]{@link https://github.com/emn178/js-sha3}
|
|
*
|
|
* @version 0.13.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;n<t.length;++n)e[n]=t[n];return e},S=function(e,n,r){return function(i){return new t(e,n,e).update(i)[r]()}},z=function(e,n,r){return function(i,o){return new t(e,n,o).update(i)[r]()}},M=function(t,e,n){return function(e,r,i,o){return U["cshake"+t].update(e,r,i,o)[n]()}},E=function(t,e,n,r){return function(e,i,o,a){return U[(n?"kmacxof":"kmac")+t].update(e,i,o,a)[r]()}},N=function(t,e,n,r){return function(e,i,o){return U[(n?"tuplehashxof":"tuplehash")+t].update(e,i,o)[r]()}},O=function(t,e,n,r){return function(e,i,o,a){return U[(n?"parallelhashxof":"parallelhash")+t].update(e,i,o,a)[r]()}},I=function(t,e,n,r){for(var i=0;i<B.length;++i){var o=B[i];t[o]=e(n,r,o)}return t},R=function(e,n){var r=S(e,n,"hex");return r.create=function(){return new t(e,n,e)},r.update=function(t){return r.create().update(t)},I(r,S,e,n)},J=function(t,n,r){var i=A[t],o=E(t,0,r,"hex");return o.create=function(o,a,u){return new e(t,n,a,r).bytepad(["KMAC",u],i).bytepad([o],i)},o.update=function(t,e,n,r){return o.create(t,n,r).update(e)},I(o,function(t,e,n){return E(t,0,r,n)},t,n)},j=function(t,e,r){var o=A[t],a=N(t,0,r,"hex");return a.create=function(i,a){return new n(t,e,i,r).bytepad(["TupleHash",a],o)},a.update=function(t,e,n){if(!k(t))throw new Error(i);for(var r=a.create(e,n),o=0;o<t.length;++o)r.update(t[o]);return r},I(a,function(t,e,n){return N(t,0,r,n)},t,e)},H=function(t,e,n){var i=A[t],o=O(t,0,n,"hex");return o.create=function(o,a,u){if("number"!=typeof o||!isFinite(o)||o<1||Math.floor(o)!==o||o>268435455)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<F.length;++L)for(var V=F[L],W=V.bits,Y=0;Y<W.length;++Y){var K=V.name+"_"+W[Y];if(D.push(K),U[K]=V.createMethod(W[Y],V.padding),"sha3"!==V.name){var P=V.name+W[Y];D.push(P),U[P]=U[K]}}t.prototype.update=function(t){if(this.finalized)throw new Error(o);var e=m(t);t=e[0];for(var n,r,i=e[1],a=this.blocks,u=this.byteCount,s=t.length,h=this.blockCount,f=0,c=this.s;f<s;){if(this.reset)for(this.reset=!1,a[0]=this.block,n=1;n<h+1;++n)a[n]=0;if(i)for(n=this.start;f<s&&n<u;++f)(r=t.charCodeAt(f))<128?a[n>>2]|=r<<b[3&n++]:r<2048?(a[n>>2]|=(192|r>>6)<<b[3&n++],a[n>>2]|=(128|63&r)<<b[3&n++]):r<55296||r>=57344?(a[n>>2]|=(224|r>>12)<<b[3&n++],a[n>>2]|=(128|r>>6&63)<<b[3&n++],a[n>>2]|=(128|63&r)<<b[3&n++]):(r=65536+((1023&r)<<10|1023&t.charCodeAt(++f)),a[n>>2]|=(240|r>>18)<<b[3&n++],a[n>>2]|=(128|r>>12&63)<<b[3&n++],a[n>>2]|=(128|r>>6&63)<<b[3&n++],a[n>>2]|=(128|63&r)<<b[3&n++]);else for(n=this.start;f<s&&n<u;++f)a[n>>2]|=t[f]<<b[3&n++];if(this.lastByteIndex=n,n>=u){for(this.start=n-u,this.block=a[h],n=0;n<h;++n)c[n]^=a[n];T(c),this.reset=!0}else this.start=n}return this},t.prototype.encode=function(e,n){var r=255&e,i=1,o=[r];for(r=255&(e>>=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<e.length;++a){var u=e.charCodeAt(a);u<128?i+=1:u<2048?i+=2:u<55296||u>=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<e.length;++i)r+=this.encodeString(e[i]);var o=(n-r%n)%n,a=[];return a.length=o,t.prototype.update.call(this,a),this},t.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,e=this.lastByteIndex,n=this.blockCount,r=this.s;if(t[e>>2]|=this.padding[3&e],this.lastByteIndex===this.byteCount)for(t[0]=t[n],e=1;e<n+1;++e)t[e]=0;for(t[n-1]|=2147483648,e=0;e<n;++e)r[e]^=t[e];T(r)}},t.prototype.toString=t.prototype.hex=function(){this.finalize();for(var t,e=this.blockCount,n=this.s,r=this.outputBlocks,i=this.extraBytes,o=0,a=0,u="";a<r;){for(o=0;o<e&&a<r;++o,++a)t=n[o],u+=l[t>>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<r;){for(o=0;o<e&&a<r;++o,++a)s[a]=n[o];a%e==0&&(n=C(n),T(n))}return i&&(s[a]=n[o],t=t.slice(0,u)),t},t.prototype.buffer=t.prototype.arrayBuffer,t.prototype.digest=t.prototype.array=function(){this.finalize();for(var t,e,n=this.blockCount,r=this.s,i=this.outputBlocks,o=this.extraBytes,a=0,u=0,s=[];u<i;){for(a=0;a<n&&u<i;++a,++u)t=u<<2,e=r[a],s[t]=255&e,s[t+1]=e>>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<t.length;++r){var i=t.charCodeAt(r);i<128?n+=1:i<2048?n+=2:i<55296||i>=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<n;){this.inner||(this.inner=new t(this.bits,d,this.bits<<1));var a=i-this.innerBytes;n-o<a&&(a=n-o),this.inner.update(r.call(e,o,o+a)),this.innerBytes+=a,o+=a,this.innerBytes===i&&this._finishInner()}},r.prototype.update=function(t){if(this.finalized)throw new Error(o);var e=m(t);if(t=e[0],e[1]){var n,r,i=[],a=t.length,u=0;for(r=0;r<a;++r)(n=t.charCodeAt(r))<128?i[u++]=n:n<2048?(i[u++]=192|n>>>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<D.length;++L)s[D[L]]=U[D[L]];c&&define(function(){return U})}}(); |