Home

js-spectre @60d4281bc8a6959c282aad6b27a847d6f07834a6 - refs - log -
-
https://git.jolheiser.com/js-spectre.git
JS implementation for spectre/masterpassword
js-spectre / spectre.js
- raw
1
2
3
4
5
6
"use strict";var spectre=(()=>{var nt=Object.defineProperty;var At=Object.getOwnPropertyDescriptor;var kt=Object.getOwnPropertyNames;var Lt=Object.prototype.hasOwnProperty;var It=(e,t)=>{for(var n in t)nt(e,n,{get:t[n],enumerable:!0})},Et=(e,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of kt(t))!Lt.call(e,o)&&o!==n&&nt(e,o,{get:()=>t[o],enumerable:!(s=At(t,o))||s.enumerable});return e};var Bt=e=>Et(nt({},"__esModule",{value:!0}),e);var Dt={};It(Dt,{Spectre:()=>ut});function v(e){if(!Number.isSafeInteger(e)||e<0)throw new Error("positive integer expected, got "+e)}function Ut(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function q(e,...t){if(!Ut(e))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(e.length))throw new Error("Uint8Array expected of length "+t+", got length="+e.length)}function Z(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash should be wrapped by utils.wrapConstructor");v(e.outputLen),v(e.blockLen)}function j(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function lt(e,t){q(e);let n=t.outputLen;if(e.length<n)throw new Error("digestInto() expects output buffer of length at least "+n)}function $(e){return new Uint32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4))}function W(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function w(e,t){return e<<32-t|e>>>t}function u(e,t){return e<<t|e>>>32-t>>>0}var ot=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function Ht(e){return e<<24&4278190080|e<<8&16711680|e>>>8&65280|e>>>24&255}function st(e){for(let t=0;t<e.length;t++)e[t]=Ht(e[t])}function Tt(e){if(typeof e!="string")throw new Error("utf8ToBytes expected string, got "+typeof e);return new Uint8Array(new TextEncoder().encode(e))}function C(e){return typeof e=="string"&&(e=Tt(e)),q(e),e}var G=class{clone(){return this._cloneInto()}};function tt(e,t){if(t!==void 0&&{}.toString.call(t)!=="[object Object]")throw new Error("Options should be object or undefined");return Object.assign(e,t)}function dt(e){let t=s=>e().update(C(s)).digest(),n=e();return t.outputLen=n.outputLen,t.blockLen=n.blockLen,t.create=()=>e(),t}var X=class extends G{constructor(t,n){super(),this.finished=!1,this.destroyed=!1,Z(t);let s=C(n);if(this.iHash=t.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;let o=this.blockLen,c=new Uint8Array(o);c.set(s.length>o?t.create().update(s).digest():s);for(let r=0;r<c.length;r++)c[r]^=54;this.iHash.update(c),this.oHash=t.create();for(let r=0;r<c.length;r++)c[r]^=106;this.oHash.update(c),c.fill(0)}update(t){return j(this),this.iHash.update(t),this}digestInto(t){j(this),q(t,this.outputLen),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){let t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));let{oHash:n,iHash:s,finished:o,destroyed:c,blockLen:r,outputLen:i}=this;return t=t,t.finished=o,t.destroyed=c,t.blockLen=r,t.outputLen=i,t.oHash=n._cloneInto(t.oHash),t.iHash=s._cloneInto(t.iHash),t}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},ct=(e,t,n)=>new X(e,t).update(n).digest();ct.create=(e,t)=>new X(e,t);function Ot(e,t,n,s){Z(e);let o=tt({dkLen:32,asyncTick:10},s),{c,dkLen:r,asyncTick:i}=o;if(v(c),v(r),v(i),c<1)throw new Error("PBKDF2: iterations (c) should be >= 1");let h=C(t),f=C(n),x=new Uint8Array(r),a=ct.create(e,h),l=a._cloneInto().update(f);return{c,dkLen:r,asyncTick:i,DK:x,PRF:a,PRFSalt:l}}function St(e,t,n,s,o){return e.destroy(),t.destroy(),s&&s.destroy(),o.fill(0),n}function rt(e,t,n,s){let{c:o,dkLen:c,DK:r,PRF:i,PRFSalt:h}=Ot(e,t,n,s),f,x=new Uint8Array(4),a=W(x),l=new Uint8Array(i.outputLen);for(let y=1,p=0;p<c;y++,p+=i.outputLen){let d=r.subarray(p,p+i.outputLen);a.setInt32(0,y,!1),(f=h._cloneInto(f)).update(x).digestInto(l),d.set(l.subarray(0,d.length));for(let m=1;m<o;m++){i._cloneInto(f).update(l).digestInto(l);for(let b=0;b<d.length;b++)d[b]^=l[b]}}return St(i,h,r,f,l)}function Mt(e,t,n,s){if(typeof e.setBigUint64=="function")return e.setBigUint64(t,n,s);let o=BigInt(32),c=BigInt(4294967295),r=Number(n>>o&c),i=Number(n&c),h=s?4:0,f=s?0:4;e.setUint32(t+h,r,s),e.setUint32(t+f,i,s)}function pt(e,t,n){return e&t^~e&n}function yt(e,t,n){return e&t^e&n^t&n}var et=class extends G{constructor(t,n,s,o){super(),this.blockLen=t,this.outputLen=n,this.padOffset=s,this.isLE=o,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(t),this.view=W(this.buffer)}update(t){j(this);let{view:n,buffer:s,blockLen:o}=this;t=C(t);let c=t.length;for(let r=0;r<c;){let i=Math.min(o-this.pos,c-r);if(i===o){let h=W(t);for(;o<=c-r;r+=o)this.process(h,r);continue}s.set(t.subarray(r,r+i),this.pos),this.pos+=i,r+=i,this.pos===o&&(this.process(n,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){j(this),lt(t,this),this.finished=!0;let{buffer:n,view:s,blockLen:o,isLE:c}=this,{pos:r}=this;n[r++]=128,this.buffer.subarray(r).fill(0),this.padOffset>o-r&&(this.process(s,0),r=0);for(let a=r;a<o;a++)n[a]=0;Mt(s,o-8,BigInt(this.length*8),c),this.process(s,0);let i=W(t),h=this.outputLen;if(h%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let f=h/4,x=this.get();if(f>x.length)throw new Error("_sha2: outputLen bigger than state");for(let a=0;a<f;a++)i.setUint32(4*a,x[a],c)}digest(){let{buffer:t,outputLen:n}=this;this.digestInto(t);let s=t.slice(0,n);return this.destroy(),s}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());let{blockLen:n,buffer:s,length:o,finished:c,destroyed:r,pos:i}=this;return t.length=o,t.pos=i,t.finished=c,t.destroyed=r,o%n&&t.buffer.set(s),t}};var _t=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),A=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),k=new Uint32Array(64),it=class extends et{constructor(){super(64,32,8,!1),this.A=A[0]|0,this.B=A[1]|0,this.C=A[2]|0,this.D=A[3]|0,this.E=A[4]|0,this.F=A[5]|0,this.G=A[6]|0,this.H=A[7]|0}get(){let{A:t,B:n,C:s,D:o,E:c,F:r,G:i,H:h}=this;return[t,n,s,o,c,r,i,h]}set(t,n,s,o,c,r,i,h){this.A=t|0,this.B=n|0,this.C=s|0,this.D=o|0,this.E=c|0,this.F=r|0,this.G=i|0,this.H=h|0}process(t,n){for(let a=0;a<16;a++,n+=4)k[a]=t.getUint32(n,!1);for(let a=16;a<64;a++){let l=k[a-15],y=k[a-2],p=w(l,7)^w(l,18)^l>>>3,d=w(y,17)^w(y,19)^y>>>10;k[a]=d+k[a-7]+p+k[a-16]|0}let{A:s,B:o,C:c,D:r,E:i,F:h,G:f,H:x}=this;for(let a=0;a<64;a++){let l=w(i,6)^w(i,11)^w(i,25),y=x+l+pt(i,h,f)+_t[a]+k[a]|0,d=(w(s,2)^w(s,13)^w(s,22))+yt(s,o,c)|0;x=f,f=h,h=i,i=r+y|0,r=c,c=o,o=s,s=y+d|0}s=s+this.A|0,o=o+this.B|0,c=c+this.C|0,r=r+this.D|0,i=i+this.E|0,h=h+this.F|0,f=f+this.G|0,x=x+this.H|0,this.set(s,o,c,r,i,h,f,x)}roundClean(){k.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}};var J=dt(()=>new it);function bt(e,t,n,s,o,c){let r=e[t++]^n[s++],i=e[t++]^n[s++],h=e[t++]^n[s++],f=e[t++]^n[s++],x=e[t++]^n[s++],a=e[t++]^n[s++],l=e[t++]^n[s++],y=e[t++]^n[s++],p=e[t++]^n[s++],d=e[t++]^n[s++],m=e[t++]^n[s++],b=e[t++]^n[s++],Y=e[t++]^n[s++],K=e[t++]^n[s++],Q=e[t++]^n[s++],L=e[t++]^n[s++],I=r,E=i,B=h,U=f,H=x,T=a,N=l,O=y,S=p,M=d,_=m,F=b,V=Y,D=K,P=Q,R=L;for(let xt=0;xt<8;xt+=2)H^=u(I+V|0,7),S^=u(H+I|0,9),V^=u(S+H|0,13),I^=u(V+S|0,18),M^=u(T+E|0,7),D^=u(M+T|0,9),E^=u(D+M|0,13),T^=u(E+D|0,18),P^=u(_+N|0,7),B^=u(P+_|0,9),N^=u(B+P|0,13),_^=u(N+B|0,18),U^=u(R+F|0,7),O^=u(U+R|0,9),F^=u(O+U|0,13),R^=u(F+O|0,18),E^=u(I+U|0,7),B^=u(E+I|0,9),U^=u(B+E|0,13),I^=u(U+B|0,18),N^=u(T+H|0,7),O^=u(N+T|0,9),H^=u(O+N|0,13),T^=u(H+O|0,18),F^=u(_+M|0,7),S^=u(F+_|0,9),M^=u(S+F|0,13),_^=u(M+S|0,18),V^=u(R+P|0,7),D^=u(V+R|0,9),P^=u(D+V|0,13),R^=u(P+D|0,18);o[c++]=r+I|0,o[c++]=i+E|0,o[c++]=h+B|0,o[c++]=f+U|0,o[c++]=x+H|0,o[c++]=a+T|0,o[c++]=l+N|0,o[c++]=y+O|0,o[c++]=p+S|0,o[c++]=d+M|0,o[c++]=m+_|0,o[c++]=b+F|0,o[c++]=Y+V|0,o[c++]=K+D|0,o[c++]=Q+P|0,o[c++]=L+R|0}function at(e,t,n,s,o){let c=s+0,r=s+16*o;for(let i=0;i<16;i++)n[r+i]=e[t+(2*o-1)*16+i];for(let i=0;i<o;i++,c+=16,t+=16)bt(n,r,e,t,n,c),i>0&&(r+=16),bt(n,c,e,t+=16,n,r)}function Ft(e,t,n){let s=tt({dkLen:32,asyncTick:10,maxmem:1073742848},n),{N:o,r:c,p:r,dkLen:i,asyncTick:h,maxmem:f,onProgress:x}=s;if(v(o),v(c),v(r),v(i),v(h),v(f),x!==void 0&&typeof x!="function")throw new Error("progressCb should be function");let a=128*c,l=a/4;if(o<=1||o&o-1||o>2**32)throw new Error("Scrypt: N must be larger than 1, a power of 2, and less than 2^32");if(r<0||r>(2**32-1)*32/a)throw new Error("Scrypt: p must be a positive integer less than or equal to ((2^32 - 1) * 32) / (128 * r)");if(i<0||i>(2**32-1)*32)throw new Error("Scrypt: dkLen should be positive integer less than or equal to (2^32 - 1) * 32");if(a*(o+r)>f)throw new Error("Scrypt: memused is bigger than maxMem. Expected 128 * r * (N + p) > maxmem of "+f);let p=rt(J,e,t,{c:1,dkLen:a*r}),d=$(p),m=$(new Uint8Array(a*o)),b=$(new Uint8Array(a)),Y=()=>{};if(x){let K=2*o*r,Q=Math.max(Math.floor(K/1e4),1),L=0;Y=()=>{L++,x&&(!(L%Q)||L===K)&&x(L/K)}}return{N:o,r:c,p:r,dkLen:i,blockSize32:l,V:m,B32:d,B:p,tmp:b,blockMixCb:Y,asyncTick:h}}function Vt(e,t,n,s,o){let c=rt(J,e,n,{c:1,dkLen:t});return n.fill(0),s.fill(0),o.fill(0),c}function vt(e,t,n){let{N:s,r:o,p:c,dkLen:r,blockSize32:i,V:h,B32:f,B:x,tmp:a,blockMixCb:l}=Ft(e,t,n);ot||st(f);for(let y=0;y<c;y++){let p=i*y;for(let d=0;d<i;d++)h[d]=f[p+d];for(let d=0,m=0;d<s-1;d++)at(h,m,h,m+=i,o),l();at(h,(s-1)*i,f,p,o),l();for(let d=0;d<s;d++){let m=f[p+i-16]%s;for(let b=0;b<i;b++)a[b]=f[p+b]^h[m*i+b];at(a,0,f,p,o),l()}}return ot||st(f),Vt(e,r,x,h,a)}var z;(function(e){e.MAXIMUM="Maximum",e.LONG="Long",e.MEDIUM="Medium",e.SHORT="Short",e.PIN="PIN",e.NAME="Name",e.PHRASE="Phrase",e.BASIC="Basic"})(z||(z={}));var mt={Maximum:["anoxxxxxxxxxxxxxxxxx","axxxxxxxxxxxxxxxxxno"],Long:["CvcvnoCvcvCvcv","CvcvCvcvnoCvcv","CvcvCvcvCvcvno","CvccnoCvcvCvcv","CvccCvcvnoCvcv","CvccCvcvCvcvno","CvcvnoCvccCvcv","CvcvCvccnoCvcv","CvcvCvccCvcvno","CvcvnoCvcvCvcc","CvcvCvcvnoCvcc","CvcvCvcvCvccno","CvccnoCvccCvcv","CvccCvccnoCvcv","CvccCvccCvcvno","CvcvnoCvccCvcc","CvcvCvccnoCvcc","CvcvCvccCvccno","CvccnoCvcvCvcc","CvccCvcvnoCvcc","CvccCvcvCvccno"],Medium:["CvcnoCvc","CvcCvcno"],Short:["Cvcn"],PIN:["nnnn"],Name:["cvccvcvcv"],Phrase:["cvcc cvc cvccvcv cvc","cvc cvccvcvcv cvcv","cv cvccv cvc cvcvccv"],Basic:["aaanaaan","aannaaan","aaannaaa"]},wt={V:"AEIOU",C:"BCDFGHJKLMNPQRSTVWXYZ",v:"aeiou",c:"bcdfghjklmnpqrstvwxyz",A:"AEIOUBCDFGHJKLMNPQRSTVWXYZ",a:"AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz",n:"0123456789",o:"@&%?,=[]_:-+*$#!'^~;()/.",x:"AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz0123456789!@#$%^&*()"," ":" "};var g;(function(e){e.AUTHENTICATION="Authentication",e.IDENTIFICATION="Identification",e.RECOVERY="Recovery"})(g||(g={}));function gt(e){switch(e){case g.AUTHENTICATION:return z.LONG;case g.IDENTIFICATION:return z.NAME;case g.RECOVERY:return z.MAXIMUM}}var ft=class{key;constructor(t){this.key=t}scope(t){switch(t){case g.AUTHENTICATION:return this.key;case g.IDENTIFICATION:return`${this.key}.login`;case g.RECOVERY:return`${this.key}.answer`}}},Ct=new ft("com.lyndir.masterpassword");var ut=class{name;secret;scoper;key;constructor(t,n,s=Ct){this.name=t,this.secret=n,this.scoper=s,this.key=this.userKey()}userKey(){let t=new TextEncoder,n=t.encode(this.name),s=t.encode(this.secret),o=t.encode(this.scoper.scope(g.AUTHENTICATION)),c=n.length,r=new Uint8Array([...o,...ht(c),...n]),i=32768,h=8,f=2,x=128*h*(i+f+2);return vt(s,r,{N:i,r:h,p:f,maxmem:x,dkLen:64})}siteKey(t,n,s){let o=new TextEncoder,c=o.encode(t),r=o.encode(this.scoper.scope(s)),i=c.length,h=new Uint8Array([...r,...ht(i),...c,...ht(n)]),f=new X(J,this.key);return f.update(h),f.digest()}site(t,n,s=1,o=g.AUTHENTICATION){n===void 0&&(n=gt(o));let c=this.siteKey(t,s,o),r=mt[n],i=r[c[0]%r.length],h="";for(let f=0;f<i.length;f++){let x=i[f],a=wt[x],l=a[c[f+1]%a.length];h+=l}return h}};function ht(e){let t=new ArrayBuffer(4);return new DataView(t).setInt32(0,e,!1),new Uint8Array(t)}return Bt(Dt);})();
/*! Bundled license information:

@noble/hashes/esm/utils.js:
  (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
*/