-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
1 lines (1 loc) · 2.29 KB
/
index.js
1
(()=>{"use strict";var e={n:t=>{var r=t&&t.__esModule?()=>t.default:()=>t;return e.d(r,{a:r}),r},d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Cryptor:()=>a,decrypt:()=>f,encrypt:()=>u,hash:()=>c});const r=require("crypto");var n=e.n(r);const o="aes-256-gcm";function a(e){if(!e||"string"!=typeof e)throw new Error("Cryptor: secret must be a non-empty string");this.encrypt=e=>{i(e);const r=n().randomBytes(16),a=n().randomBytes(64),c=t(a),s=n().createCipheriv(o,c,r),u=Buffer.concat([s.update(String(e),"utf8"),s.final()]),f=s.getAuthTag();return Buffer.concat([a,r,f,u]).toString("hex")},this.decrypt=e=>{i(e);const u=Buffer.from(String(e),"hex"),f=r(u),l=a(u),d=c(u),h=s(u),g=t(f),p=n().createDecipheriv(o,g,l);return p.setAuthTag(d),p.update(h,"base64","utf8")+p.final("utf8")};const t=t=>n().pbkdf2Sync(e,t,1e5,32,"sha512"),r=e=>e.slice(0,64),a=e=>e.slice(64,80),c=e=>e.slice(80,96),s=e=>e.slice(96),i=e=>{if(!e)throw new Error("Cryptor: value must not be null or undefined");return!0}}const c=(e,t="sha512")=>n().createHash(t).update(e).digest("base64"),s=(e,t=16)=>{let r=e;const n=t-e.length%t,o=String.fromCharCode(n);for(let e=0;e<n;e++)r+=o;return r},i="aes-256-gcm",u=(e,t,r="base64")=>{const o=d(),a=h(),c=l(t,o),u=n().createCipheriv(i,c,a),f=Buffer.concat([u.update(String(s(e)),"utf8"),u.final()]),g=u.getAuthTag();return Buffer.concat([o,a,g,f]).toString(r)},f=(e,t,r="base64")=>{const o=Buffer.from(String(e),r),a=o.slice(0,64),c=o.slice(64,80),s=o.slice(80,96),u=o.slice(96),f=l(t,a),d=n().createDecipheriv(i,f,c);d.setAuthTag(s);let h=d.update(u,"base64","utf8");try{h+=d.final("utf8")}catch(e){console.log("Warning: Failed to authenticate data")}return(e=>{let t=e;const r=e.charCodeAt(e.length-1);let n,o;for(n=e.length-2,o=e.length-r;n>=o;n--)if(e.charCodeAt(n)!==r)throw o=e.length,new Error("unpad(): found a padding byte of "+e.charCodeAt(n)+" instead of "+r+" at position "+n);return t=e.substring(0,o),t})(h)},l=(e,t)=>n().pbkdf2Sync(e,t,1e5,32,"sha512"),d=()=>n().randomBytes(64),h=()=>n().randomBytes(16);module.exports=t})();