UNPKG

@bicycle-codes/simple-aes

Version:

An easy way to use symmetric keys in browsers or node

3 lines (2 loc) 20.9 kB
var ve=Object.defineProperty;var o=(r,e)=>ve(r,"name",{value:e,configurable:!0});var m=(r,e)=>{for(var t in e)ve(r,t,{get:e[t],enumerable:!0})};var Xe=globalThis||window||self,A=Xe.crypto;function q(r=0){return new Uint8Array(r)}o(q,"allocUnsafe");var Z={};m(Z,{base10:()=>tt});var Ir=new Uint8Array(0);function Ce(r,e){if(r===e)return!0;if(r.byteLength!==e.byteLength)return!1;for(let t=0;t<r.byteLength;t++)if(r[t]!==e[t])return!1;return!0}o(Ce,"equals");function E(r){if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")}o(E,"coerce");function Ee(r){return new TextEncoder().encode(r)}o(Ee,"fromString");function Se(r){return new TextDecoder().decode(r)}o(Se,"toString");function Qe(r,e){if(r.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),n=0;n<t.length;n++)t[n]=255;for(var i=0;i<r.length;i++){var s=r.charAt(i),a=s.charCodeAt(0);if(t[a]!==255)throw new TypeError(s+" is ambiguous");t[a]=i}var c=r.length,u=r.charAt(0),M=Math.log(c)/Math.log(256),h=Math.log(256)/Math.log(c);function D(f){if(f instanceof Uint8Array||(ArrayBuffer.isView(f)?f=new Uint8Array(f.buffer,f.byteOffset,f.byteLength):Array.isArray(f)&&(f=Uint8Array.from(f))),!(f instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(f.length===0)return"";for(var d=0,B=0,y=0,g=f.length;y!==g&&f[y]===0;)y++,d++;for(var x=(g-y)*h+1>>>0,l=new Uint8Array(x);y!==g;){for(var w=f[y],S=0,b=x-1;(w!==0||S<B)&&b!==-1;b--,S++)w+=256*l[b]>>>0,l[b]=w%c>>>0,w=w/c>>>0;if(w!==0)throw new Error("Non-zero carry");B=S,y++}for(var C=x-B;C!==x&&l[C]===0;)C++;for(var k=u.repeat(d);C<x;++C)k+=r.charAt(l[C]);return k}o(D,"encode");function G(f){if(typeof f!="string")throw new TypeError("Expected String");if(f.length===0)return new Uint8Array;var d=0;if(f[d]!==" "){for(var B=0,y=0;f[d]===u;)B++,d++;for(var g=(f.length-d)*M+1>>>0,x=new Uint8Array(g);f[d];){var l=t[f.charCodeAt(d)];if(l===255)return;for(var w=0,S=g-1;(l!==0||w<y)&&S!==-1;S--,w++)l+=c*x[S]>>>0,x[S]=l%256>>>0,l=l/256>>>0;if(l!==0)throw new Error("Non-zero carry");y=w,d++}if(f[d]!==" "){for(var b=g-y;b!==g&&x[b]===0;)b++;for(var C=new Uint8Array(B+(g-b)),k=B;b!==g;)C[k++]=x[b++];return C}}}o(G,"decodeUnsafe");function Je(f){var d=G(f);if(d)return d;throw new Error(`Non-${e} character`)}return o(Je,"decode"),{encode:D,decodeUnsafe:G,decode:Je}}o(Qe,"base");var He=Qe,We=He,Be=We;var Y=class{static{o(this,"Encoder")}name;prefix;baseEncode;constructor(e,t,n){this.name=e,this.prefix=t,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},Q=class{static{o(this,"Decoder")}name;prefix;baseDecode;prefixCodePoint;constructor(e,t,n){if(this.name=e,this.prefix=t,t.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=t.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 Te(this,e)}},H=class{static{o(this,"ComposedDecoder")}decoders;constructor(e){this.decoders=e}or(e){return Te(this,e)}decode(e){let t=e[0],n=this.decoders[t];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 Te(r,e){return new H({...r.decoders??{[r.prefix]:r},...e.decoders??{[e.prefix]:e}})}o(Te,"or");var W=class{static{o(this,"Codec")}name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(e,t,n,i){this.name=e,this.prefix=t,this.baseEncode=n,this.baseDecode=i,this.encoder=new Y(e,t,n),this.decoder=new Q(e,t,i)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};function N({name:r,prefix:e,encode:t,decode:n}){return new W(r,e,t,n)}o(N,"from");function U({name:r,prefix:e,alphabet:t}){let{encode:n,decode:i}=Be(t,r);return N({prefix:e,name:r,encode:n,decode:o(s=>E(i(s)),"decode")})}o(U,"baseX");function Ze(r,e,t,n){let i={};for(let h=0;h<e.length;++h)i[e[h]]=h;let s=r.length;for(;r[s-1]==="=";)--s;let a=new Uint8Array(s*t/8|0),c=0,u=0,M=0;for(let h=0;h<s;++h){let D=i[r[h]];if(D===void 0)throw new SyntaxError(`Non-${n} character`);u=u<<t|D,c+=t,c>=8&&(c-=8,a[M++]=255&u>>c)}if(c>=t||255&u<<8-c)throw new SyntaxError("Unexpected end of data");return a}o(Ze,"decode");function et(r,e,t){let n=e[e.length-1]==="=",i=(1<<t)-1,s="",a=0,c=0;for(let u=0;u<r.length;++u)for(c=c<<8|r[u],a+=8;a>t;)a-=t,s+=e[i&c>>a];if(a!==0&&(s+=e[i&c<<t-a]),n)for(;s.length*t&7;)s+="=";return s}o(et,"encode");function p({name:r,prefix:e,bitsPerChar:t,alphabet:n}){return N({prefix:e,name:r,encode(i){return et(i,n,t)},decode(i){return Ze(i,n,t,r)}})}o(p,"rfc4648");var tt=U({prefix:"9",name:"base10",alphabet:"0123456789"});var ee={};m(ee,{base16:()=>rt,base16upper:()=>nt});var rt=p({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),nt=p({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var te={};m(te,{base2:()=>ot});var ot=p({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var re={};m(re,{base256emoji:()=>ft});var Me=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}"),it=Me.reduce((r,e,t)=>(r[t]=e,r),[]),st=Me.reduce((r,e,t)=>(r[e.codePointAt(0)]=t,r),[]);function at(r){return r.reduce((e,t)=>(e+=it[t],e),"")}o(at,"encode");function ct(r){let e=[];for(let t of r){let n=st[t.codePointAt(0)];if(n===void 0)throw new Error(`Non-base256emoji character: ${t}`);e.push(n)}return new Uint8Array(e)}o(ct,"decode");var ft=N({prefix:"\u{1F680}",name:"base256emoji",encode:at,decode:ct});var ne={};m(ne,{base32:()=>z,base32hex:()=>ht,base32hexpad:()=>mt,base32hexpadupper:()=>yt,base32hexupper:()=>lt,base32pad:()=>ut,base32padupper:()=>dt,base32upper:()=>pt,base32z:()=>bt});var z=p({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),pt=p({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),ut=p({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),dt=p({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),ht=p({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),lt=p({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),mt=p({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),yt=p({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),bt=p({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var oe={};m(oe,{base36:()=>gt,base36upper:()=>xt});var gt=U({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),xt=U({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var ie={};m(ie,{base58btc:()=>v,base58flickr:()=>wt});var v=U({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),wt=U({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var se={};m(se,{base64:()=>At,base64pad:()=>vt,base64url:()=>Ct,base64urlpad:()=>Et});var At=p({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),vt=p({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Ct=p({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Et=p({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var ae={};m(ae,{base8:()=>St});var St=p({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var ce={};m(ce,{identity:()=>Ut});var Ut=N({prefix:"\0",name:"identity",encode:o(r=>Se(r),"encode"),decode:o(r=>Ee(r),"decode")});var en=new TextEncoder,tn=new TextDecoder;var pe={};m(pe,{identity:()=>Yt});var Mt=ze,De=128,Dt=127,Nt=~Dt,zt=Math.pow(2,31);function ze(r,e,t){e=e||[],t=t||0;for(var n=t;r>=zt;)e[t++]=r&255|De,r/=128;for(;r&Nt;)e[t++]=r&255|De,r>>>=7;return e[t]=r|0,ze.bytes=t-n+1,e}o(ze,"encode");var It=fe,Ot=128,Ne=127;function fe(r,n){var t=0,n=n||0,i=0,s=n,a,c=r.length;do{if(s>=c)throw fe.bytes=0,new RangeError("Could not decode varint");a=r[s++],t+=i<28?(a&Ne)<<i:(a&Ne)*Math.pow(2,i),i+=7}while(a>=Ot);return fe.bytes=s-n,t}o(fe,"read");var Lt=Math.pow(2,7),Pt=Math.pow(2,14),Rt=Math.pow(2,21),Kt=Math.pow(2,28),Vt=Math.pow(2,35),Ft=Math.pow(2,42),jt=Math.pow(2,49),$t=Math.pow(2,56),Gt=Math.pow(2,63),kt=o(function(r){return r<Lt?1:r<Pt?2:r<Rt?3:r<Kt?4:r<Vt?5:r<Ft?6:r<jt?7:r<$t?8:r<Gt?9:10},"length"),qt={encode:Mt,decode:It,encodingLength:kt},_t=qt,R=_t;function K(r,e=0){return[R.decode(r,e),R.decode.bytes]}o(K,"decode");function I(r,e,t=0){return R.encode(r,e,t),e}o(I,"encodeTo");function O(r){return R.encodingLength(r)}o(O,"encodingLength");function T(r,e){let t=e.byteLength,n=O(r),i=n+O(t),s=new Uint8Array(i+t);return I(r,s,0),I(t,s,n),s.set(e,i),new L(r,t,e,s)}o(T,"create");function Ie(r){let e=E(r),[t,n]=K(e),[i,s]=K(e.subarray(n)),a=e.subarray(n+s);if(a.byteLength!==i)throw new Error("Incorrect length");return new L(t,i,a,e)}o(Ie,"decode");function Oe(r,e){if(r===e)return!0;{let t=e;return r.code===t.code&&r.size===t.size&&t.bytes instanceof Uint8Array&&Ce(r.bytes,t.bytes)}}o(Oe,"equals");var L=class{static{o(this,"Digest")}code;size;digest;bytes;constructor(e,t,n,i){this.code=e,this.size=t,this.digest=n,this.bytes=i}};var Le=0,Jt="identity",Pe=E;function Xt(r){return T(Le,Pe(r))}o(Xt,"digest");var Yt={code:Le,name:Jt,encode:Pe,digest:Xt};var he={};m(he,{sha256:()=>Qt,sha512:()=>Ht});function de({name:r,code:e,encode:t}){return new ue(r,e,t)}o(de,"from");var ue=class{static{o(this,"Hasher")}name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){let t=this.encode(e);return t instanceof Uint8Array?T(this.code,t):t.then(n=>T(this.code,n))}else throw Error("Unknown type, must be binary type")}};function Ke(r){return async e=>new Uint8Array(await crypto.subtle.digest(r,e))}o(Ke,"sha");var Qt=de({name:"sha2-256",code:18,encode:Ke("SHA-256")}),Ht=de({name:"sha2-512",code:19,encode:Ke("SHA-512")});function Ve(r,e){let{bytes:t,version:n}=r;switch(n){case 0:return Zt(t,le(r),e??v.encoder);default:return er(t,le(r),e??z.encoder)}}o(Ve,"format");var Fe=new WeakMap;function le(r){let e=Fe.get(r);if(e==null){let t=new Map;return Fe.set(r,t),t}return e}o(le,"baseCache");var J=class r{static{o(this,"CID")}code;version;multihash;bytes;"/";constructor(e,t,n,i){this.code=t,this.version=e,this.multihash=n,this.bytes=i,this["/"]=i}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:t}=this;if(e!==F)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(t.code!==tr)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return r.createV0(t)}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:t}=this.multihash,n=T(e,t);return r.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 r.equals(this,e)}static equals(e,t){let n=t;return n!=null&&e.code===n.code&&e.version===n.version&&Oe(e.multihash,n.multihash)}toString(e){return Ve(this,e)}toJSON(){return{"/":Ve(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 t=e;if(t instanceof r)return t;if(t["/"]!=null&&t["/"]===t.bytes||t.asCID===t){let{version:n,code:i,multihash:s,bytes:a}=t;return new r(n,i,s,a??je(n,i,s.bytes))}else if(t[rr]===!0){let{version:n,multihash:i,code:s}=t,a=Ie(i);return r.create(n,s,a)}else return null}static create(e,t,n){if(typeof t!="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(t!==F)throw new Error(`Version 0 CID must use dag-pb (code: ${F}) block encoding`);return new r(e,t,n,n.bytes)}case 1:{let i=je(e,t,n.bytes);return new r(e,t,n,i)}default:throw new Error("Invalid version")}}static createV0(e){return r.create(0,F,e)}static createV1(e,t){return r.create(1,e,t)}static decode(e){let[t,n]=r.decodeFirst(e);if(n.length!==0)throw new Error("Incorrect length");return t}static decodeFirst(e){let t=r.inspectBytes(e),n=t.size-t.multihashSize,i=E(e.subarray(n,n+t.multihashSize));if(i.byteLength!==t.multihashSize)throw new Error("Incorrect length");let s=i.subarray(t.multihashSize-t.digestSize),a=new L(t.multihashCode,t.digestSize,s,i);return[t.version===0?r.createV0(a):r.createV1(t.codec,a),e.subarray(t.size)]}static inspectBytes(e){let t=0,n=o(()=>{let[D,G]=K(e.subarray(t));return t+=G,D},"next"),i=n(),s=F;if(i===18?(i=0,t=0):s=n(),i!==0&&i!==1)throw new RangeError(`Invalid CID version ${i}`);let a=t,c=n(),u=n(),M=t+u,h=M-a;return{version:i,codec:s,multihashCode:c,digestSize:u,multihashSize:h,size:M}}static parse(e,t){let[n,i]=Wt(e,t),s=r.decode(i);if(s.version===0&&e[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return le(s).set(n,e),s}};function Wt(r,e){switch(r[0]){case"Q":{let t=e??v;return[v.prefix,t.decode(`${v.prefix}${r}`)]}case v.prefix:{let t=e??v;return[v.prefix,t.decode(r)]}case z.prefix:{let t=e??z;return[z.prefix,t.decode(r)]}default:{if(e==null)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[r[0],e.decode(r)]}}}o(Wt,"parseCIDtoBytes");function Zt(r,e,t){let{prefix:n}=t;if(n!==v.prefix)throw Error(`Cannot string encode V0 in ${t.name} encoding`);let i=e.get(n);if(i==null){let s=t.encode(r).slice(1);return e.set(n,s),s}else return i}o(Zt,"toStringV0");function er(r,e,t){let{prefix:n}=t,i=e.get(n);if(i==null){let s=t.encode(r);return e.set(n,s),s}else return i}o(er,"toStringV1");var F=112,tr=18;function je(r,e,t){let n=O(r),i=n+O(e),s=new Uint8Array(i+t.byteLength);return I(r,s,0),I(e,s,n),s.set(t,i),s}o(je,"encodeCID");var rr=Symbol.for("@ipld/js-cid/CID");var me={...ce,...te,...ae,...Z,...ee,...ne,...oe,...ie,...se,...re},zn={...he,...pe};function Ge(r,e,t,n){return{name:r,prefix:e,encoder:{name:r,prefix:e,encode:t},decoder:{decode:n}}}o(Ge,"createCodec");var $e=Ge("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),ye=Ge("ascii","a",r=>{let e="a";for(let t=0;t<r.length;t++)e+=String.fromCharCode(r[t]);return e},r=>{r=r.substring(1);let e=q(r.length);for(let t=0;t<r.length;t++)e[t]=r.charCodeAt(t);return e}),nr={utf8:$e,"utf-8":$e,hex:me.base16,latin1:ye,ascii:ye,binary:ye,...me},X=nr;function P(r,e="utf8"){let t=X[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return t.decoder.decode(`${t.prefix}${r}`)}o(P,"fromString");function j(r,e="utf8"){let t=X[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return t.encoder.encode(r).substring(1)}o(j,"toString");var be="base64pad",ke="base64url",$="AES-GCM";var ge=o(function(e,t){if(typeof e=="string")return t(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 xe(r,e){return P(e,r).buffer}o(xe,"base64ToArrBuf");var qe=o(r=>ge(r,e=>or(e,16)),"normalizeUtf16ToBuf");function or(r,e){let t=e===8?new Uint8Array(r.length):new Uint16Array(r.length);for(let n=0,i=r.length;n<i;n++)t[n]=r.charCodeAt(n);return t.buffer}o(or,"strToArrBuf");async function ir(r){let e=await A.subtle.exportKey("raw",r),t=new Uint8Array(e);return j(t,ke)}o(ir,"exportKey");var sr=(n=>(n[n.B128=128]="B128",n[n.B192=192]="B192",n[n.B256=256]="B256",n))(sr||{}),we=256;async function ho(r,e={length:we}){let t=await cr({length:e.length}),n=await ar(r.content,t,"AES-GCM"),i=j(n,be),s=await ir(t);return[{content:i},{key:s}]}o(ho,"encryptMessage");async function ar(r,e,t,n){let i=typeof r=="string"?P(r):r,s=n?await A.subtle.encrypt({name:t,iv:n},e,i):await fr(i,e,{alg:t});return new Uint8Array(s)}o(ar,"aesEncrypt");function cr(r){return A.subtle.generateKey({name:r?.alg||$,length:r?.length||we},!0,["encrypt","decrypt"])}o(cr,"createKey");var _e=64;async function fr(r,e,t){let n=qe(r),i=typeof e=="string"?await Ae(e,t):e,s=t?.alg||$,a=t?.iv||ur(12),c=await A.subtle.encrypt({name:s,iv:s==="AES-CTR"?void 0:a,counter:s==="AES-CTR"?new Uint8Array(a):void 0,length:s==="AES-CTR"?_e:void 0},i,n);return pr(a,c)}o(fr,"encryptBytes");function pr(r,e){let t=new Uint8Array(r),n=new Uint8Array(e),i=new Uint8Array(t.length+n.length);return i.set(t),i.set(n,t.length),i.buffer}o(pr,"joinBufs");function ur(r,{max:e}={max:255}){if(e<1||e>255)throw new Error("Max must be less than 256 and greater than 0");let t=new Uint8Array(r);if(e===255)return A.getRandomValues(t),t.buffer;let n=0,i=e+1,s=Math.floor(256/i)*i,a=new Uint8Array(1);for(;n<t.length;)A.getRandomValues(a),a[0]<s&&(t[n]=a[0]%i,n++);return t.buffer}o(ur,"randomBuf");function Ae(r,e){let t=xe("base64url",r);return A.subtle.importKey("raw",t,{name:e?.alg||$,length:e?.length||we},!0,["encrypt","decrypt"])}o(Ae,"importKey");async function lo(r,e){let t=await Ae(e),n=P(r.content,be),i=await dr(n,t,"AES-GCM");return{content:j(i)}}o(lo,"decryptMessage");async function dr(r,e,t,n){let i=n?await A.subtle.decrypt({name:t,iv:n},e,r):await hr(r,e,{alg:t});return new Uint8Array(i)}o(dr,"aesDecrypt");async function hr(r,e,t){let n=lr(r,"base64pad"),i=typeof e=="string"?await Ae(e,t):e,s=t?.alg||$,a=n.slice(0,12),c=n.slice(12);return await A.subtle.decrypt({name:s,iv:s==="AES-CTR"?void 0:a,counter:s==="AES-CTR"?new Uint8Array(a):void 0,length:s==="AES-CTR"?_e:void 0},i,c)}o(hr,"decryptBytes");function lr(r,e){return ge(r,xe.bind(null,e))}o(lr,"normalizeBase64ToBuf");export{we as DEFAULT_SYMM_LEN,sr as SymmKeyLength,dr as aesDecrypt,ar as aesEncrypt,lo as decryptMessage,fr as encryptBytes,ho as encryptMessage}; //# sourceMappingURL=index.min.js.map