Home

js-spectre @7230b4554e2d62f217e12504610499b7f47e3179 - refs - log -
-
https://git.jolheiser.com/js-spectre.git
JS implementation for spectre/masterpassword
js-spectre / lib / spectre.min.js
- raw -
1
"use strict";var spectre=(()=>{var et=Object.defineProperty;var St=Object.getOwnPropertyDescriptor;var Bt=Object.getOwnPropertyNames;var Tt=Object.prototype.hasOwnProperty;var Ut=(e,t)=>{for(var n in t)et(e,n,{get:t[n],enumerable:!0})},It=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of Bt(t))!Tt.call(e,c)&&c!==n&&et(e,c,{get:()=>t[c],enumerable:!(o=St(t,c))||o.enumerable});return e};var Ot=e=>It(et({},"__esModule",{value:!0}),e);var Vt={};Ut(Vt,{Scope:()=>rt,SimpleScoper:()=>V,Spectre:()=>pt,Template:()=>nt,characters:()=>ct,defaultScoper:()=>at,defaultTemplate:()=>st,templates:()=>ot});var nt=(i=>(i.MAXIMUM="Maximum",i.LONG="Long",i.MEDIUM="Medium",i.SHORT="Short",i.PIN="PIN",i.NAME="Name",i.PHRASE="Phrase",i.BASIC="Basic",i))(nt||{}),ot={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"]},ct={V:"AEIOU",C:"BCDFGHJKLMNPQRSTVWXYZ",v:"aeiou",c:"bcdfghjklmnpqrstvwxyz",A:"AEIOUBCDFGHJKLMNPQRSTVWXYZ",a:"AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz",n:"0123456789",o:"@&%?,=[]_:-+*$#!'^~;()/.",x:"AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz0123456789!@#$%^&*()"," ":" "};var rt=(o=>(o.AUTHENTICATION="Authentication",o.IDENTIFICATION="Identification",o.RECOVERY="Recovery",o))(rt||{});function st(e){switch(e){case"Authentication":return"Long";case"Identification":return"Name";case"Recovery":return"Maximum"}}var V=class{key;constructor(t){this.key=t}scope(t){switch(t){case"Authentication":return this.key;case"Identification":return`${this.key}.login`;case"Recovery":return`${this.key}.answer`}}},at=new V("com.lyndir.masterpassword");function mt(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"&&"BYTES_PER_ELEMENT"in e&&e.BYTES_PER_ELEMENT===1}var it=e=>e?`"${e}" `:"";function y(e,t=""){if(typeof e!="number")throw new TypeError(it(t)+"expected number, got "+typeof e);if(!Number.isSafeInteger(e)||e<0)throw new RangeError(it(t)+"expected integer >= 0, got "+e);return e}function j(e,t,n=""){if(mt(e)&&(t===void 0||e.length===t))return e;t!==void 0&&y(t,"length");let o=mt(e),c=t!==void 0?` of length ${t}`:"",r=o?`length=${e.length}`:`type=${typeof e}`,a=it(n)+"expected Uint8Array"+c+", got "+r;throw o?new RangeError(a):new TypeError(a)}function z(e){if(typeof e!="function"||typeof e.create!="function")throw new TypeError("expected hash wrapped by utils.createHasher");if(y(e.outputLen),y(e.blockLen),e.outputLen<1||e.blockLen<1)throw new Error("hash blockLen / outputLen must be >= 1")}var Nt=(e,t)=>{if(e===null||typeof e!="object"||Array.isArray(e))throw new TypeError((t==="object"?"":`"${t}" `)+"expected object, got type="+typeof e)},wt=(e,t)=>{Nt(e,t);let n=Object.getPrototypeOf(e);if(n!==Object.prototype&&n!==null)throw new TypeError(`"${t}" expected plain object`);if(Object.hasOwn(e,"__proto__"))throw new TypeError(`"${t}.__proto__" is not allowed`)};function G(e,t=!0){if(e.destroyed)throw new Error("hash was destroyed");if(t&&e.finished)throw new Error("digest() was already called")}function J(e,t){j(e,void 0,"output");let n=t.outputLen;if(!(e.length>=n))throw new RangeError('"output" expected length >= '+n)}function Q(e){return new Uint32Array(e.buffer,e.byteOffset,Math.floor(e.byteLength/4))}function m(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}function P(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function v(e,t){return e<<32-t|e>>>t}function d(e,t){return e<<t|e>>>32-t>>>0}var _t=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function Mt(e){return e<<24&4278190080|e<<8&16711680|e>>>8&65280|e>>>24&255}function Dt(e){for(let t=0;t<e.length;t++)e[t]=Mt(e[t]);return e}var ft=_t?e=>e:Dt;function Ft(e){if(typeof e!="string")throw new TypeError("string expected");let t=new TextEncoder().encode(e);try{return new Uint8Array(t)}finally{m(t)}}function xt(e,t=""){return typeof e=="string"?Ft(e):j(e,void 0,t)}function X(e,t,n="opts"){return wt(e,"defaults"),t!==void 0&&wt(t,n),Object.assign(Object.create(null),e,t)}function gt(e,t={}){if(typeof e!="function")throw new TypeError('"hashCons" expected function, got type='+typeof e);t=X({},t,"info");let n=(c,r)=>e(r).update(c).digest(),o=e(void 0);return n.outputLen=o.outputLen,n.blockLen=o.blockLen,n.canXOF=o.canXOF,n.create=c=>e(c),Object.assign(n,t),Object.freeze(n)}var vt=e=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,e])});var Z=class{oHash;iHash;blockLen;outputLen;canXOF=!1;finished=!1;destroyed=!1;constructor(t,n){if(z(t),j(n,void 0,"key"),this.iHash=t.create(),typeof this.iHash.update!="function")throw new Error("expected Hash instance");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;let o=this.blockLen,c=new Uint8Array(o);c.set(n.length>o?t.create().update(n).digest():n);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),m(c)}update(t){return G(this),this.iHash.update(t),this}digestInto(t){G(this),J(t,this),this.finished=!0;let n=t.subarray(0,this.outputLen);this.iHash.digestInto(n),this.oHash.update(n),this.oHash.digestInto(n),this.destroy()}digest(){let t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||=Object.create(Object.getPrototypeOf(this),{});let{oHash:n,iHash:o,finished:c,destroyed:r,blockLen:a,outputLen:s,canXOF:i}=this;return t=t,t.finished=c,t.destroyed=r,t.blockLen=a,t.outputLen=s,t.canXOF=i,t.oHash=n._cloneInto(t.oHash),t.iHash=o._cloneInto(t.iHash),t}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}},q=(()=>{let e=((t,n,o)=>new Z(t,n).update(o).digest());return e.create=(t,n)=>new Z(t,n),e})();function jt(e,t,n,o){z(e);let c=X({dkLen:32,asyncTick:10},o),{c:r,dkLen:a,asyncTick:s}=c;if(y(r,"c"),y(a,"dkLen"),y(s,"asyncTick"),r<1)throw new Error('"c" (iterations) must be >= 1');if(a<1)throw new Error('"dkLen" must be >= 1');if(a>(2**32-1)*e.outputLen)throw new Error("derived key too long");let i=xt(t,"password");try{let x=xt(n,"salt");try{let h=new Uint8Array(a),{iHash:f,oHash:b,outputLen:u}=q.create(e,i),p=new Uint8Array(u),l=Gt(f,b,x,p);return{c:r,dkLen:a,asyncTick:s,DK:h,outputLen:u,eng:l}}finally{typeof n=="string"&&m(x)}}finally{typeof t=="string"&&m(i)}}function Gt(e,t,n,o){let c=new Uint8Array(4),r=P(c),a=e._cloneInto().update(n),s=t._cloneInto(),i=e._cloneInto,x=t._cloneInto;return{u1:(h,f)=>{r.setInt32(0,h,!1),a._cloneInto(s).update(c).digestInto(o),t._cloneInto(s).update(o).digestInto(o),f.set(o.subarray(0,f.length))},rounds:(h,f)=>{for(let b=1;b<h;b++){i.call(e,s).update(o).digestInto(o),x.call(t,s).update(o).digestInto(o);for(let u=0;u<f.length;u++)f[u]^=o[u]}},output:h=>(e.destroy(),t.destroy(),a.destroy(),s.destroy(),m(o),h)}}function ht(e,t,n,o){let{c,dkLen:r,DK:a,outputLen:s,eng:i}=jt(e,t,n,o);for(let x=1,h=0;h<r;x++,h+=s){let f=a.subarray(h,h+s);i.u1(x,f),i.rounds(c,f)}return i.output(a)}var Pt=e=>e/2**32|0,$t=e=>e>>>0;function Ct(e,t,n,o){let c=Pt(n),r=$t(n);e.setUint32(t,o?r:c,o),e.setUint32(t+4,o?c:r,o)}function At(e,t,n){return e&t^~e&n}function Et(e,t,n){return e&t^e&n^t&n}var tt=class{blockLen;outputLen;canXOF=!1;padOffset;isLE;buffer;view;finished=!1;length=0;pos=0;destroyed=!1;constructor(t,n,o,c){this.blockLen=t,this.outputLen=n,this.padOffset=o,this.isLE=c,this.buffer=new Uint8Array(t),this.view=P(this.buffer)}update(t){G(this),j(t);let{view:n,buffer:o,blockLen:c}=this,r=t.length,a=!1;for(let s=0;s<r;){let i=Math.min(c-this.pos,r-s);if(i===c){let x=P(t);for(;c<=r-s;s+=c)this.process(x,s);a=!0;continue}o.set(s===0&&i===r?t:t.subarray(s,s+i),this.pos),this.pos+=i,s+=i,this.pos===c&&(this.process(n,0),this.pos=0,a=!0)}return this.length+=t.length,a&&this.roundClean(),this}digestInto(t){G(this),J(t,this),this.finished=!0;let{buffer:n,view:o,blockLen:c,isLE:r}=this,{pos:a}=this;n[a++]=128,n.fill(0,a),this.padOffset>c-a&&(this.process(o,0),n.fill(0)),Ct(o,c-8,this.length*8,r),this.process(o,0),this.roundClean();let s=t===n?o:P(t),i=this.outputLen,x=i/4,h=this.get();if(i%4||x>h.length)throw new Error("invalid outputLen");for(let f=0;f<x;f++)s.setUint32(4*f,h[f],r)}digest(){let{buffer:t,outputLen:n}=this;this.digestInto(t);let o=t.slice(0,n);return this.destroy(),o}_cloneIntoMeta(t){let{buffer:n,length:o,finished:c,destroyed:r,pos:a}=this;return t.destroyed=r,t.finished=c,t.length=o,t.pos=a,a&&t.buffer.set(n),t}clone(){return this._cloneInto()}},Lt=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var Kt=Uint32Array.from([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]),E=new Uint32Array(64),dt=class extends tt{A=0;B=0;C=0;D=0;E=0;F=0;G=0;H=0;constructor(t,n){super(64,t,8,!1),this.A=n[0]|0,this.B=n[1]|0,this.C=n[2]|0,this.D=n[3]|0,this.E=n[4]|0,this.F=n[5]|0,this.G=n[6]|0,this.H=n[7]|0}get(){let{A:t,B:n,C:o,D:c,E:r,F:a,G:s,H:i}=this;return[t,n,o,c,r,a,s,i]}set(t,n,o,c,r,a,s,i){this.A=t|0,this.B=n|0,this.C=o|0,this.D=c|0,this.E=r|0,this.F=a|0,this.G=s|0,this.H=i|0}_cloneInto(t){return(t||=new this.constructor).set(...this.get()),this._cloneIntoMeta(t)}process(t,n){for(let f=0;f<16;f++,n+=4)E[f]=t.getUint32(n,!1);for(let f=16;f<64;f++){let b=E[f-15],u=E[f-2],p=v(b,7)^v(b,18)^b>>>3,l=v(u,17)^v(u,19)^u>>>10;E[f]=l+E[f-7]+p+E[f-16]|0}let{A:o,B:c,C:r,D:a,E:s,F:i,G:x,H:h}=this;for(let f=0;f<64;f++){let b=v(s,6)^v(s,11)^v(s,25),u=h+b+At(s,i,x)+Kt[f]+E[f]|0,l=(v(o,2)^v(o,13)^v(o,22))+Et(o,c,r)|0;h=x,x=i,i=s,s=a+u|0,a=r,r=c,c=o,o=u+l|0}o=o+this.A|0,c=c+this.B|0,r=r+this.C|0,a=a+this.D|0,s=s+this.E|0,i=i+this.F|0,x=x+this.G|0,h=h+this.H|0,this.set(o,c,r,a,s,i,x,h)}roundClean(){m(E)}destroy(){this.destroyed=!0,this.set(0,0,0,0,0,0,0,0),m(this.buffer)}},ut=class extends dt{constructor(){super(32,Lt)}};var W=gt(()=>new ut,vt(1));function Ht(e,t,n,o,c,r){let a=e[t++]^n[o++],s=e[t++]^n[o++],i=e[t++]^n[o++],x=e[t++]^n[o++],h=e[t++]^n[o++],f=e[t++]^n[o++],b=e[t++]^n[o++],u=e[t++]^n[o++],p=e[t++]^n[o++],l=e[t++]^n[o++],C=e[t++]^n[o++],g=e[t++]^n[o++],$=e[t++]^n[o++],K=e[t++]^n[o++],R=e[t++]^n[o++],Y=e[t++]^n[o++],w=a,A=s,L=i,H=x,k=h,S=f,B=b,T=u,U=p,I=l,O=C,N=g,_=$,M=K,D=R,F=Y;for(let yt=0;yt<8;yt+=2)k^=d(w+_|0,7),U^=d(k+w|0,9),_^=d(U+k|0,13),w^=d(_+U|0,18),I^=d(S+A|0,7),M^=d(I+S|0,9),A^=d(M+I|0,13),S^=d(A+M|0,18),D^=d(O+B|0,7),L^=d(D+O|0,9),B^=d(L+D|0,13),O^=d(B+L|0,18),H^=d(F+N|0,7),T^=d(H+F|0,9),N^=d(T+H|0,13),F^=d(N+T|0,18),A^=d(w+H|0,7),L^=d(A+w|0,9),H^=d(L+A|0,13),w^=d(H+L|0,18),B^=d(S+k|0,7),T^=d(B+S|0,9),k^=d(T+B|0,13),S^=d(k+T|0,18),N^=d(O+I|0,7),U^=d(N+O|0,9),I^=d(U+N|0,13),O^=d(I+U|0,18),_^=d(F+D|0,7),M^=d(_+F|0,9),D^=d(M+_|0,13),F^=d(D+M|0,18);c[r++]=a+w|0,c[r++]=s+A|0,c[r++]=i+L|0,c[r++]=x+H|0,c[r++]=h+k|0,c[r++]=f+S|0,c[r++]=b+B|0,c[r++]=u+T|0,c[r++]=p+U|0,c[r++]=l+I|0,c[r++]=C+O|0,c[r++]=g+N|0,c[r++]=$+_|0,c[r++]=K+M|0,c[r++]=R+D|0,c[r++]=Y+F|0}function lt(e,t,n,o,c){let r=o+0,a=o+16*c;for(let s=0;s<16;s++)n[a+s]=e[t+(2*c-1)*16+s];for(let s=0;s<c;s++,r+=16,t+=16)Ht(n,a,e,t,n,r),s>0&&(a+=16),Ht(n,r,e,t+=16,n,a)}var Xt=1024*(2**20+1+1);function Wt(e,t,n){let o=X({dkLen:32,asyncTick:10,maxmem:Xt},n),{N:c,r,p:a,dkLen:s,asyncTick:i,maxmem:x,onProgress:h}=o;if(y(c,"N"),y(r,"r"),y(a,"p"),y(s,"dkLen"),y(i,"asyncTick"),y(x,"maxmem"),h!==void 0&&typeof h!="function")throw new Error('"onProgress" must be a function');if(r<1)throw new Error('"r" expected integer >= 1');let f=128*r,b=f/4,u=Math.pow(2,32);if(c<=1||(c&c-1)!==0||c>u)throw new Error('"N" expected a power of 2, and 2^1 <= N <= 2^32');if(a<1||a>(u-1)*32/f)throw new Error('"p" expected integer 1..((2^32 - 1) * 32) / (128 * r)');if(s<1||s>(u-1)*32)throw new Error('"dkLen" expected integer 1..(2^32 - 1) * 32');let p=f*(c+a+1);if(p>x)throw new Error('"maxmem" limit was hit: memUsed(128*r*(N+p+1))='+p+", maxmem="+x);let l=ht(W,e,t,{c:1,dkLen:f*a}),C=Q(l),g=Q(new Uint8Array(f*c)),$=Q(new Uint8Array(f)),K=()=>{};if(h){let R=2*c*a,Y=Math.max(Math.floor(R/1e4),1),w=0;K=()=>{if(w++,h&&(!(w%Y)||w===R))try{h(w/R)}catch(A){throw m(l,g,$),A}}}return{N:c,r,p:a,dkLen:s,blockSize32:b,V:g,B32:C,B:l,tmp:$,blockMixCb:K,asyncTick:i}}function Yt(e,t,n,o,c){let r=ht(W,e,n,{c:1,dkLen:t});return m(n,o,c),r}function kt(e,t,n){let{N:o,r:c,p:r,dkLen:a,blockSize32:s,V:i,B32:x,B:h,tmp:f,blockMixCb:b}=Wt(e,t,n);ft(x);for(let u=0;u<r;u++){let p=s*u;for(let l=0;l<s;l++)i[l]=x[p+l];for(let l=0,C=0;l<o-1;l++)lt(i,C,i,C+=s,c),b();lt(i,(o-1)*s,x,p,c),b();for(let l=0;l<o;l++){let C=(x[p+s-16]&o-1)>>>0;for(let g=0;g<s;g++)f[g]=x[p+g]^i[C*s+g];lt(f,0,x,p,c),b()}}return ft(x),Yt(e,a,h,i,f)}var pt=class{name;secret;scoper;key;constructor(t,n,o=at){this.name=t,this.secret=n,this.scoper=o,this.key=this.userKey()}userKey(){let t=new TextEncoder,n=t.encode(this.name),o=t.encode(this.secret),c=t.encode(this.scoper.scope("Authentication")),r=n.length,a=new Uint8Array([...c,...bt(r),...n]),s=32768,i=8,x=2,h=128*i*(s+x+2);return kt(o,a,{N:s,r:i,p:x,maxmem:h,dkLen:64})}siteKey(t,n,o){let c=new TextEncoder,r=c.encode(t),a=c.encode(this.scoper.scope(o)),s=r.length,i=new Uint8Array([...a,...bt(s),...r,...bt(n)]);return q(W,this.key,i)}site(t,n,o=1,c="Authentication"){n??=st(c);let r=this.siteKey(t,o,c),a=ot[n],s=a[r[0]%a.length],i="";for(let x=0;x<s.length;x++){let h=s[x],f=ct[h],b=f[r[x+1]%f.length];i+=b}return i}};function bt(e){let t=new ArrayBuffer(4);return new DataView(t).setInt32(0,e,!1),new Uint8Array(t)}return Ot(Vt);})();