var T=Object.create;var A=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var G=Object.getPrototypeOf,z=Object.prototype.hasOwnProperty;var F=(e,t)=>{for(var r in t)A(e,r,{get:t[r],enumerable:!0})},P=(e,t,r,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of R(t))!z.call(e,i)&&i!==r&&A(e,i,{get:()=>t[i],enumerable:!(a=M(t,i))||a.enumerable});return e};var k=(e,t,r)=>(r=e!=null?T(G(e)):{},P(t||!e||!e.__esModule?A(r,"default",{value:e,enumerable:!0}):r,e)),O=e=>P(A({},"__esModule",{value:!0}),e);var Q={};F(Q,{AESDecrypt:()=>B,AESEncrypt:()=>C,AESImportKey:()=>f,AESMakeKey:()=>x,EcDecrypt:()=>J,EcEncrypt:()=>Y,EcMakeCryptKeys:()=>V,EcMakeSigKeys:()=>Z,EcSignData:()=>q,EcVerifySig:()=>$,EcdhConvertKey:()=>u,EcdsaConvertKey:()=>E,HASH:()=>h,HKDFDecrypt:()=>W,HKDFEncrypt:()=>L,HMAC:()=>j,PASSWORD_DECRYPT:()=>N,PASSWORD_ENCRYPT:()=>I,arrayToBase64:()=>y,base64ToArray:()=>o,sleep:()=>c});module.exports=O(Q);var o=e=>Uint8Array.from(atob(e),t=>t.charCodeAt(0)),y=e=>{let r=[];for(let a=0;a{await new Promise(t=>setTimeout(t,e))};var p=async()=>{let e;if(typeof globalThis.crypto<"u")e={crypto:globalThis.crypto,CryptoKey:globalThis.CryptoKey||null};else if(typeof globalThis.require=="function"){let{webcrypto:t}=await import("crypto");e={crypto:t,CryptoKey:null}}else throw new Error("Crypto API is not available in this environment");return e};var j=async(e,t)=>{let{crypto:r}=await p(),a=new TextEncoder,i=a.encode(e),n=a.encode(t),s=await r.subtle.importKey("raw",i,{name:"HMAC",hash:{name:"SHA-256"}},!1,["sign","verify"]),l=await r.subtle.sign("HMAC",s,n),m=new Uint8Array(l);return Array.prototype.map.call(m,w=>`00${w.toString(16)}`.slice(-2)).join("")};var h=async(e,t,r)=>{let{crypto:a}=await p(),i=await a.subtle.digest(e,new TextEncoder().encode(t)),n=new Uint8Array(i);if(!r)return y(n);let s=new Uint8Array(r),l=Math.max(r,n.length);for(let m=0;m{await c(0);let{crypto:t}=await p(),r=await t.subtle.generateKey({name:"AES-GCM",length:256},e,["encrypt","decrypt"]);if(!e)return r;let a=await t.subtle.exportKey("raw",r);return y(new Uint8Array(a))},f=async(e,t=!0)=>{await c(0);let{crypto:r}=await p();return(i=>typeof i=="object"&&i!==null&&"type"in i&&"algorithm"in i)(e)?e:r.subtle.importKey("raw",o(e).buffer,"AES-GCM",t,["encrypt","decrypt"])},C=async(e,t,r=!1)=>{await c(0);let{crypto:a}=await p(),i=await f(e),n=r?o(r):a.getRandomValues(new Uint8Array(16)),s=await a.subtle.encrypt({name:"AES-GCM",iv:n},i,o(t));return{ciphertext:y(new Uint8Array(s)),iv:y(n)}},B=async(e,t,r,a=!1)=>{await c(0);let{crypto:i}=await p(),n=await f(e),s=o(t),l=o(r),m=await i.subtle.decrypt({name:"AES-GCM",iv:s},n,l);if(!a)return m;let w=new Uint8Array(m);return new TextDecoder().decode(w)};var D=5e3,I=async(e,t)=>{await c(0);let r=e;for(let s=0;s{await c(0);let r=e;for(let l=0;l{await c(0);let{crypto:t,CryptoKey:r}=await p();if(r&&e instanceof r)return e;if(typeof e!="string")throw new Error("Invalid key format");try{return await t.subtle.importKey("spki",o(e),{name:"ECDH",namedCurve:"P-256"},!0,[])}catch{}try{return await t.subtle.importKey("raw",o(e),{name:"ECDH",namedCurve:"P-256"},!0,[])}catch{}try{return await t.subtle.importKey("pkcs8",o(e),{name:"ECDH",namedCurve:"P-256"},!1,["deriveKey","deriveBits"])}catch{}try{let a=new Uint8Array([4,...Array.from(o(e))]);return await t.subtle.importKey("raw",a,{name:"ECDH",namedCurve:"P-256"},!0,[])}catch{throw new Error("UNRECOGNIZED KEY FORMAT")}},E=async e=>{await c(0);let{crypto:t,CryptoKey:r}=await p();if(r&&e instanceof r)return e;if(typeof e!="string")throw new Error("Invalid key format");try{return await t.subtle.importKey("spki",o(e),{name:"ECDSA",namedCurve:"P-256"},!0,["verify"])}catch{}try{return await t.subtle.importKey("raw",o(e),{name:"ECDSA",namedCurve:"P-256"},!0,["verify"])}catch{}try{return await t.subtle.importKey("pkcs8",o(e),{name:"ECDSA",namedCurve:"P-256"},!1,["sign"])}catch{}try{let a=new Uint8Array([4,...Array.from(o(e))]);return await t.subtle.importKey("raw",a,{name:"ECDSA",namedCurve:"P-256"},!0,["sign"])}catch{throw new Error("UNRECOGNIZED KEY FORMAT")}};var V=async(e=!0)=>{await c(0);let{crypto:t}=await p(),r=await t.subtle.generateKey({name:"ECDH",namedCurve:"P-256"},e,["deriveKey","deriveBits"]);if(!e){let i=await Promise.all([t.subtle.exportKey("spki",r.publicKey).then(n=>y(new Uint8Array(n))),r.privateKey,t.subtle.exportKey("raw",r.publicKey).then(n=>y(new Uint8Array(n))),t.subtle.exportKey("raw",r.publicKey).then(n=>y(new Uint8Array(n).slice(1,1e3)))]);return{publicKey:i[0],privateKey:i[1],rawPublicKey:i[2],rawPublicKeyLite:i[3]}}let a=await Promise.all([t.subtle.exportKey("spki",r.publicKey).then(i=>y(new Uint8Array(i))),t.subtle.exportKey("pkcs8",r.privateKey).then(i=>y(new Uint8Array(i))),t.subtle.exportKey("jwk",r.publicKey),t.subtle.exportKey("jwk",r.privateKey),t.subtle.exportKey("raw",r.publicKey).then(i=>y(new Uint8Array(i))),t.subtle.exportKey("raw",r.publicKey).then(i=>y(new Uint8Array(i).slice(1,1e3)))]);return{publicKey:a[0],privateKey:a[1],jwkPublicKey:a[2],jwkPrivateKey:a[3],rawPublicKey:a[4],rawPublicKeyLite:a[5]}},Y=async(e,t,r)=>{await c(0);let{crypto:a}=await p(),i=await u(t),n=await u(e),s=await a.subtle.deriveKey({name:"ECDH",public:i},n,{name:"AES-GCM",length:256},!1,["encrypt","decrypt"]);return C(s,r)},J=async(e,t,r,a,i=!1)=>{let{crypto:n}=await p(),s=await u(t),l=await u(e),m=o(r),w=o(a),S=await n.subtle.deriveKey({name:"ECDH",public:s},l,{name:"AES-GCM",length:256},!1,["encrypt","decrypt"]),g=await n.subtle.decrypt({name:"AES-GCM",iv:m},S,w);if(!i)return g;let b=new Uint8Array(g);return new TextDecoder().decode(b)};var L=async(e,t,r,a=16,i=16)=>{await c(0);let{crypto:n}=await p(),s=await u(t),l=await u(e),m=await n.subtle.deriveBits({name:"ECDH",public:s},l,256),w=await n.subtle.importKey("raw",m,{name:"HKDF"},!1,["deriveKey","deriveBits"]),S=n.getRandomValues(new Uint8Array(i)),g=await n.subtle.deriveBits({name:"HKDF",hash:"SHA-256",salt:S,info:new Uint8Array([])},w,256),b=await n.subtle.importKey("raw",g,"AES-GCM",!1,["encrypt","decrypt"]),d=n.getRandomValues(new Uint8Array(a)),v=await n.subtle.encrypt({name:"AES-GCM",iv:d},b,o(r));return{ciphertext:y(new Uint8Array(v)),salt:y(S),iv:y(d)}},W=async(e,t,r,a,i,n=!1)=>{await c(0);let{crypto:s}=await p(),l=await u(t),m=await u(e),w=o(r),S=o(a),g=o(i),b=await s.subtle.deriveBits({name:"ECDH",public:l},m,256),d=await s.subtle.importKey("raw",b,{name:"HKDF"},!1,["deriveKey","deriveBits"]),v=await s.subtle.deriveBits({name:"HKDF",hash:"SHA-256",salt:w,info:new Uint8Array([])},d,256),H=await s.subtle.importKey("raw",v,"AES-GCM",!1,["encrypt","decrypt"]);try{let K=await s.subtle.decrypt({name:"AES-GCM",iv:S},H,g);if(!n)return K;let U=new Uint8Array(K);return new TextDecoder().decode(U)}catch(K){throw console.log({name:K.name,stack:K.stack,message:K.message}),K}};var Z=async(e=!0)=>{await c(0);let{crypto:t}=await p(),r=await t.subtle.generateKey({name:"ECDSA",namedCurve:"P-256"},e,["sign","verify"]);if(e){let i=await Promise.all([t.subtle.exportKey("spki",r.publicKey).then(n=>y(new Uint8Array(n))),t.subtle.exportKey("pkcs8",r.privateKey).then(n=>y(new Uint8Array(n)))]);return{publicKey:i[0],privateKey:i[1]}}return{publicKey:(await Promise.all([t.subtle.exportKey("spki",r.publicKey).then(i=>y(new Uint8Array(i)))]))[0],privateKey:r.privateKey}},q=async(e,t)=>{await c(0);let r=await E(e),a=await crypto.subtle.sign({name:"ECDSA",hash:{name:"SHA-256"}},r,o(t));return y(new Uint8Array(a))},$=async(e,t,r)=>{await c(0);let a=await E(e),i=o(t);return crypto.subtle.verify({name:"ECDSA",hash:{name:"SHA-256"}},a,i,o(r))};0&&(module.exports={AESDecrypt,AESEncrypt,AESImportKey,AESMakeKey,EcDecrypt,EcEncrypt,EcMakeCryptKeys,EcMakeSigKeys,EcSignData,EcVerifySig,EcdhConvertKey,EcdsaConvertKey,HASH,HKDFDecrypt,HKDFEncrypt,HMAC,PASSWORD_DECRYPT,PASSWORD_ENCRYPT,arrayToBase64,base64ToArray,sleep});