UNPKG

@justinwwolcott/ez-web-crypto

Version:

class for working with webcrypto in browser and node

2 lines (1 loc) 7.5 kB
var o=e=>Uint8Array.from(atob(e),r=>r.charCodeAt(0)),y=e=>{let t=[];for(let a=0;a<e.length;a+=8192){let i=e.subarray(a,a+8192);t.push(String.fromCharCode.apply(null,[...i]))}return btoa(t.join(""))},p=async e=>{await new Promise(r=>setTimeout(r,e))};var c=async()=>{let e;if(typeof globalThis.crypto<"u")e={crypto:globalThis.crypto,CryptoKey:globalThis.CryptoKey||null};else if(typeof globalThis.require=="function"){let{webcrypto:r}=await import("crypto");e={crypto:r,CryptoKey:null}}else throw new Error("Crypto API is not available in this environment");return e};var M=async(e,r)=>{let{crypto:t}=await c(),a=new TextEncoder,i=a.encode(e),n=a.encode(r),s=await t.subtle.importKey("raw",i,{name:"HMAC",hash:{name:"SHA-256"}},!1,["sign","verify"]),l=await t.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,r,t)=>{let{crypto:a}=await c(),i=await a.subtle.digest(e,new TextEncoder().encode(r)),n=new Uint8Array(i);if(!t)return y(n);let s=new Uint8Array(t),l=Math.max(t,n.length);for(let m=0;m<l;m+=1)s[m%t]=s[m%t]^n[m%n.length];return y(s)};var v=async(e=!0)=>{await p(0);let{crypto:r}=await c(),t=await r.subtle.generateKey({name:"AES-GCM",length:256},e,["encrypt","decrypt"]);if(!e)return t;let a=await r.subtle.exportKey("raw",t);return y(new Uint8Array(a))},f=async(e,r=!0)=>{await p(0);let{crypto:t}=await c();return(i=>typeof i=="object"&&i!==null&&"type"in i&&"algorithm"in i)(e)?e:t.subtle.importKey("raw",o(e).buffer,"AES-GCM",r,["encrypt","decrypt"])},C=async(e,r,t=!1)=>{await p(0);let{crypto:a}=await c(),i=await f(e),n=t?o(t):a.getRandomValues(new Uint8Array(16)),s=await a.subtle.encrypt({name:"AES-GCM",iv:n},i,o(r));return{ciphertext:y(new Uint8Array(s)),iv:y(n)}},x=async(e,r,t,a=!1)=>{await p(0);let{crypto:i}=await c(),n=await f(e),s=o(r),l=o(t),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 B=5e3,Y=async(e,r)=>{await p(0);let t=e;for(let s=0;s<B;s+=1)t=await h("SHA-512",t);let a=await v(!0),i=btoa(t),n=await C(a,r,i);return btoa(JSON.stringify({ciphertext:n.ciphertext,aes:a}))},J=async(e,r)=>{await p(0);let t=e;for(let l=0;l<B;l+=1)t=await h("SHA-512",t);let a=btoa(t),i=JSON.parse(atob(r)),n=await f(i.aes,!1);return await x(n,a,i.ciphertext,!0)};var u=async e=>{await p(0);let{crypto:r,CryptoKey:t}=await c();if(t&&e instanceof t)return e;if(typeof e!="string")throw new Error("Invalid key format");try{return await r.subtle.importKey("spki",o(e),{name:"ECDH",namedCurve:"P-256"},!0,[])}catch{}try{return await r.subtle.importKey("raw",o(e),{name:"ECDH",namedCurve:"P-256"},!0,[])}catch{}try{return await r.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 r.subtle.importKey("raw",a,{name:"ECDH",namedCurve:"P-256"},!0,[])}catch{throw new Error("UNRECOGNIZED KEY FORMAT")}},E=async e=>{await p(0);let{crypto:r,CryptoKey:t}=await c();if(t&&e instanceof t)return e;if(typeof e!="string")throw new Error("Invalid key format");try{return await r.subtle.importKey("spki",o(e),{name:"ECDSA",namedCurve:"P-256"},!0,["verify"])}catch{}try{return await r.subtle.importKey("raw",o(e),{name:"ECDSA",namedCurve:"P-256"},!0,["verify"])}catch{}try{return await r.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 r.subtle.importKey("raw",a,{name:"ECDSA",namedCurve:"P-256"},!0,["sign"])}catch{throw new Error("UNRECOGNIZED KEY FORMAT")}};var ee=async(e=!0)=>{await p(0);let{crypto:r}=await c(),t=await r.subtle.generateKey({name:"ECDH",namedCurve:"P-256"},e,["deriveKey","deriveBits"]);if(!e){let i=await Promise.all([r.subtle.exportKey("spki",t.publicKey).then(n=>y(new Uint8Array(n))),t.privateKey,r.subtle.exportKey("raw",t.publicKey).then(n=>y(new Uint8Array(n))),r.subtle.exportKey("raw",t.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([r.subtle.exportKey("spki",t.publicKey).then(i=>y(new Uint8Array(i))),r.subtle.exportKey("pkcs8",t.privateKey).then(i=>y(new Uint8Array(i))),r.subtle.exportKey("jwk",t.publicKey),r.subtle.exportKey("jwk",t.privateKey),r.subtle.exportKey("raw",t.publicKey).then(i=>y(new Uint8Array(i))),r.subtle.exportKey("raw",t.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]}},te=async(e,r,t)=>{let{crypto:a}=await c(),[i,n]=await Promise.all([u(r),u(e)]),s=await a.subtle.deriveKey({name:"ECDH",public:i},n,{name:"AES-GCM",length:256},!1,["encrypt","decrypt"]);return C(s,t)},re=async(e,r,t,a,i=!1)=>{let{crypto:n}=await c(),[s,l]=await Promise.all([u(r),u(e)]),[m,w]=[o(t),o(a)],S=await n.subtle.deriveKey({name:"ECDH",public:s},l,{name:"AES-GCM",length:256},!1,["decrypt"]),g=await n.subtle.decrypt({name:"AES-GCM",iv:m},S,w);return i?new TextDecoder().decode(new Uint8Array(g)):g};var se=async(e,r,t,a=16,i=16)=>{await p(0);let{crypto:n}=await c(),s=await u(r),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),d=await n.subtle.importKey("raw",g,"AES-GCM",!1,["encrypt","decrypt"]),b=n.getRandomValues(new Uint8Array(a)),A=await n.subtle.encrypt({name:"AES-GCM",iv:b},d,o(t));return{ciphertext:y(new Uint8Array(A)),salt:y(S),iv:y(b)}},ye=async(e,r,t,a,i,n=!1)=>{await p(0);let{crypto:s}=await c(),l=await u(r),m=await u(e),w=o(t),S=o(a),g=o(i),d=await s.subtle.deriveBits({name:"ECDH",public:l},m,256),b=await s.subtle.importKey("raw",d,{name:"HKDF"},!1,["deriveKey","deriveBits"]),A=await s.subtle.deriveBits({name:"HKDF",hash:"SHA-256",salt:w,info:new Uint8Array([])},b,256),P=await s.subtle.importKey("raw",A,"AES-GCM",!1,["encrypt","decrypt"]);try{let K=await s.subtle.decrypt({name:"AES-GCM",iv:S},P,g);if(!n)return K;let D=new Uint8Array(K);return new TextDecoder().decode(D)}catch(K){throw console.log({name:K.name,stack:K.stack,message:K.message}),K}};var ue=async(e=!0)=>{await p(0);let{crypto:r}=await c(),t=await r.subtle.generateKey({name:"ECDSA",namedCurve:"P-256"},e,["sign","verify"]);if(e){let i=await Promise.all([r.subtle.exportKey("spki",t.publicKey).then(n=>y(new Uint8Array(n))),r.subtle.exportKey("pkcs8",t.privateKey).then(n=>y(new Uint8Array(n)))]);return{publicKey:i[0],privateKey:i[1]}}return{publicKey:(await Promise.all([r.subtle.exportKey("spki",t.publicKey).then(i=>y(new Uint8Array(i)))]))[0],privateKey:t.privateKey}},we=async(e,r)=>{await p(0);let t=await E(e),a=await crypto.subtle.sign({name:"ECDSA",hash:{name:"SHA-256"}},t,o(r));return y(new Uint8Array(a))},Ke=async(e,r,t)=>{await p(0);let a=await E(e),i=o(r);return crypto.subtle.verify({name:"ECDSA",hash:{name:"SHA-256"}},a,i,o(t))};export{x as AESDecrypt,C as AESEncrypt,f as AESImportKey,v as AESMakeKey,re as EcDecrypt,te as EcEncrypt,ee as EcMakeCryptKeys,ue as EcMakeSigKeys,we as EcSignData,Ke as EcVerifySig,u as EcdhConvertKey,E as EcdsaConvertKey,h as HASH,ye as HKDFDecrypt,se as HKDFEncrypt,M as HMAC,J as PASSWORD_DECRYPT,Y as PASSWORD_ENCRYPT,y as arrayToBase64,o as base64ToArray,p as sleep};