forked from Rabbit-Company/Argon2id-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Argon2idWorker.min.js
6 lines (6 loc) · 9.67 KB
/
Argon2idWorker.min.js
1
2
3
4
5
6
/*
Argon2id-JS v1.1.0
https://github.com/Rabbit-Company/Argon2id-JS
License: GPL-3.0
*/
function runWorker(){class t{v=new Uint32Array(32);m=new Uint32Array(32);BLAKE2B_IV32=new Uint32Array([4089235720,1779033703,2227873595,3144134277,4271175723,1013904242,1595750129,2773480762,2917565137,1359893119,725511199,2600822924,4215389547,528734635,327033209,1541459225]);SIGMA8=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3];parameterBlock=new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);SIGMA82=new Uint8Array(this.SIGMA8.map(function(t){return 2*t}));ADD64AA(t,e,_){let i=t[e]+t[_],l=t[e+1]+t[_+1];i>=4294967296&&l++,t[e]=i,t[e+1]=l}ADD64AC(t,e,_,i){let l=t[e]+_;_<0&&(l+=4294967296);let n=t[e+1]+i;l>=4294967296&&n++,t[e]=l,t[e+1]=n}B2B_GET32(t,e){return t[e]^t[e+1]<<8^t[e+2]<<16^t[e+3]<<24}B2B_G(t,e,_,i,l,n){let $=this.m[l],s=this.m[l+1],h=this.m[n],r=this.m[n+1];this.ADD64AA(this.v,t,e),this.ADD64AC(this.v,t,$,s);let a=this.v[i]^this.v[t],c=this.v[i+1]^this.v[t+1];this.v[i]=c,this.v[i+1]=a,this.ADD64AA(this.v,_,i),a=this.v[e]^this.v[_],c=this.v[e+1]^this.v[_+1],this.v[e]=a>>>24^c<<8,this.v[e+1]=c>>>24^a<<8,this.ADD64AA(this.v,t,e),this.ADD64AC(this.v,t,h,r),a=this.v[i]^this.v[t],c=this.v[i+1]^this.v[t+1],this.v[i]=a>>>16^c<<16,this.v[i+1]=c>>>16^a<<16,this.ADD64AA(this.v,_,i),a=this.v[e]^this.v[_],c=this.v[e+1]^this.v[_+1],this.v[e]=c>>>31^a<<1,this.v[e+1]=a>>>31^c<<1}blake2bCompress(t,e){let _=0;for(_=0;_<16;_++)this.v[_]=t.h[_],this.v[_+16]=this.BLAKE2B_IV32[_];for(this.v[24]=this.v[24]^t.t,this.v[25]=this.v[25]^t.t/4294967296,e&&(this.v[28]=~this.v[28],this.v[29]=~this.v[29]),_=0;_<32;_++)this.m[_]=this.B2B_GET32(t.b,4*_);for(_=0;_<12;_++)this.B2B_G(0,8,16,24,this.SIGMA82[16*_+0],this.SIGMA82[16*_+1]),this.B2B_G(2,10,18,26,this.SIGMA82[16*_+2],this.SIGMA82[16*_+3]),this.B2B_G(4,12,20,28,this.SIGMA82[16*_+4],this.SIGMA82[16*_+5]),this.B2B_G(6,14,22,30,this.SIGMA82[16*_+6],this.SIGMA82[16*_+7]),this.B2B_G(0,10,20,30,this.SIGMA82[16*_+8],this.SIGMA82[16*_+9]),this.B2B_G(2,12,22,24,this.SIGMA82[16*_+10],this.SIGMA82[16*_+11]),this.B2B_G(4,14,16,26,this.SIGMA82[16*_+12],this.SIGMA82[16*_+13]),this.B2B_G(6,8,18,28,this.SIGMA82[16*_+14],this.SIGMA82[16*_+15]);for(_=0;_<16;_++)t.h[_]=t.h[_]^this.v[_]^this.v[_+16]}blake2bInit(t,e,_,i){if(0===t||t>64)throw Error("Illegal output length, expected 0 < length <= 64");if(e&&e.length>64)throw Error("Illegal key, expected Uint8Array with 0 < length <= 64");if(_&&16!==_.length)throw Error("Illegal salt, expected Uint8Array with length is 16");if(i&&16!==i.length)throw Error("Illegal personal, expected Uint8Array with length is 16");let l={b:new Uint8Array(128),h:new Uint32Array(16),t:0,c:0,outlen:t};this.parameterBlock.fill(0),this.parameterBlock[0]=t,e&&(this.parameterBlock[1]=e.length),this.parameterBlock[2]=1,this.parameterBlock[3]=1,_&&this.parameterBlock.set(_,32),i&&this.parameterBlock.set(i,48);for(let n=0;n<16;n++)l.h[n]=this.BLAKE2B_IV32[n]^this.B2B_GET32(this.parameterBlock,4*n);return e&&(this.blake2bUpdate(l,e),l.c=128),l}blake2bUpdate(t,e){for(let _=0;_<e.length;_++)128===t.c&&(t.t+=t.c,this.blake2bCompress(t,!1),t.c=0),t.b[t.c++]=e[_]}blake2bFinal(t){for(t.t+=t.c;t.c<128;)t.b[t.c++]=0;this.blake2bCompress(t,!0);let e=new Uint8Array(t.outlen);for(let _=0;_<t.outlen;_++)e[_]=t.h[_>>2]>>8*(3&_);return e}blake2bStart(t,e,_,i,l){_=_||64,t=this.normalizeInput(t),i&&(i=this.normalizeInput(i)),l&&(l=this.normalizeInput(l));let n=this.blake2bInit(_,e,i,l);return this.blake2bUpdate(n,t),this.blake2bFinal(n)}blake2bHex(t,e,_,i,l){var n;return n=blake2bStart(t,e,_,i,l),Array.prototype.map.call(n,function(t){return(t<16?"0":"")+t.toString(16)}).join("")}normalizeInput(t){let e;if(t instanceof Uint8Array)e=t;else if("string"==typeof t)e=(new TextEncoder).encode(t);else throw Error("Input must be an string, Buffer or Uint8Array");return e}toHex(t){return Array.prototype.map.call(t,function(t){return(t<16?"0":"")+t.toString(16)}).join("")}static hash(e,_,i=64,l,n){let $=new t,s=$.blake2bStart(e,_,i,l,n);return $.toHex(s)}}let e={},_=null;function i(t){let e=new Uint8Array(4);return e[0]=t,e[1]=t>>8&255,e[2]=t>>16&255,e[3]=t>>24&255,Array.from(e)}function l(t){let e=new Uint8Array(8);return e[0]=t,e[1]=t>>8&255,e[2]=t>>16&255,e[3]=t>>24&255,Array.from(e)}function n(t,e){return 0==e?s(t.slice(0,4)):1==e?s(t.slice(4,8)):void 0}function $(t){var e=[];let _=Math.ceil(Math.floor(Math.log2(new Number(t))+1)/8);for(let i=0;i<_;i++)e.unshift(new Number(255n&t)),t>>=8n;return new Uint8Array(e).reverse()}function s(t){let e=0n;for(let _=t.length-1;_>=0;_--)e=BigInt(t[_])|e<<8n;return e}function h(t){return new Uint8Array(t)}function r(...t){let e=0;t.forEach(t=>{e+=t.length});let _=new Uint8Array(e),i=0;return t.forEach(t=>{_.set(t,i),i+=t.length}),_}function a(t,e){let _=new Uint8Array(1024);for(let i=0;i<t.length;i++)_[i]=t[i]^e[i];return _}function c(t,e){return(t%e+e)%e}function o(t,e,_,i){let l=2n**64n-1n,n=BigInt(4294967295),$=i^(t=t+e+BigInt(2)*(t&n)*(e&n)&BigInt.asUintN(64,l));return t=t+(e=($=e^(_=_+(i=$>>BigInt(32)|($&n)<<BigInt(32))+BigInt(2)*(_&n)*(i&n)&BigInt.asUintN(64,l)))>>BigInt(24)|($&BigInt(16777215))<<BigInt(40))+BigInt(2)*(t&n)*(e&n)&BigInt.asUintN(64,l),e=($=e^(_=_+(i=($=i^t)>>BigInt(16)|($&BigInt(65535))<<BigInt(48))+BigInt(2)*(_&n)*(i&n)&BigInt.asUintN(64,l)))>>BigInt(63)|$<<BigInt(1)&BigInt.asUintN(64,l),[t,e,_,i]}function f(t,e,_,i,l,n,a,c){let f,u=s(t.slice(0,8)),g=s(t.slice(8,16)),v=s(e.slice(0,8)),b=s(e.slice(8,16)),A=s(_.slice(0,8)),p=s(_.slice(8,16)),w=s(i.slice(0,8)),B=s(i.slice(8,16)),k=s(l.slice(0,8)),I=s(l.slice(8,16)),G=s(n.slice(0,8)),m=s(n.slice(8,16)),S=s(a.slice(0,8)),M=s(a.slice(8,16)),F=s(c.slice(0,8)),x=s(c.slice(8,16)),D=o(u,A,k,S);return u=D[0],A=D[1],k=D[2],S=D[3],g=(D=o(g,p,I,M))[0],p=D[1],I=D[2],M=D[3],v=(D=o(v,w,G,F))[0],w=D[1],G=D[2],F=D[3],b=(D=o(b,B,m,x))[0],B=D[1],m=D[2],u=(D=o(u,p,G,x=D[3]))[0],p=D[1],G=D[2],x=D[3],g=(D=o(g,w,m,S))[0],w=D[1],m=D[2],S=D[3],v=(D=o(v,B,k,M))[0],B=D[1],k=D[2],M=D[3],b=(D=o(b,A,I,F))[0],A=D[1],I=D[2],F=D[3],u=$(u),g=$(g),v=$(v),b=$(b),A=$(A),p=$(p),w=$(w),B=$(B),k=$(k),I=$(I),G=$(G),m=$(m),S=$(S),M=$(M),F=$(F),x=$(x),u.length<8&&(f=8-u.length,u=r(u,h(f))),g.length<8&&(f=8-g.length,g=r(g,h(f))),v.length<8&&(f=8-v.length,v=r(v,h(f))),b.length<8&&(f=8-b.length,b=r(b,h(f))),A.length<8&&(f=8-A.length,A=r(A,h(f))),p.length<8&&(f=8-p.length,p=r(p,h(f))),w.length<8&&(f=8-w.length,w=r(w,h(f))),B.length<8&&(f=8-B.length,B=r(B,h(f))),k.length<8&&(f=8-k.length,k=r(k,h(f))),I.length<8&&(f=8-I.length,I=r(I,h(f))),G.length<8&&(f=8-G.length,G=r(G,h(f))),m.length<8&&(f=8-m.length,m=r(m,h(f))),S.length<8&&(f=8-S.length,S=r(S,h(f))),M.length<8&&(f=8-M.length,M=r(M,h(f))),F.length<8&&(f=8-F.length,F=r(F,h(f))),x.length<8&&(f=8-x.length,x=r(x,h(f))),t=r(u,g),e=r(v,b),_=r(A,p),i=r(w,B),l=r(k,I),n=r(G,m),a=r(S,M),c=r(F,x),[t,e,_,i,l,n,a,c]}function u(t,e){return new Promise(_=>{let i=a(t,e),l=[],n,$;for(let s=0;s<8;s++)$=128*s,(n=f(i.slice($,$+16),i.slice($+16,$+32),i.slice($+32,$+48),i.slice($+48,$+64),i.slice($+64,$+80),i.slice($+80,$+96),i.slice($+96,$+112),i.slice($+112,$+128))).forEach(t=>l.push(...t));let h=[];for(let c=0;c<8;c++){$=16*c,(n=f(l.slice($,$+16),l.slice($+128,$+128+16),l.slice($+256,$+256+16),l.slice($+384,$+384+16),l.slice($+512,$+512+16),l.slice($+640,$+640+16),l.slice($+768,$+768+16),l.slice($+896,$+896+16))).forEach(t=>l.push(...t));for(let o=0;o<n.length;o++)h=r(h,n[o])}let u=[];for(let g=0;g<8;g++)$=16*g,u=r(u,h.slice($,$+16),h.slice($+128,$+128+16),h.slice($+256,$+256+16),h.slice($+384,$+384+16),h.slice($+512,$+512+16),h.slice($+640,$+640+16),h.slice($+768,$+768+16),h.slice($+896,$+896+16));_(a(u,i))})}function g(e,i=null){_=new t().blake2bInit(e,i)}function v(l,n,$){let s=`hash${l}_${n}_${$}`;if(e[s])return e[s];let h=function e(l,n){if(l<=64)return g(l),s=r(i(l),n),new t().blake2bUpdate(_,s),new t().blake2bFinal(_);var $=Math.ceil(l/32)-2,s=r(i(l),n);g(64),new t().blake2bUpdate(_,s);var h=new t().blake2bFinal(_),a=h.slice(0,32);for(let c=2;c<=$;c++)g(64),new t().blake2bUpdate(_,h),a=r(a,(h=new t().blake2bFinal(_)).slice(0,32));return g(l-32*$),new t().blake2bUpdate(_,h),r(a,h=new t().blake2bFinal(_))}(1024,r(l,i(n),i($)));return e[s]=h,h}async function b(t,_,i,n,$,s,a){let c=`${t}_${_}_${i}_${n}_${$}_${s}_${a}`;if(e[c])return e[c];let o=r(l(t),l(_),l(i),l(n),l($),l(s),l(a),h(968)),f=await u(h(1024),await u(h(1024),o));return e[c]=f,f}async function A(t,e,_,i,l,$,h,r,o,f,g,A){var p=1==l||2==l&&0==e&&_<=1;if(p)for(var w=[],B=0;w.length<$;){B+=1;let k=await b(e,i,_,f,g,l,B);for(let I=0;I<1024;I+=8)w.push([s(k.slice(I,I+4)),s(k.slice(I+4,I+8))])}for(let G=0;G<$;G++){var m=_*$+G;if(0==e&&m<2){t[i][m]=v(h,m,i);continue}var S=NaN,M=NaN;p?(S=w[G][0],M=w[G][1]):(S=n(t[i][c(m-1,r)],0),M=n(t[i][c(m-1,r)],1));var F=NaN;F=0==e&&0==_?i:c(M,BigInt(o));var x=NaN;x=0==e?0==_||i==F?m-1:0==G?_*$-1:_*$:i==F?r-$+G-1:0==G?r-$-1:r-$;var D=S**BigInt(2)>>BigInt(32);D=BigInt(x)-BigInt(1)-(BigInt(x)*D>>BigInt(32));var U=0;0!=e&&3!=_&&(U=(_+1)*$);var d=await u(t[i][c(m-1,r)],t[F][c(BigInt(U)+D,BigInt(r))]);0!=e&&19==A&&(d=a(t[i][m],d)),t[i][m]=d}return t[i].slice(_*$,(_+1)*$)}onmessage=function(t){let{type:e,payload:_}=t.data;if("fill-segment"===e){let{B:i,t:l,segment:n,i:$,y:s,segment_Length:h,H_0:r,q:a,p:c,mm:o,time_cost:f,v:u}=_;A(i,l,n,$,s,h,r,a,c,o,f,u).then(t=>{postMessage({type:e,payload:_,result:t})})}}}window!=self&&runWorker();