@bicycle-codes/simple-aes
Version:
An easy way to use symmetric keys in browsers or node
8 lines (6 loc) • 28.6 kB
JavaScript
var $t=Object.defineProperty;var s=(t,e)=>$t(t,"name",{value:e,configurable:!0});var S=(t,e)=>{for(var r in e)$t(t,r,{get:e[r],enumerable:!0})};function ht(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"}s(ht,"isBytes");function v(t,...e){if(!ht(t))throw new Error("Uint8Array expected");if(e.length>0&&!e.includes(t.length))throw new Error("Uint8Array expected of length "+e+", got length="+t.length)}s(v,"abytes");function Z(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}s(Z,"aexists");function dt(t,e){v(t);let r=e.outputLen;if(t.length<r)throw new Error("digestInto() expects output buffer of length at least "+r)}s(dt,"aoutput");var Wt=s(t=>new Uint8Array(t.buffer,t.byteOffset,t.byteLength),"u8");var M=s(t=>new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4)),"u32"),j=s(t=>new DataView(t.buffer,t.byteOffset,t.byteLength),"createView"),Ue=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;if(!Ue)throw new Error("Non little-endian hardware is not supported");function Se(t){if(typeof t!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(t))}s(Se,"utf8ToBytes");function G(t){if(typeof t=="string")t=Se(t);else if(ht(t))t=z(t);else throw new Error("Uint8Array expected, got "+typeof t);return t}s(G,"toBytes");function Yt(t,e){if(t.length!==e.length)return!1;let r=0;for(let n=0;n<t.length;n++)r|=t[n]^e[n];return r===0}s(Yt,"equalBytes");var Jt=s((t,e)=>{function r(n,...o){if(v(n),t.nonceLength!==void 0){let p=o[0];if(!p)throw new Error("nonce / iv required");t.varSizeNonce?v(p):v(p,t.nonceLength)}let i=t.tagLength;i&&o[1]!==void 0&&v(o[1]);let c=e(n,...o),f=s((p,h)=>{if(h!==void 0){if(p!==2)throw new Error("cipher output not supported");v(h)}},"checkOutput"),a=!1;return{encrypt(p,h){if(a)throw new Error("cannot encrypt() twice with same key + nonce");return a=!0,v(p),f(c.encrypt.length,h),c.encrypt(p,h)},decrypt(p,h){if(v(p),i&&p.length<i)throw new Error("invalid ciphertext length: smaller than tagLength="+i);return f(c.decrypt.length,h),c.decrypt(p,h)}}}return s(r,"wrappedCipher"),Object.assign(r,t),r},"wrapCipher");function Zt(t,e,r=!0){if(e===void 0)return new Uint8Array(t);if(e.length!==t)throw new Error("invalid output length, expected "+t+", got: "+e.length);if(r&&!H(e))throw new Error("invalid output, must be aligned");return e}s(Zt,"getOutput");function nt(t,e,r,n){if(typeof t.setBigUint64=="function")return t.setBigUint64(e,r,n);let o=BigInt(32),i=BigInt(4294967295),c=Number(r>>o&i),f=Number(r&i),a=n?4:0,u=n?0:4;t.setUint32(e+a,c,n),t.setUint32(e+u,f,n)}s(nt,"setBigUint64");function H(t){return t.byteOffset%4===0}s(H,"isAligned32");function z(t){return Uint8Array.from(t)}s(z,"copyBytes");function I(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}s(I,"clean");var K=16,gt=new Uint8Array(16),O=M(gt),Te=225,Le=s((t,e,r,n)=>{let o=n&1;return{s3:r<<31|n>>>1,s2:e<<31|r>>>1,s1:t<<31|e>>>1,s0:t>>>1^Te<<24&-(o&1)}},"mul2"),T=s(t=>(t>>>0&255)<<24|(t>>>8&255)<<16|(t>>>16&255)<<8|t>>>24&255|0,"swapLE");function Me(t){t.reverse();let e=t[15]&1,r=0;for(let n=0;n<t.length;n++){let o=t[n];t[n]=o>>>1|r,r=(o&1)<<7}return t[0]^=-e&225,t}s(Me,"_toGHASHKey");var Ie=s(t=>t>64*1024?8:t>1024?4:2,"estimateWindow"),ot=class{static{s(this,"GHASH")}constructor(e,r){this.blockLen=K,this.outputLen=K,this.s0=0,this.s1=0,this.s2=0,this.s3=0,this.finished=!1,e=G(e),v(e,16);let n=j(e),o=n.getUint32(0,!1),i=n.getUint32(4,!1),c=n.getUint32(8,!1),f=n.getUint32(12,!1),a=[];for(let l=0;l<128;l++)a.push({s0:T(o),s1:T(i),s2:T(c),s3:T(f)}),{s0:o,s1:i,s2:c,s3:f}=Le(o,i,c,f);let u=Ie(r||1024);if(![1,2,4,8].includes(u))throw new Error("ghash: invalid window size, expected 2, 4 or 8");this.W=u;let h=128/u,g=this.windowSize=2**u,w=[];for(let l=0;l<h;l++)for(let d=0;d<g;d++){let A=0,b=0,y=0,E=0;for(let m=0;m<u;m++){if(!(d>>>u-m-1&1))continue;let{s0:U,s1:B,s2:L,s3:k}=a[u*l+m];A^=U,b^=B,y^=L,E^=k}w.push({s0:A,s1:b,s2:y,s3:E})}this.t=w}_updateBlock(e,r,n,o){e^=this.s0,r^=this.s1,n^=this.s2,o^=this.s3;let{W:i,t:c,windowSize:f}=this,a=0,u=0,p=0,h=0,g=(1<<i)-1,w=0;for(let l of[e,r,n,o])for(let d=0;d<4;d++){let A=l>>>8*d&255;for(let b=8/i-1;b>=0;b--){let y=A>>>i*b&g,{s0:E,s1:m,s2:C,s3:U}=c[w*f+y];a^=E,u^=m,p^=C,h^=U,w+=1}}this.s0=a,this.s1=u,this.s2=p,this.s3=h}update(e){e=G(e),Z(this);let r=M(e),n=Math.floor(e.length/K),o=e.length%K;for(let i=0;i<n;i++)this._updateBlock(r[i*4+0],r[i*4+1],r[i*4+2],r[i*4+3]);return o&&(gt.set(e.subarray(n*K)),this._updateBlock(O[0],O[1],O[2],O[3]),I(O)),this}destroy(){let{t:e}=this;for(let r of e)r.s0=0,r.s1=0,r.s2=0,r.s3=0}digestInto(e){Z(this),dt(e,this),this.finished=!0;let{s0:r,s1:n,s2:o,s3:i}=this,c=M(e);return c[0]=r,c[1]=n,c[2]=o,c[3]=i,e}digest(){let e=new Uint8Array(K);return this.digestInto(e),this.destroy(),e}},yt=class extends ot{static{s(this,"Polyval")}constructor(e,r){e=G(e);let n=Me(z(e));super(n,r),I(n)}update(e){e=G(e),Z(this);let r=M(e),n=e.length%K,o=Math.floor(e.length/K);for(let i=0;i<o;i++)this._updateBlock(T(r[i*4+3]),T(r[i*4+2]),T(r[i*4+1]),T(r[i*4+0]));return n&&(gt.set(e.subarray(o*K)),this._updateBlock(T(O[3]),T(O[2]),T(O[1]),T(O[0])),I(O)),this}digestInto(e){Z(this),dt(e,this),this.finished=!0;let{s0:r,s1:n,s2:o,s3:i}=this,c=M(e);return c[0]=r,c[1]=n,c[2]=o,c[3]=i,e.reverse()}};function Ht(t){let e=s((n,o)=>t(o,n.length).update(G(n)).digest(),"hashC"),r=t(new Uint8Array(16),0);return e.outputLen=r.outputLen,e.blockLen=r.blockLen,e.create=(n,o)=>t(n,o),e}s(Ht,"wrapConstructorWithKey");var mt=Ht((t,e)=>new ot(t,e)),Oe=Ht((t,e)=>new yt(t,e));var bt=16,Ne=4,st=new Uint8Array(bt),Ke=283;function xt(t){return t<<1^Ke&-(t>>7)}s(xt,"mul2");function Xt(t,e){let r=0;for(;e>0;e>>=1)r^=t&-(e&1),t=xt(t);return r}s(Xt,"mul");var De=(()=>{let t=new Uint8Array(256);for(let r=0,n=1;r<256;r++,n^=xt(n))t[r]=n;let e=new Uint8Array(256);e[0]=99;for(let r=0;r<255;r++){let n=t[255-r];n|=n<<8,e[t[r]]=(n^n>>4^n>>5^n>>6^n>>7^99)&255}return I(t),e})();var _e=s(t=>t<<24|t>>>8,"rotr32_8"),wt=s(t=>t<<8|t>>>24,"rotl32_8");function ze(t,e){if(t.length!==256)throw new Error("Wrong sbox length");let r=new Uint32Array(256).map((u,p)=>e(t[p])),n=r.map(wt),o=n.map(wt),i=o.map(wt),c=new Uint32Array(256*256),f=new Uint32Array(256*256),a=new Uint16Array(256*256);for(let u=0;u<256;u++)for(let p=0;p<256;p++){let h=u*256+p;c[h]=r[u]^n[p],f[h]=o[u]^i[p],a[h]=t[u]<<8|t[p]}return{sbox:t,sbox2:a,T0:r,T1:n,T2:o,T3:i,T01:c,T23:f}}s(ze,"genTtable");var te=ze(De,t=>Xt(t,3)<<24|t<<16|t<<8|Xt(t,2));var Pe=(()=>{let t=new Uint8Array(16);for(let e=0,r=1;e<16;e++,r=xt(r))t[e]=r;return t})();function Re(t){v(t);let e=t.length;if(![16,24,32].includes(e))throw new Error("aes: invalid key size, should be 16, 24 or 32, got "+e);let{sbox2:r}=te,n=[];H(t)||n.push(t=z(t));let o=M(t),i=o.length,c=s(a=>X(r,a,a,a,a),"subByte"),f=new Uint32Array(e+28);f.set(o);for(let a=i;a<f.length;a++){let u=f[a-1];a%i===0?u=c(_e(u))^Pe[a/i-1]:i>6&&a%i===4&&(u=c(u)),f[a]=f[a-i]^u}return I(...n),f}s(Re,"expandKeyLE");function it(t,e,r,n,o,i){return t[r<<8&65280|n>>>8&255]^e[o>>>8&65280|i>>>24&255]}s(it,"apply0123");function X(t,e,r,n,o){return t[e&255|r&65280]|t[n>>>16&255|o>>>16&65280]<<16}s(X,"applySbox");function Qt(t,e,r,n,o){let{sbox2:i,T01:c,T23:f}=te,a=0;e^=t[a++],r^=t[a++],n^=t[a++],o^=t[a++];let u=t.length/4-2;for(let l=0;l<u;l++){let d=t[a++]^it(c,f,e,r,n,o),A=t[a++]^it(c,f,r,n,o,e),b=t[a++]^it(c,f,n,o,e,r),y=t[a++]^it(c,f,o,e,r,n);e=d,r=A,n=b,o=y}let p=t[a++]^X(i,e,r,n,o),h=t[a++]^X(i,r,n,o,e),g=t[a++]^X(i,n,o,e,r),w=t[a++]^X(i,o,e,r,n);return{s0:p,s1:h,s2:g,s3:w}}s(Qt,"encrypt");function ct(t,e,r,n,o){v(r,bt),v(n),o=Zt(n.length,o);let i=r,c=M(i),f=j(i),a=M(n),u=M(o),p=e?0:12,h=n.length,g=f.getUint32(p,e),{s0:w,s1:l,s2:d,s3:A}=Qt(t,c[0],c[1],c[2],c[3]);for(let y=0;y+4<=a.length;y+=4)u[y+0]=a[y+0]^w,u[y+1]=a[y+1]^l,u[y+2]=a[y+2]^d,u[y+3]=a[y+3]^A,g=g+1>>>0,f.setUint32(p,g,e),{s0:w,s1:l,s2:d,s3:A}=Qt(t,c[0],c[1],c[2],c[3]);let b=bt*Math.floor(a.length/Ne);if(b<h){let y=new Uint32Array([w,l,d,A]),E=Wt(y);for(let m=b,C=0;m<h;m++,C++)o[m]=n[m]^E[C];I(y)}return o}s(ct,"ctr32");function Ve(t,e,r,n,o){let i=o==null?0:o.length,c=t.create(r,n.length+i);o&&c.update(o),c.update(n);let f=new Uint8Array(16),a=j(f);o&&nt(a,0,BigInt(i*8),e),nt(a,8,BigInt(n.length*8),e),c.update(f);let u=c.digest();return I(f),u}s(Ve,"computeTag");var At=Jt({blockSize:16,nonceLength:12,tagLength:16,varSizeNonce:!0},s(function(e,r,n){if(r.length<8)throw new Error("aes/gcm: invalid nonce length");let o=16;function i(f,a,u){let p=Ve(mt,!1,f,u,n);for(let h=0;h<a.length;h++)p[h]^=a[h];return p}s(i,"_computeTag");function c(){let f=Re(e),a=st.slice(),u=st.slice();if(ct(f,!1,u,u,a),r.length===12)u.set(r);else{let h=st.slice(),g=j(h);nt(g,8,BigInt(r.length*8),!1);let w=mt.create(a).update(r).update(h);w.digestInto(u),w.destroy()}let p=ct(f,!1,u,st);return{xk:f,authKey:a,counter:u,tagMask:p}}return s(c,"deriveKeys"),{encrypt(f){let{xk:a,authKey:u,counter:p,tagMask:h}=c(),g=new Uint8Array(f.length+o),w=[a,u,p,h];H(f)||w.push(f=z(f)),ct(a,!1,p,f,g.subarray(0,f.length));let l=i(u,h,g.subarray(0,g.length-o));return w.push(l),g.set(l,f.length),I(...w),g},decrypt(f){let{xk:a,authKey:u,counter:p,tagMask:h}=c(),g=[a,u,h,p];H(f)||g.push(f=z(f));let w=f.subarray(0,-o),l=f.subarray(-o),d=i(u,h,w);if(g.push(d),!Yt(d,l))throw new Error("aes/gcm: invalid ghash tag");let A=ct(a,!1,p,w);return I(...g),A}}},"aesgcm"));var P=typeof globalThis=="object"&&"crypto"in globalThis?globalThis.crypto:void 0;function Et(t=32){if(P&&typeof P.getRandomValues=="function")return P.getRandomValues(new Uint8Array(t));if(P&&typeof P.randomBytes=="function")return P.randomBytes(t);throw new Error("crypto.getRandomValues must be defined")}s(Et,"randomBytes");function at(t=0){return new Uint8Array(t)}s(at,"allocUnsafe");var St={};S(St,{base10:()=>We});var zn=new Uint8Array(0);function ee(t,e){if(t===e)return!0;if(t.byteLength!==e.byteLength)return!1;for(let r=0;r<t.byteLength;r++)if(t[r]!==e[r])return!1;return!0}s(ee,"equals");function D(t){if(t instanceof Uint8Array&&t.constructor.name==="Uint8Array")return t;if(t instanceof ArrayBuffer)return new Uint8Array(t);if(ArrayBuffer.isView(t))return new Uint8Array(t.buffer,t.byteOffset,t.byteLength);throw new Error("Unknown type, must be binary type")}s(D,"coerce");function re(t){return new TextEncoder().encode(t)}s(re,"fromString");function ne(t){return new TextDecoder().decode(t)}s(ne,"toString");function je(t,e){if(t.length>=255)throw new TypeError("Alphabet too long");for(var r=new Uint8Array(256),n=0;n<r.length;n++)r[n]=255;for(var o=0;o<t.length;o++){var i=t.charAt(o),c=i.charCodeAt(0);if(r[c]!==255)throw new TypeError(i+" is ambiguous");r[c]=o}var f=t.length,a=t.charAt(0),u=Math.log(f)/Math.log(256),p=Math.log(256)/Math.log(f);function h(l){if(l instanceof Uint8Array||(ArrayBuffer.isView(l)?l=new Uint8Array(l.buffer,l.byteOffset,l.byteLength):Array.isArray(l)&&(l=Uint8Array.from(l))),!(l instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(l.length===0)return"";for(var d=0,A=0,b=0,y=l.length;b!==y&&l[b]===0;)b++,d++;for(var E=(y-b)*p+1>>>0,m=new Uint8Array(E);b!==y;){for(var C=l[b],U=0,B=E-1;(C!==0||U<A)&&B!==-1;B--,U++)C+=256*m[B]>>>0,m[B]=C%f>>>0,C=C/f>>>0;if(C!==0)throw new Error("Non-zero carry");A=U,b++}for(var L=E-A;L!==E&&m[L]===0;)L++;for(var k=a.repeat(d);L<E;++L)k+=t.charAt(m[L]);return k}s(h,"encode");function g(l){if(typeof l!="string")throw new TypeError("Expected String");if(l.length===0)return new Uint8Array;var d=0;if(l[d]!==" "){for(var A=0,b=0;l[d]===a;)A++,d++;for(var y=(l.length-d)*u+1>>>0,E=new Uint8Array(y);l[d];){var m=r[l.charCodeAt(d)];if(m===255)return;for(var C=0,U=y-1;(m!==0||C<b)&&U!==-1;U--,C++)m+=f*E[U]>>>0,E[U]=m%256>>>0,m=m/256>>>0;if(m!==0)throw new Error("Non-zero carry");b=C,d++}if(l[d]!==" "){for(var B=y-b;B!==y&&E[B]===0;)B++;for(var L=new Uint8Array(A+(y-B)),k=A;B!==y;)L[k++]=E[B++];return L}}}s(g,"decodeUnsafe");function w(l){var d=g(l);if(d)return d;throw new Error(`Non-${e} character`)}return s(w,"decode"),{encode:h,decodeUnsafe:g,decode:w}}s(je,"base");var Ge=je,Fe=Ge,se=Fe;var Ct=class{static{s(this,"Encoder")}name;prefix;baseEncode;constructor(e,r,n){this.name=e,this.prefix=r,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},vt=class{static{s(this,"Decoder")}name;prefix;baseDecode;prefixCodePoint;constructor(e,r,n){if(this.name=e,this.prefix=r,r.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=r.codePointAt(0),this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return ie(this,e)}},Bt=class{static{s(this,"ComposedDecoder")}decoders;constructor(e){this.decoders=e}or(e){return ie(this,e)}decode(e){let r=e[0],n=this.decoders[r];if(n!=null)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function ie(t,e){return new Bt({...t.decoders??{[t.prefix]:t},...e.decoders??{[e.prefix]:e}})}s(ie,"or");var Ut=class{static{s(this,"Codec")}name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(e,r,n,o){this.name=e,this.prefix=r,this.baseEncode=n,this.baseDecode=o,this.encoder=new Ct(e,r,n),this.decoder=new vt(e,r,o)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};function F({name:t,prefix:e,encode:r,decode:n}){return new Ut(t,e,r,n)}s(F,"from");function _({name:t,prefix:e,alphabet:r}){let{encode:n,decode:o}=se(r,t);return F({prefix:e,name:t,encode:n,decode:s(i=>D(o(i)),"decode")})}s(_,"baseX");function qe(t,e,r,n){let o={};for(let p=0;p<e.length;++p)o[e[p]]=p;let i=t.length;for(;t[i-1]==="=";)--i;let c=new Uint8Array(i*r/8|0),f=0,a=0,u=0;for(let p=0;p<i;++p){let h=o[t[p]];if(h===void 0)throw new SyntaxError(`Non-${n} character`);a=a<<r|h,f+=r,f>=8&&(f-=8,c[u++]=255&a>>f)}if(f>=r||255&a<<8-f)throw new SyntaxError("Unexpected end of data");return c}s(qe,"decode");function $e(t,e,r){let n=e[e.length-1]==="=",o=(1<<r)-1,i="",c=0,f=0;for(let a=0;a<t.length;++a)for(f=f<<8|t[a],c+=8;c>r;)c-=r,i+=e[o&f>>c];if(c!==0&&(i+=e[o&f<<r-c]),n)for(;i.length*r&7;)i+="=";return i}s($e,"encode");function x({name:t,prefix:e,bitsPerChar:r,alphabet:n}){return F({prefix:e,name:t,encode(o){return $e(o,n,r)},decode(o){return qe(o,n,r,t)}})}s(x,"rfc4648");var We=_({prefix:"9",name:"base10",alphabet:"0123456789"});var Tt={};S(Tt,{base16:()=>Ye,base16upper:()=>Je});var Ye=x({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Je=x({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var Lt={};S(Lt,{base2:()=>Ze});var Ze=x({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var Mt={};S(Mt,{base256emoji:()=>er});var ce=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),He=ce.reduce((t,e,r)=>(t[r]=e,t),[]),Xe=ce.reduce((t,e,r)=>(t[e.codePointAt(0)]=r,t),[]);function Qe(t){return t.reduce((e,r)=>(e+=He[r],e),"")}s(Qe,"encode");function tr(t){let e=[];for(let r of t){let n=Xe[r.codePointAt(0)];if(n===void 0)throw new Error(`Non-base256emoji character: ${r}`);e.push(n)}return new Uint8Array(e)}s(tr,"decode");var er=F({prefix:"\u{1F680}",name:"base256emoji",encode:Qe,decode:tr});var It={};S(It,{base32:()=>q,base32hex:()=>sr,base32hexpad:()=>cr,base32hexpadupper:()=>ar,base32hexupper:()=>ir,base32pad:()=>nr,base32padupper:()=>or,base32upper:()=>rr,base32z:()=>fr});var q=x({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),rr=x({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),nr=x({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),or=x({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),sr=x({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),ir=x({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),cr=x({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),ar=x({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),fr=x({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Ot={};S(Ot,{base36:()=>ur,base36upper:()=>pr});var ur=_({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),pr=_({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var Nt={};S(Nt,{base58btc:()=>N,base58flickr:()=>lr});var N=_({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),lr=_({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Kt={};S(Kt,{base64:()=>hr,base64pad:()=>dr,base64url:()=>yr,base64urlpad:()=>gr});var hr=x({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),dr=x({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),yr=x({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),gr=x({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var Dt={};S(Dt,{base8:()=>mr});var mr=x({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var _t={};S(_t,{identity:()=>wr});var wr=F({prefix:"\0",name:"identity",encode:s(t=>ne(t),"encode"),decode:s(t=>re(t),"decode")});var oo=new TextEncoder,so=new TextDecoder;var Pt={};S(Pt,{identity:()=>kr});var Ar=ue,ae=128,Er=127,Cr=~Er,vr=Math.pow(2,31);function ue(t,e,r){e=e||[],r=r||0;for(var n=r;t>=vr;)e[r++]=t&255|ae,t/=128;for(;t&Cr;)e[r++]=t&255|ae,t>>>=7;return e[r]=t|0,ue.bytes=r-n+1,e}s(ue,"encode");var Br=zt,Ur=128,fe=127;function zt(t,n){var r=0,n=n||0,o=0,i=n,c,f=t.length;do{if(i>=f)throw zt.bytes=0,new RangeError("Could not decode varint");c=t[i++],r+=o<28?(c&fe)<<o:(c&fe)*Math.pow(2,o),o+=7}while(c>=Ur);return zt.bytes=i-n,r}s(zt,"read");var Sr=Math.pow(2,7),Tr=Math.pow(2,14),Lr=Math.pow(2,21),Mr=Math.pow(2,28),Ir=Math.pow(2,35),Or=Math.pow(2,42),Nr=Math.pow(2,49),Kr=Math.pow(2,56),Dr=Math.pow(2,63),_r=s(function(t){return t<Sr?1:t<Tr?2:t<Lr?3:t<Mr?4:t<Ir?5:t<Or?6:t<Nr?7:t<Kr?8:t<Dr?9:10},"length"),zr={encode:Ar,decode:Br,encodingLength:_r},Pr=zr,Q=Pr;function tt(t,e=0){return[Q.decode(t,e),Q.decode.bytes]}s(tt,"decode");function $(t,e,r=0){return Q.encode(t,e,r),e}s($,"encodeTo");function W(t){return Q.encodingLength(t)}s(W,"encodingLength");function R(t,e){let r=e.byteLength,n=W(t),o=n+W(r),i=new Uint8Array(o+r);return $(t,i,0),$(r,i,n),i.set(e,o),new Y(t,r,e,i)}s(R,"create");function pe(t){let e=D(t),[r,n]=tt(e),[o,i]=tt(e.subarray(n)),c=e.subarray(n+i);if(c.byteLength!==o)throw new Error("Incorrect length");return new Y(r,o,c,e)}s(pe,"decode");function le(t,e){if(t===e)return!0;{let r=e;return t.code===r.code&&t.size===r.size&&r.bytes instanceof Uint8Array&&ee(t.bytes,r.bytes)}}s(le,"equals");var Y=class{static{s(this,"Digest")}code;size;digest;bytes;constructor(e,r,n,o){this.code=e,this.size=r,this.digest=n,this.bytes=o}};var he=0,Rr="identity",de=D;function Vr(t){return R(he,de(t))}s(Vr,"digest");var kr={code:he,name:Rr,encode:de,digest:Vr};var kt={};S(kt,{sha256:()=>jr,sha512:()=>Gr});function Vt({name:t,code:e,encode:r}){return new Rt(t,e,r)}s(Vt,"from");var Rt=class{static{s(this,"Hasher")}name;code;encode;constructor(e,r,n){this.name=e,this.code=r,this.encode=n}digest(e){if(e instanceof Uint8Array){let r=this.encode(e);return r instanceof Uint8Array?R(this.code,r):r.then(n=>R(this.code,n))}else throw Error("Unknown type, must be binary type")}};function ge(t){return async e=>new Uint8Array(await crypto.subtle.digest(t,e))}s(ge,"sha");var jr=Vt({name:"sha2-256",code:18,encode:ge("SHA-256")}),Gr=Vt({name:"sha2-512",code:19,encode:ge("SHA-512")});function me(t,e){let{bytes:r,version:n}=t;switch(n){case 0:return qr(r,jt(t),e??N.encoder);default:return $r(r,jt(t),e??q.encoder)}}s(me,"format");var we=new WeakMap;function jt(t){let e=we.get(t);if(e==null){let r=new Map;return we.set(t,r),r}return e}s(jt,"baseCache");var ut=class t{static{s(this,"CID")}code;version;multihash;bytes;"/";constructor(e,r,n,o){this.code=r,this.version=e,this.multihash=n,this.bytes=o,this["/"]=o}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:e,multihash:r}=this;if(e!==rt)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(r.code!==Wr)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return t.createV0(r)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:e,digest:r}=this.multihash,n=R(e,r);return t.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(e){return t.equals(this,e)}static equals(e,r){let n=r;return n!=null&&e.code===n.code&&e.version===n.version&&le(e.multihash,n.multihash)}toString(e){return me(this,e)}toJSON(){return{"/":me(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(e){if(e==null)return null;let r=e;if(r instanceof t)return r;if(r["/"]!=null&&r["/"]===r.bytes||r.asCID===r){let{version:n,code:o,multihash:i,bytes:c}=r;return new t(n,o,i,c??be(n,o,i.bytes))}else if(r[Yr]===!0){let{version:n,multihash:o,code:i}=r,c=pe(o);return t.create(n,i,c)}else return null}static create(e,r,n){if(typeof r!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(e){case 0:{if(r!==rt)throw new Error(`Version 0 CID must use dag-pb (code: ${rt}) block encoding`);return new t(e,r,n,n.bytes)}case 1:{let o=be(e,r,n.bytes);return new t(e,r,n,o)}default:throw new Error("Invalid version")}}static createV0(e){return t.create(0,rt,e)}static createV1(e,r){return t.create(1,e,r)}static decode(e){let[r,n]=t.decodeFirst(e);if(n.length!==0)throw new Error("Incorrect length");return r}static decodeFirst(e){let r=t.inspectBytes(e),n=r.size-r.multihashSize,o=D(e.subarray(n,n+r.multihashSize));if(o.byteLength!==r.multihashSize)throw new Error("Incorrect length");let i=o.subarray(r.multihashSize-r.digestSize),c=new Y(r.multihashCode,r.digestSize,i,o);return[r.version===0?t.createV0(c):t.createV1(r.codec,c),e.subarray(r.size)]}static inspectBytes(e){let r=0,n=s(()=>{let[h,g]=tt(e.subarray(r));return r+=g,h},"next"),o=n(),i=rt;if(o===18?(o=0,r=0):i=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let c=r,f=n(),a=n(),u=r+a,p=u-c;return{version:o,codec:i,multihashCode:f,digestSize:a,multihashSize:p,size:u}}static parse(e,r){let[n,o]=Fr(e,r),i=t.decode(o);if(i.version===0&&e[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return jt(i).set(n,e),i}};function Fr(t,e){switch(t[0]){case"Q":{let r=e??N;return[N.prefix,r.decode(`${N.prefix}${t}`)]}case N.prefix:{let r=e??N;return[N.prefix,r.decode(t)]}case q.prefix:{let r=e??q;return[q.prefix,r.decode(t)]}default:{if(e==null)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[t[0],e.decode(t)]}}}s(Fr,"parseCIDtoBytes");function qr(t,e,r){let{prefix:n}=r;if(n!==N.prefix)throw Error(`Cannot string encode V0 in ${r.name} encoding`);let o=e.get(n);if(o==null){let i=r.encode(t).slice(1);return e.set(n,i),i}else return o}s(qr,"toStringV0");function $r(t,e,r){let{prefix:n}=r,o=e.get(n);if(o==null){let i=r.encode(t);return e.set(n,i),i}else return o}s($r,"toStringV1");var rt=112,Wr=18;function be(t,e,r){let n=W(t),o=n+W(e),i=new Uint8Array(o+r.byteLength);return $(t,i,0),$(e,i,n),i.set(r,o),i}s(be,"encodeCID");var Yr=Symbol.for("@ipld/js-cid/CID");var Gt={..._t,...Lt,...Dt,...St,...Tt,...It,...Ot,...Nt,...Kt,...Mt},_o={...kt,...Pt};function Ae(t,e,r,n){return{name:t,prefix:e,encoder:{name:t,prefix:e,encode:r},decoder:{decode:n}}}s(Ae,"createCodec");var xe=Ae("utf8","u",t=>"u"+new TextDecoder("utf8").decode(t),t=>new TextEncoder().encode(t.substring(1))),Ft=Ae("ascii","a",t=>{let e="a";for(let r=0;r<t.length;r++)e+=String.fromCharCode(t[r]);return e},t=>{t=t.substring(1);let e=at(t.length);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);return e}),Jr={utf8:xe,"utf-8":xe,hex:Gt.base16,latin1:Ft,ascii:Ft,binary:Ft,...Gt},pt=Jr;function V(t,e="utf8"){let r=pt[e];if(r==null)throw new Error(`Unsupported encoding "${e}"`);return r.decoder.decode(`${r.prefix}${t}`)}s(V,"fromString");function J(t,e="utf8"){let r=pt[e];if(r==null)throw new Error(`Unsupported encoding "${e}"`);return r.encoder.encode(t).substring(1)}s(J,"toString");var qt="base64pad",lt="base64url";var Ee=s(function(e,r){if(typeof e=="string")return r(e);if(typeof e=="object"&&e.byteLength!==void 0)return new Uint8Array(e).buffer;throw new Error("Improper value. Must be a string, ArrayBuffer, Uint8Array")},"normalizeToBuf");function Ce(t,e){return V(e,t).buffer}s(Ce,"base64ToArrBuf");function ve(t,e){return Ee(t,Ce.bind(null,e))}s(ve,"normalizeBase64ToBuf");var Zr=globalThis||window||self,Hr=Zr.crypto;var Be=256;async function Ss(t,e={length:Be}){let r=Et(e.length/8),n=Et(12),i=await At(r,n).encrypt(V(t.content)),c=J(new Uint8Array([...n,...i]),qt),f=J(r,lt);return[{content:c},{key:f}]}s(Ss,"encryptMessage");async function Ts(t,e){let r=ve(t.content,"base64pad"),n=r.slice(0,12),o=r.slice(12),c=At(V(e,lt),new Uint8Array(n)).decrypt(new Uint8Array(o));return{content:J(c)}}s(Ts,"decryptMessage");export{Ts as decryptMessage,Ss as encryptMessage};
/*! Bundled license information:
@noble/ciphers/esm/utils.js:
(*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
*/
//# sourceMappingURL=compat.min.js.map