forked from Rabbit-Company/Argon2id-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Argon2id.min.js
12 lines (12 loc) · 14.8 KB
/
Argon2id.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*
Blake2b-JS v1.0.0
https://github.com/Rabbit-Company/Blake2b-JS
License: GPL-3.0
*/
class Blake2b{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,_,$){let e=t[_]+t[$],i=t[_+1]+t[$+1];e>=4294967296&&i++,t[_]=e,t[_+1]=i}ADD64AC(t,_,$,e){let i=t[_]+$;$<0&&(i+=4294967296);let h=t[_+1]+e;i>=4294967296&&h++,t[_]=i,t[_+1]=h}B2B_GET32(t,_){return t[_]^t[_+1]<<8^t[_+2]<<16^t[_+3]<<24}B2B_G(t,_,$,e,i,h){let s=this.m[i],l=this.m[i+1],r=this.m[h],a=this.m[h+1];this.ADD64AA(this.v,t,_),this.ADD64AC(this.v,t,s,l);let n=this.v[e]^this.v[t],A=this.v[e+1]^this.v[t+1];this.v[e]=A,this.v[e+1]=n,this.ADD64AA(this.v,$,e),n=this.v[_]^this.v[$],A=this.v[_+1]^this.v[$+1],this.v[_]=n>>>24^A<<8,this.v[_+1]=A>>>24^n<<8,this.ADD64AA(this.v,t,_),this.ADD64AC(this.v,t,r,a),n=this.v[e]^this.v[t],A=this.v[e+1]^this.v[t+1],this.v[e]=n>>>16^A<<16,this.v[e+1]=A>>>16^n<<16,this.ADD64AA(this.v,$,e),n=this.v[_]^this.v[$],A=this.v[_+1]^this.v[$+1],this.v[_]=A>>>31^n<<1,this.v[_+1]=n>>>31^A<<1}blake2bCompress(t,_){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,_&&(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){if(0===t||t>64)throw Error("Illegal output length, expected 0 < length <= 64");if(_&&_.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(e&&16!==e.length)throw Error("Illegal personal, expected Uint8Array with length is 16");let i={b:new Uint8Array(128),h:new Uint32Array(16),t:0,c:0,outlen:t};this.parameterBlock.fill(0),this.parameterBlock[0]=t,_&&(this.parameterBlock[1]=_.length),this.parameterBlock[2]=1,this.parameterBlock[3]=1,$&&this.parameterBlock.set($,32),e&&this.parameterBlock.set(e,48);for(let h=0;h<16;h++)i.h[h]=this.BLAKE2B_IV32[h]^this.B2B_GET32(this.parameterBlock,4*h);return _&&(this.blake2bUpdate(i,_),i.c=128),i}blake2bUpdate(t,_){for(let $=0;$<_.length;$++)128===t.c&&(t.t+=t.c,this.blake2bCompress(t,!1),t.c=0),t.b[t.c++]=_[$]}blake2bFinal(t){for(t.t+=t.c;t.c<128;)t.b[t.c++]=0;this.blake2bCompress(t,!0);let _=new Uint8Array(t.outlen);for(let $=0;$<t.outlen;$++)_[$]=t.h[$>>2]>>8*(3&$);return _}blake2bStart(t,_,$,e,i){$=$||64,t=this.normalizeInput(t),e&&(e=this.normalizeInput(e)),i&&(i=this.normalizeInput(i));let h=this.blake2bInit($,_,e,i);return this.blake2bUpdate(h,t),this.blake2bFinal(h)}blake2bHex(t,_,$,e,i){let h=blake2bStart(t,_,$,e,i);return toHex(h)}normalizeInput(t){let _;if(t instanceof Uint8Array)_=t;else if("string"==typeof t){let $=new TextEncoder;_=$.encode(t)}else throw Error("Input must be an string, Buffer or Uint8Array");return _}toHex(t){return Array.prototype.map.call(t,function(t){return(t<16?"0":"")+t.toString(16)}).join("")}static hash(t,_,$=64,e,i){let h=new Blake2b,s=h.blake2bStart(t,_,$,e,i);return h.toHex(s)}}
/*
Argon2id-JS v1.1.0
https://github.com/Rabbit-Company/Argon2id-JS
License: GPL-3.0
*/
class Argon2id{static memo={};construtor(){this.mm=NaN,this.H_0=NaN,this.p=NaN,this.q=NaN}LE32(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)}LE64(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)}toHex(t){return Array.prototype.map.call(t,function(t){return(t<16?"0":"")+t.toString(16)}).join("")}ascii(t){for(var e=[],i=0,n=t.length;i<n;i++){var r=Number(t.charCodeAt(i));e.push(r)}return e}extract(t,e){return 0==e?this.Uint8ArrayToBigInt(t.slice(0,4)):1==e?this.Uint8ArrayToBigInt(t.slice(4,8)):void 0}NumberToUint8Array(t){var e=[];let i=Math.ceil(Math.floor(Math.log2(new Number(t))+1)/8);for(let n=0;n<i;n++)e.unshift(new Number(255n&t)),t>>=8n;return new Uint8Array(e).reverse()}Uint8ArrayToBigInt(t){let e=0n;for(let i=t.length-1;i>=0;i--)e=BigInt(t[i])|e<<8n;return e}ZERO(t){return new Uint8Array(t)}Concatenate(...t){let e=0;t.forEach(t=>{e+=t.length});let i=new Uint8Array(e),n=0;return t.forEach(t=>{i.set(t,n),n+=t.length}),i}XOR(t,e){let i=new Uint8Array(1024);for(let n=0;n<t.length;n++)i[n]=t[n]^e[n];return i}MOD(t,e){return(t%e+e)%e}GB(t,e,i,n){let r=2n**64n-1n,s=BigInt(4294967295),a=n^(t=t+e+BigInt(2)*(t&s)*(e&s)&BigInt.asUintN(64,r));return t=t+(e=(a=e^(i=i+(n=a>>BigInt(32)|(a&s)<<BigInt(32))+BigInt(2)*(i&s)*(n&s)&BigInt.asUintN(64,r)))>>BigInt(24)|(a&BigInt(16777215))<<BigInt(40))+BigInt(2)*(t&s)*(e&s)&BigInt.asUintN(64,r),e=(a=e^(i=i+(n=(a=n^t)>>BigInt(16)|(a&BigInt(65535))<<BigInt(48))+BigInt(2)*(i&s)*(n&s)&BigInt.asUintN(64,r)))>>BigInt(63)|a<<BigInt(1)&BigInt.asUintN(64,r),[t,e,i,n]}P(t,e,i,n,r,s,a,h){let l,o=this.Uint8ArrayToBigInt(t.slice(0,8)),c=this.Uint8ArrayToBigInt(t.slice(8,16)),g=this.Uint8ArrayToBigInt(e.slice(0,8)),$=this.Uint8ArrayToBigInt(e.slice(8,16)),_=this.Uint8ArrayToBigInt(i.slice(0,8)),u=this.Uint8ArrayToBigInt(i.slice(8,16)),f=this.Uint8ArrayToBigInt(n.slice(0,8)),A=this.Uint8ArrayToBigInt(n.slice(8,16)),m=this.Uint8ArrayToBigInt(r.slice(0,8)),b=this.Uint8ArrayToBigInt(r.slice(8,16)),E=this.Uint8ArrayToBigInt(s.slice(0,8)),y=this.Uint8ArrayToBigInt(s.slice(8,16)),U=this.Uint8ArrayToBigInt(a.slice(0,8)),d=this.Uint8ArrayToBigInt(a.slice(8,16)),p=this.Uint8ArrayToBigInt(h.slice(0,8)),T=this.Uint8ArrayToBigInt(h.slice(8,16)),w=this.GB(o,_,m,U);return o=w[0],_=w[1],m=w[2],U=w[3],c=(w=this.GB(c,u,b,d))[0],u=w[1],b=w[2],d=w[3],g=(w=this.GB(g,f,E,p))[0],f=w[1],E=w[2],p=w[3],$=(w=this.GB($,A,y,T))[0],A=w[1],y=w[2],T=w[3],o=(w=this.GB(o,u,E,T))[0],u=w[1],E=w[2],T=w[3],c=(w=this.GB(c,f,y,U))[0],f=w[1],y=w[2],U=w[3],g=(w=this.GB(g,A,m,d))[0],A=w[1],m=w[2],d=w[3],$=(w=this.GB($,_,b,p))[0],_=w[1],b=w[2],p=w[3],o=this.NumberToUint8Array(o),c=this.NumberToUint8Array(c),g=this.NumberToUint8Array(g),$=this.NumberToUint8Array($),_=this.NumberToUint8Array(_),u=this.NumberToUint8Array(u),f=this.NumberToUint8Array(f),A=this.NumberToUint8Array(A),m=this.NumberToUint8Array(m),b=this.NumberToUint8Array(b),E=this.NumberToUint8Array(E),y=this.NumberToUint8Array(y),U=this.NumberToUint8Array(U),d=this.NumberToUint8Array(d),p=this.NumberToUint8Array(p),T=this.NumberToUint8Array(T),o.length<8&&(l=8-o.length,o=this.Concatenate(o,this.ZERO(l))),c.length<8&&(l=8-c.length,c=this.Concatenate(c,this.ZERO(l))),g.length<8&&(l=8-g.length,g=this.Concatenate(g,this.ZERO(l))),$.length<8&&(l=8-$.length,$=this.Concatenate($,this.ZERO(l))),_.length<8&&(l=8-_.length,_=this.Concatenate(_,this.ZERO(l))),u.length<8&&(l=8-u.length,u=this.Concatenate(u,this.ZERO(l))),f.length<8&&(l=8-f.length,f=this.Concatenate(f,this.ZERO(l))),A.length<8&&(l=8-A.length,A=this.Concatenate(A,this.ZERO(l))),m.length<8&&(l=8-m.length,m=this.Concatenate(m,this.ZERO(l))),b.length<8&&(l=8-b.length,b=this.Concatenate(b,this.ZERO(l))),E.length<8&&(l=8-E.length,E=this.Concatenate(E,this.ZERO(l))),y.length<8&&(l=8-y.length,y=this.Concatenate(y,this.ZERO(l))),U.length<8&&(l=8-U.length,U=this.Concatenate(U,this.ZERO(l))),d.length<8&&(l=8-d.length,d=this.Concatenate(d,this.ZERO(l))),p.length<8&&(l=8-p.length,p=this.Concatenate(p,this.ZERO(l))),T.length<8&&(l=8-T.length,T=this.Concatenate(T,this.ZERO(l))),t=this.Concatenate(o,c),e=this.Concatenate(g,$),i=this.Concatenate(_,u),n=this.Concatenate(f,A),r=this.Concatenate(m,b),s=this.Concatenate(E,y),[t,e,i,n,r,s,a=this.Concatenate(U,d),h=this.Concatenate(p,T)]}G(t,e){return new Promise(i=>{let n=this.XOR(t,e),r=[],s,a;for(let h=0;h<8;h++)a=128*h,(s=this.P(n.slice(a,a+16),n.slice(a+16,a+32),n.slice(a+32,a+48),n.slice(a+48,a+64),n.slice(a+64,a+80),n.slice(a+80,a+96),n.slice(a+96,a+112),n.slice(a+112,a+128))).forEach(t=>r.push(...t));let l=[];for(let o=0;o<8;o++){a=16*o,(s=this.P(r.slice(a,a+16),r.slice(a+128,a+128+16),r.slice(a+256,a+256+16),r.slice(a+384,a+384+16),r.slice(a+512,a+512+16),r.slice(a+640,a+640+16),r.slice(a+768,a+768+16),r.slice(a+896,a+896+16))).forEach(t=>r.push(...t));for(let c=0;c<s.length;c++)l=this.Concatenate(l,s[c])}let g=[];for(let $=0;$<8;$++)a=16*$,g=this.Concatenate(g,l.slice(a,a+16),l.slice(a+128,a+128+16),l.slice(a+256,a+256+16),l.slice(a+384,a+384+16),l.slice(a+512,a+512+16),l.slice(a+640,a+640+16),l.slice(a+768,a+768+16),l.slice(a+896,a+896+16));i(this.XOR(g,n))})}blake2bSetup(t,e=null){this.context=new Blake2b().blake2bInit(t,e)}async Argon2Operation(t,e,i,n,r,s,a,h,l="",o=""){let c=this.EstablishH_0(t,e,i,n,r,s,a,h,l,o);this.AllocateMemory(t,i);let g=this.mm/t;var $=g/4;let _=Array.from(Array(Number(t)),()=>[]);if(window.Worker&&window.runWorker){let u=[];await new Promise(e=>{let i=0,a=0,h=0,l=()=>{u.forEach((e,h)=>{e.postMessage({type:"fill-segment",payload:{B:_,t:i,segment:a,i:Number(h),y:s,segment_Length:$,H_0:c,q:g,p:Number(t),mm:this.mm,time_cost:n,v:r}})})},o=()=>{u.forEach(t=>t.terminate())};for(let f=0;f<t;f++)u[f]=new Worker(URL.createObjectURL(new Blob(["("+runWorker.toString()+")()"],{type:"text/javascript"}))),u[f].onmessage=function(r){let{i:s}=r.data.payload;h++;for(let c=0;c<$;c++)_[s][a*$+c]=r.data.result[c];if(h>=t){if(h=0,++a>=4&&(a=0,++i>=n))return o(),e();l()}};l()})}else for(let f=0;f<n;f++)for(let A=0;A<4;A++){var m=await Promise.all(Array.from(Array(Number(t)),(e,i)=>this._fill_segment(_,f,A,i,s,$,c,g,t,this.mm,n,r)));for(let b=0;b<t;b++){var E=m[b];for(let y=0;y<$;y++)_[b][A*$+y]=E[y]}}let U=this.ZERO(1024);for(let d=0;d<t;d++)U=this.XOR(U,_[d][g-1]);return this.variableLengthHashFunction(e,U)}EstablishH_0(t,e,i,n,r,s,a,h,l="",o=""){var c=this.Concatenate(this.LE32(t),this.LE32(e),this.LE32(i),this.LE32(n),this.LE32(r),this.LE32(s),this.LE32(a.length),this.ascii(a),this.LE32(h.length),this.ascii(h),this.LE32(l.length),this.ascii(l),this.LE32(o.length),this.ascii(o));return this.blake2bSetup(64),new Blake2b().blake2bUpdate(this.context,c),new Blake2b().blake2bFinal(this.context)}AllocateMemory(t,e){this.mm=4*t*Math.floor(e/(4*t))}getHash(t,e,i){let n="hash"+t+"_"+e+"_"+i;if(Argon2id.memo[n])return Argon2id.memo[n];let r=this.variableLengthHashFunction(1024,this.Concatenate(t,this.LE32(e),this.LE32(i)));return Argon2id.memo[n]=r,r}variableLengthHashFunction(t,e){if(t<=64)return this.blake2bSetup(t),n=this.Concatenate(this.LE32(t),e),new Blake2b().blake2bUpdate(this.context,n),new Blake2b().blake2bFinal(this.context);var i=Math.ceil(t/32)-2,n=this.Concatenate(this.LE32(t),e);this.blake2bSetup(64),new Blake2b().blake2bUpdate(this.context,n);var r=new Blake2b().blake2bFinal(this.context),s=r.slice(0,32);for(let a=2;a<=i;a++)this.blake2bSetup(64),new Blake2b().blake2bUpdate(this.context,r),r=new Blake2b().blake2bFinal(this.context),s=this.Concatenate(s,r.slice(0,32));return this.blake2bSetup(t-32*i),new Blake2b().blake2bUpdate(this.context,r),r=new Blake2b().blake2bFinal(this.context),this.Concatenate(s,r)}async getAddressBlock(t,e,i,n,r,s,a){let h=`${t}_${e}_${i}_${n}_${r}_${s}_${a}`;if(Argon2id.memo[h])return Argon2id.memo[h];let l=this.Concatenate(this.LE64(t),this.LE64(e),this.LE64(i),this.LE64(n),this.LE64(r),this.LE64(s),this.LE64(a),this.ZERO(968)),o=await this.G(this.ZERO(1024),await this.G(this.ZERO(1024),l));return Argon2id.memo[h]=o,o}async _fill_segment(t,e,i,n,r,s,a,h,l,o,c,g){var $=1==r||2==r&&0==e&&i<=1;if($)for(var _=[],u=0;_.length<s;){u+=1;let f=await this.getAddressBlock(e,n,i,o,c,r,u);for(let A=0;A<1024;A+=8)_.push([this.Uint8ArrayToBigInt(f.slice(A,A+4)),this.Uint8ArrayToBigInt(f.slice(A+4,A+8))])}for(let m=0;m<s;m++){var b=i*s+m;if(0==e&&b<2){t[n][b]=this.getHash(a,b,n);continue}var E=NaN,y=NaN;$?(E=_[m][0],y=_[m][1]):(E=this.extract(t[n][this.MOD(b-1,h)],0),y=this.extract(t[n][this.MOD(b-1,h)],1));var U=NaN;U=0==e&&0==i?n:this.MOD(y,BigInt(l));var d=NaN;d=0==e?0==i||n==U?b-1:0==m?i*s-1:i*s:n==U?h-s+m-1:0==m?h-s-1:h-s;var p=E**BigInt(2)>>BigInt(32);p=BigInt(d)-BigInt(1)-(BigInt(d)*p>>BigInt(32));var T=0;0!=e&&3!=i&&(T=(i+1)*s);var w=await this.G(t[n][this.MOD(b-1,h)],t[U][this.MOD(BigInt(T)+p,BigInt(h))]);0!=e&&19==g&&(w=this.XOR(t[n][b],w)),t[n][b]=w}return t[n].slice(i*s,(i+1)*s)}hexToBase64(t){return btoa(t.match(/\w{2}/g).map(function(t){return String.fromCharCode(parseInt(t,16))}).join(""))}base64ToHex(t){let e=atob(t),i="";for(let n=0;n<e.length;n++){let r=e.charCodeAt(n).toString(16);i+=2===r.length?r:"0"+r}return i.toUpperCase()}randRange(t,e){var i=e-t,n=Math.ceil(Math.log2(i)/8);if(!n)return t;for(var r=Math.pow(256,n),s=new Uint8Array(n);;){window.crypto.getRandomValues(s);for(var a=0,h=0;h<n;h++)a=(a<<8)+s[h];if(a<r-r%i)return t+a%i}}static randomSalt(){let t=new Argon2id,e="abcdefghijklmnopqrstuvwxyz",i="ABCDEFGHIJKLMNOPQRSTUVWXYZ",n="1234567890",r="";for(let s=0;s<16;s++)r+=e.charAt(t.randRange(0,e.length));r=r.split("");for(let a=0;a<8;a++)r[t.randRange(0,r.length)]=i.charAt(t.randRange(0,i.length));for(let h=0;h<8;h++)r[t.randRange(0,r.length)]=n.charAt(t.randRange(0,n.length));return r.join("")}static async hash(t,e=Argon2id.randomSalt(),i=2,n=32,r=3,s=32,a="",h=""){let l=new Argon2id,o=await l.Argon2Operation(r,s,n,i,19,2,t,e,a,h);return l.toHex(o)}static hashDecode(t){let e=new Argon2id,i=t.split("$")[5];return e.base64ToHex(i).toLowerCase()}static hashEncoded(t,e=Argon2id.randomSalt(),i=2,n=32,r=3,s=32,a="",h=""){return new Promise(l=>{let o=new Argon2id;o.Argon2Operation(r,s,n,i,19,2,t,e,a,h).then(t=>{l("$argon2id$v=19$m="+n+",t="+i+",p="+r+"$"+btoa(e).replaceAll("=","")+"$"+o.hexToBase64(o.toHex(t)).replaceAll("=",""))})})}static verify(t,e,i="",n=""){let r=t.split("$");if(6!=r.length||"argon2id"!=r[1]||"v=19"!=r[2])return!1;let s=r[3].split(",");if(3!=s.length)return!1;let a=s[0].split("=")[1],h=s[1].split("=")[1],l=s[2].split("=")[1],o=atob(r[4]),c=Argon2id.hashDecode(t);return new Promise(t=>{Argon2id.hash(e,o,h,a,l,c.length/2,i,n).then(e=>{t(c==e)})})}}