@lowerdeck/encryption 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +13 -0
- package/dist/base86.d.ts +5 -0
- package/dist/base86.d.ts.map +1 -0
- package/dist/crypto.d.ts +6 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.module.js +2 -0
- package/dist/index.module.js.map +1 -0
- package/dist/index.umd.js +2 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +1 -1
- package/src/crypto.ts +1 -1
- package/src/index.ts +1 -1
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
[0m[2m[35m$[0m [2m[1mmicrobundle[0m
|
|
3
|
+
No name was provided for external module '@lowerdeck/id' in output.globals – guessing 'id'
|
|
4
|
+
No name was provided for external module '@lowerdeck/base62' in output.globals – guessing 'base62'
|
|
5
|
+
[34mBuild "@lowerdeck/encryption" to dist:[39m
|
|
6
|
+
[32m878 B[39m: [37mindex.cjs[39m.gz
|
|
7
|
+
[32m788 B[39m: [37mindex.cjs[39m.br
|
|
8
|
+
[32m730 B[39m: [37mindex.module.js[39m.gz
|
|
9
|
+
[32m672 B[39m: [37mindex.module.js[39m.br
|
|
10
|
+
[32m849 B[39m: [37mindex.module.js[39m.gz
|
|
11
|
+
[32m767 B[39m: [37mindex.module.js[39m.br
|
|
12
|
+
[32m964 B[39m: [37mindex.umd.js[39m.gz
|
|
13
|
+
[32m862 B[39m: [37mindex.umd.js[39m.br
|
package/dist/base86.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base86.d.ts","sourceRoot":"","sources":["../src/base86.ts"],"names":[],"mappings":"AAMA,eAAO,IAAI,MAAM;oBACC,MAAM,GAAG,UAAU;oBAEnB,MAAM;CACvB,CAAC"}
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAkEA,eAAO,IAAI,aAAa;0BA5Ca,MAAM,YAAY,MAAM;6BAqBrB,MAAM,YAAY,MAAM;mBAkBtC,MAAM;CAS/B,CAAC"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=require("@lowerdeck/id"),r=require("@lowerdeck/base62");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=/*#__PURE__*/t(require("base-x")).default("!#$%()*+-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~§"),o=new TextEncoder,c=new TextDecoder,i=function(e){return crypto.subtle.importKey("raw",o.encode(e),"PBKDF2",!1,["deriveKey"])},s=function(e,r){return crypto.subtle.deriveKey({name:"PBKDF2",iterations:25e4,hash:"SHA-256"},e,{name:"AES-GCM",length:256},!1,r)},u={encrypt:function(e,r){try{var t=crypto.getRandomValues(new Uint8Array(12));return Promise.resolve(i(r)).then(function(r){return Promise.resolve(s(r,["encrypt"])).then(function(r){return Promise.resolve(crypto.subtle.encrypt({name:"AES-GCM",iv:t},r,o.encode(e))).then(function(e){var r,o=new Uint8Array(e),c=new Uint8Array(t.byteLength+o.byteLength);return c.set(t,0),c.set(o,t.byteLength),n.encode("string"==typeof(r=c)?(new TextEncoder).encode(r):r)})})})}catch(e){return Promise.reject(e)}},decrypt:function(e,r){try{var t=n.decode(e),o=t.slice(0,16),u=t.slice(16);return Promise.resolve(i(r)).then(function(e){return Promise.resolve(s(e,["decrypt"])).then(function(e){return Promise.resolve(crypto.subtle.decrypt({name:"AES-GCM",iv:o},e,u)).then(function(e){return c.decode(e)})})})}catch(e){return Promise.reject(e)}},sha512:function(e){try{return Promise.resolve(crypto.subtle.digest("SHA-512",o.encode(e))).then(function(e){return r.base62.encode(new Uint8Array(e))})}catch(e){return Promise.reject(e)}}};exports.Encryption=/*#__PURE__*/function(){function r(e){this.password=void 0,this.password=e}var t=r.prototype;return t.getPassword=function(e){try{return Promise.resolve(u.sha512(""+e+this.password)).then(function(e){return e.slice(0,50)})}catch(e){return Promise.reject(e)}},t.encrypt=function(r){try{var t=u.encrypt,n=JSON.stringify({id:e.generatePlainId(10),key:r.secret});return Promise.resolve(this.getPassword(r.entityId)).then(function(e){return Promise.resolve(t.call(u,n,e))})}catch(e){return Promise.reject(e)}},t.decrypt=function(e){try{var r=u.decrypt,t=e.encrypted;return Promise.resolve(this.getPassword(e.entityId)).then(function(e){return Promise.resolve(r.call(u,t,e)).then(function(e){return JSON.parse(e).key})})}catch(e){return Promise.reject(e)}},r}();
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/base86.ts","../src/crypto.ts","../src/index.ts"],"sourcesContent":["import baseX from 'base-x';\n\nlet internal = baseX(\n '!#$%()*+-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~§'\n);\n\nexport let base86 = {\n encode: (input: string | Uint8Array) =>\n internal.encode(typeof input == 'string' ? new TextEncoder().encode(input) : input),\n decode: (input: string) => internal.decode(input)\n};\n","import { base62 } from '@lowerdeck/base62';\nimport { base86 } from './base86';\n\nlet enc = new TextEncoder();\nlet dec = new TextDecoder();\n\nlet getPasswordKey = (password: string) =>\n crypto.subtle.importKey('raw', enc.encode(password), 'PBKDF2', false, ['deriveKey']);\n\nlet deriveKey = (passwordKey: CryptoKey, keyUsage: ('encrypt' | 'decrypt')[]) =>\n crypto.subtle.deriveKey(\n {\n name: 'PBKDF2',\n iterations: 250000,\n hash: 'SHA-256'\n },\n passwordKey,\n { name: 'AES-GCM', length: 256 },\n false,\n keyUsage\n );\n\nlet encryptData = async (secretData: string, password: string) => {\n let iv = crypto.getRandomValues(new Uint8Array(12));\n let passwordKey = await getPasswordKey(password);\n let aesKey = await deriveKey(passwordKey, ['encrypt']);\n let encryptedContent = await crypto.subtle.encrypt(\n {\n name: 'AES-GCM',\n iv\n },\n aesKey,\n enc.encode(secretData)\n );\n\n let encryptedContentArr = new Uint8Array(encryptedContent);\n let buff = new Uint8Array(iv.byteLength + encryptedContentArr.byteLength);\n buff.set(iv, 0);\n buff.set(encryptedContentArr, iv.byteLength);\n\n return base86.encode(buff);\n};\n\nlet decryptData = async (encryptedData: string, password: string) => {\n let encryptedDataBuff = base86.decode(encryptedData);\n let iv = encryptedDataBuff.slice(0, 16);\n let data = encryptedDataBuff.slice(16);\n let passwordKey = await getPasswordKey(password);\n let aesKey = await deriveKey(passwordKey, ['decrypt']);\n let decryptedContent = await crypto.subtle.decrypt(\n {\n name: 'AES-GCM',\n iv\n },\n aesKey,\n data\n );\n\n return dec.decode(decryptedContent);\n};\n\nlet sha512 = async (data: string) => {\n let hashBuffer = await crypto.subtle.digest('SHA-512', enc.encode(data));\n return base62.encode(new Uint8Array(hashBuffer));\n};\n\nexport let secretsCrypto = {\n encrypt: encryptData,\n decrypt: decryptData,\n sha512\n};\n","import { generatePlainId } from '@lowerdeck/id';\nimport { secretsCrypto } from './crypto';\n\nexport class Encryption {\n constructor(private readonly password: string) {}\n\n private async getPassword(entityId: string) {\n return (await secretsCrypto.sha512(`${entityId}${this.password!}`)).slice(0, 50);\n }\n\n async encrypt(input: { secret: string; entityId: string }) {\n return await secretsCrypto.encrypt(\n JSON.stringify({\n id: generatePlainId(10),\n key: input.secret\n }),\n await this.getPassword(input.entityId)\n );\n }\n\n async decrypt(info: { encrypted: string; entityId: string }) {\n let content = JSON.parse(\n await secretsCrypto.decrypt(info.encrypted, await this.getPassword(info.entityId))\n );\n\n return content.key;\n }\n}\n"],"names":["internal","baseX","enc","TextEncoder","dec","TextDecoder","getPasswordKey","password","crypto","subtle","importKey","encode","deriveKey","passwordKey","keyUsage","name","iterations","hash","length","secretsCrypto","encrypt","secretData","iv","getRandomValues","Uint8Array","Promise","resolve","then","aesKey","encryptedContent","input","encryptedContentArr","buff","byteLength","set","e","reject","decrypt","encryptedData","encryptedDataBuff","decode","slice","data","decryptedContent","sha512","digest","hashBuffer","base62","Encryption","this","_proto","prototype","getPassword","entityId","_secretsCrypto$sha","_this2","_encrypt","_JSON$stringify","JSON","stringify","id","generatePlainId","key","secret","_this2$getPassword","call","info","_this3","_decrypt","_info$encrypted","encrypted","_this3$getPassword","_secretsCrypto$decryp","parse"],"mappings":"0IAEIA,oCAAWC,QACb,0FCAEC,EAAM,IAAIC,YACVC,EAAM,IAAIC,YAEVC,EAAiB,SAACC,GACpB,OAAAC,OAAOC,OAAOC,UAAU,MAAOR,EAAIS,OAAOJ,GAAW,UAAU,EAAO,CAAC,aAAa,EAElFK,EAAY,SAACC,EAAwBC,UACvCN,OAAOC,OAAOG,UACZ,CACEG,KAAM,SACNC,WAAY,KACZC,KAAM,WAERJ,EACA,CAAEE,KAAM,UAAWG,OAAQ,MAC3B,EACAJ,EACD,EA8CQK,EAAgB,CACzBC,QA7Ca,SAAUC,EAAoBd,GAAgB,IAC3D,IAAIe,EAAKd,OAAOe,gBAAgB,IAAIC,WAAW,KAAK,OAAAC,QAAAC,QAC5BpB,EAAeC,IAASoB,KAAA,SAA5Cd,GAAW,OAAAY,QAAAC,QACId,EAAUC,EAAa,CAAC,aAAWc,KAAA,SAAlDC,GAAM,OAAAH,QAAAC,QACmBlB,OAAOC,OAAOW,QACzC,CACEL,KAAM,UACNO,GAAAA,GAEFM,EACA1B,EAAIS,OAAOU,KACZM,KAAA,SAPGE,GASJ,ID5BSC,EC4BLC,EAAsB,IAAIP,WAAWK,GACrCG,EAAO,IAAIR,WAAWF,EAAGW,WAAaF,EAAoBE,YAI9D,OAHAD,EAAKE,IAAIZ,EAAI,GACbU,EAAKE,IAAIH,EAAqBT,EAAGW,YD9B/BjC,EA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EAEK9B,QAAO,SAACU,GAA2C,IAAAyB,IAM3CC,EALCrC,EAAcC,QAAOqC,EAChCC,KAAKC,UAAU,CACbC,GAAIC,EAAeA,gBAAC,IACpBC,IAAKhC,EAAMiC,SACX,OAAAtC,QAAAC,QACIuB,KAAKG,YAAYtB,EAAMuB,WAAS1B,KAAAqC,SAAAA,GAAAvC,OAAAA,QAAAC,QAAA8B,EAAAS,KAL3B9C,EAAasC,EAAAO,GAO5B,EAAA,CAAC,MAAA7B,GAAAV,OAAAA,QAAAW,OAAAD,EAAAe,CAAAA,EAAAA,EAEKb,QAAA,SAAQ6B,GAA6C,IAAAC,IAEDC,EAAhDjD,EAAckB,QAAOgC,EAACH,EAAKI,UAAS,OAAA7C,QAAAC,QAAQuB,KAAKG,YAAYc,EAAKb,WAAS1B,KAAA4C,SAAAA,GAAA9C,OAAAA,QAAAC,QAAA0C,EAAAH,KAA3E9C,EAAakD,EAAAE,IAAA5C,KAAA,SAAA6C,GAGrB,OAJcd,KAAKe,MAAKD,GAITV,GAAI,EACrB,EAAA,CAAC,MAAA3B,GAAAV,OAAAA,QAAAW,OAAAD,KAAAa,CAAA,CAtBD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class Encryption {
|
|
2
|
+
private readonly password;
|
|
3
|
+
constructor(password: string);
|
|
4
|
+
private getPassword;
|
|
5
|
+
encrypt(input: {
|
|
6
|
+
secret: string;
|
|
7
|
+
entityId: string;
|
|
8
|
+
}): Promise<string>;
|
|
9
|
+
decrypt(info: {
|
|
10
|
+
encrypted: string;
|
|
11
|
+
entityId: string;
|
|
12
|
+
}): Promise<any>;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,qBAAa,UAAU;IACT,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,MAAM;YAE/B,WAAW;IAInB,OAAO,CAAC,KAAK,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE;IAUnD,OAAO,CAAC,IAAI,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE;CAO5D"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{generatePlainId as e}from"@lowerdeck/id";import{base62 as r}from"@lowerdeck/base62";import t from"base-x";var n=t("!#$%()*+-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~§"),o=new TextEncoder,c=new TextDecoder,i=function(e){return crypto.subtle.importKey("raw",o.encode(e),"PBKDF2",!1,["deriveKey"])},s=function(e,r){return crypto.subtle.deriveKey({name:"PBKDF2",iterations:25e4,hash:"SHA-256"},e,{name:"AES-GCM",length:256},!1,r)},u={encrypt:function(e,r){try{var t=crypto.getRandomValues(new Uint8Array(12));return Promise.resolve(i(r)).then(function(r){return Promise.resolve(s(r,["encrypt"])).then(function(r){return Promise.resolve(crypto.subtle.encrypt({name:"AES-GCM",iv:t},r,o.encode(e))).then(function(e){var r,o=new Uint8Array(e),c=new Uint8Array(t.byteLength+o.byteLength);return c.set(t,0),c.set(o,t.byteLength),n.encode("string"==typeof(r=c)?(new TextEncoder).encode(r):r)})})})}catch(e){return Promise.reject(e)}},decrypt:function(e,r){try{var t=n.decode(e),o=t.slice(0,16),u=t.slice(16);return Promise.resolve(i(r)).then(function(e){return Promise.resolve(s(e,["decrypt"])).then(function(e){return Promise.resolve(crypto.subtle.decrypt({name:"AES-GCM",iv:o},e,u)).then(function(e){return c.decode(e)})})})}catch(e){return Promise.reject(e)}},sha512:function(e){try{return Promise.resolve(crypto.subtle.digest("SHA-512",o.encode(e))).then(function(e){return r.encode(new Uint8Array(e))})}catch(e){return Promise.reject(e)}}},y=/*#__PURE__*/function(){function r(e){this.password=void 0,this.password=e}var t=r.prototype;return t.getPassword=function(e){try{return Promise.resolve(u.sha512(""+e+this.password)).then(function(e){return e.slice(0,50)})}catch(e){return Promise.reject(e)}},t.encrypt=function(r){try{var t=u.encrypt,n=JSON.stringify({id:e(10),key:r.secret});return Promise.resolve(this.getPassword(r.entityId)).then(function(e){return Promise.resolve(t.call(u,n,e))})}catch(e){return Promise.reject(e)}},t.decrypt=function(e){try{var r=u.decrypt,t=e.encrypted;return Promise.resolve(this.getPassword(e.entityId)).then(function(e){return Promise.resolve(r.call(u,t,e)).then(function(e){return JSON.parse(e).key})})}catch(e){return Promise.reject(e)}},r}();export{y as Encryption};
|
|
2
|
+
//# sourceMappingURL=index.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.module.js","sources":["../src/base86.ts","../src/crypto.ts","../src/index.ts"],"sourcesContent":["import baseX from 'base-x';\n\nlet internal = baseX(\n '!#$%()*+-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~§'\n);\n\nexport let base86 = {\n encode: (input: string | Uint8Array) =>\n internal.encode(typeof input == 'string' ? new TextEncoder().encode(input) : input),\n decode: (input: string) => internal.decode(input)\n};\n","import { base62 } from '@lowerdeck/base62';\nimport { base86 } from './base86';\n\nlet enc = new TextEncoder();\nlet dec = new TextDecoder();\n\nlet getPasswordKey = (password: string) =>\n crypto.subtle.importKey('raw', enc.encode(password), 'PBKDF2', false, ['deriveKey']);\n\nlet deriveKey = (passwordKey: CryptoKey, keyUsage: ('encrypt' | 'decrypt')[]) =>\n crypto.subtle.deriveKey(\n {\n name: 'PBKDF2',\n iterations: 250000,\n hash: 'SHA-256'\n },\n passwordKey,\n { name: 'AES-GCM', length: 256 },\n false,\n keyUsage\n );\n\nlet encryptData = async (secretData: string, password: string) => {\n let iv = crypto.getRandomValues(new Uint8Array(12));\n let passwordKey = await getPasswordKey(password);\n let aesKey = await deriveKey(passwordKey, ['encrypt']);\n let encryptedContent = await crypto.subtle.encrypt(\n {\n name: 'AES-GCM',\n iv\n },\n aesKey,\n enc.encode(secretData)\n );\n\n let encryptedContentArr = new Uint8Array(encryptedContent);\n let buff = new Uint8Array(iv.byteLength + encryptedContentArr.byteLength);\n buff.set(iv, 0);\n buff.set(encryptedContentArr, iv.byteLength);\n\n return base86.encode(buff);\n};\n\nlet decryptData = async (encryptedData: string, password: string) => {\n let encryptedDataBuff = base86.decode(encryptedData);\n let iv = encryptedDataBuff.slice(0, 16);\n let data = encryptedDataBuff.slice(16);\n let passwordKey = await getPasswordKey(password);\n let aesKey = await deriveKey(passwordKey, ['decrypt']);\n let decryptedContent = await crypto.subtle.decrypt(\n {\n name: 'AES-GCM',\n iv\n },\n aesKey,\n data\n );\n\n return dec.decode(decryptedContent);\n};\n\nlet sha512 = async (data: string) => {\n let hashBuffer = await crypto.subtle.digest('SHA-512', enc.encode(data));\n return base62.encode(new Uint8Array(hashBuffer));\n};\n\nexport let secretsCrypto = {\n encrypt: encryptData,\n decrypt: decryptData,\n sha512\n};\n","import { generatePlainId } from '@lowerdeck/id';\nimport { secretsCrypto } from './crypto';\n\nexport class Encryption {\n constructor(private readonly password: string) {}\n\n private async getPassword(entityId: string) {\n return (await secretsCrypto.sha512(`${entityId}${this.password!}`)).slice(0, 50);\n }\n\n async encrypt(input: { secret: string; entityId: string }) {\n return await secretsCrypto.encrypt(\n JSON.stringify({\n id: generatePlainId(10),\n key: input.secret\n }),\n await this.getPassword(input.entityId)\n );\n }\n\n async decrypt(info: { encrypted: string; entityId: string }) {\n let content = JSON.parse(\n await secretsCrypto.decrypt(info.encrypted, await this.getPassword(info.entityId))\n );\n\n return content.key;\n }\n}\n"],"names":["internal","baseX","enc","TextEncoder","dec","TextDecoder","getPasswordKey","password","crypto","subtle","importKey","encode","deriveKey","passwordKey","keyUsage","name","iterations","hash","length","secretsCrypto","encrypt","secretData","iv","getRandomValues","Uint8Array","Promise","resolve","then","aesKey","encryptedContent","input","encryptedContentArr","buff","byteLength","set","e","reject","decrypt","encryptedData","encryptedDataBuff","decode","slice","data","decryptedContent","sha512","digest","hashBuffer","base62","Encryption","this","_proto","prototype","getPassword","entityId","_secretsCrypto$sha","_this2","_encrypt","_JSON$stringify","JSON","stringify","id","generatePlainId","key","secret","_this2$getPassword","call","info","_this3","_decrypt","_info$encrypted","encrypted","_this3$getPassword","_secretsCrypto$decryp","parse"],"mappings":"iHAEA,IAAIA,EAAWC,EACb,0FCAEC,EAAM,IAAIC,YACVC,EAAM,IAAIC,YAEVC,EAAiB,SAACC,GACpB,OAAAC,OAAOC,OAAOC,UAAU,MAAOR,EAAIS,OAAOJ,GAAW,UAAU,EAAO,CAAC,aAAa,EAElFK,EAAY,SAACC,EAAwBC,UACvCN,OAAOC,OAAOG,UACZ,CACEG,KAAM,SACNC,WAAY,KACZC,KAAM,WAERJ,EACA,CAAEE,KAAM,UAAWG,OAAQ,MAC3B,EACAJ,EACD,EA8CQK,EAAgB,CACzBC,QA7Ca,SAAUC,EAAoBd,GAAgB,IAC3D,IAAIe,EAAKd,OAAOe,gBAAgB,IAAIC,WAAW,KAAK,OAAAC,QAAAC,QAC5BpB,EAAeC,IAASoB,KAAA,SAA5Cd,GAAW,OAAAY,QAAAC,QACId,EAAUC,EAAa,CAAC,aAAWc,KAAA,SAAlDC,GAAM,OAAAH,QAAAC,QACmBlB,OAAOC,OAAOW,QACzC,CACEL,KAAM,UACNO,GAAAA,GAEFM,EACA1B,EAAIS,OAAOU,KACZM,KAAA,SAPGE,GASJ,ID5BSC,EC4BLC,EAAsB,IAAIP,WAAWK,GACrCG,EAAO,IAAIR,WAAWF,EAAGW,WAAaF,EAAoBE,YAI9D,OAHAD,EAAKE,IAAIZ,EAAI,GACbU,EAAKE,IAAIH,EAAqBT,EAAGW,YD9B/BjC,EAASW,OAAuB,iBADzBmB,ECiCYE,IDhCwB,IAAI7B,aAAcQ,OAAOmB,GAASA,ECgCpD,EAA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UAAU,CACbC,GAAIC,EAAgB,IACpBC,IAAKhC,EAAMiC,SACX,OAAAtC,QAAAC,QACIuB,KAAKG,YAAYtB,EAAMuB,WAAS1B,KAAAqC,SAAAA,GAAAvC,OAAAA,QAAAC,QAAA8B,EAAAS,KAL3B9C,EAAasC,EAAAO,GAO5B,EAAA,CAAC,MAAA7B,GAAAV,OAAAA,QAAAW,OAAAD,EAAAe,CAAAA,EAAAA,EAEKb,QAAA,SAAQ6B,GAA6C,IAAAC,IAEDC,EAAhDjD,EAAckB,QAAOgC,EAACH,EAAKI,UAAS,OAAA7C,QAAAC,QAAQuB,KAAKG,YAAYc,EAAKb,WAAS1B,KAAA4C,SAAAA,GAAA9C,OAAAA,QAAAC,QAAA0C,EAAAH,KAA3E9C,EAAakD,EAAAE,IAAA5C,KAAA,SAAA6C,GAGrB,OAJcd,KAAKe,MAAKD,GAITV,GAAI,EACrB,EAAA,CAAC,MAAA3B,GAAAV,OAAAA,QAAAW,OAAAD,KAAAa,CAAA,CAtBD"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@lowerdeck/id"),require("@lowerdeck/base62"),require("base-x")):"function"==typeof define&&define.amd?define(["exports","@lowerdeck/id","@lowerdeck/base62","base-x"],r):r((e||self).encryption={},e.id,e.base62,e.baseX)}(this,function(e,r,t,n){function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=/*#__PURE__*/o(n).default("!#$%()*+-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~§"),c=new TextEncoder,s=new TextDecoder,u=function(e){return crypto.subtle.importKey("raw",c.encode(e),"PBKDF2",!1,["deriveKey"])},d=function(e,r){return crypto.subtle.deriveKey({name:"PBKDF2",iterations:25e4,hash:"SHA-256"},e,{name:"AES-GCM",length:256},!1,r)},a={encrypt:function(e,r){try{var t=crypto.getRandomValues(new Uint8Array(12));return Promise.resolve(u(r)).then(function(r){return Promise.resolve(d(r,["encrypt"])).then(function(r){return Promise.resolve(crypto.subtle.encrypt({name:"AES-GCM",iv:t},r,c.encode(e))).then(function(e){var r,n=new Uint8Array(e),o=new Uint8Array(t.byteLength+n.byteLength);return o.set(t,0),o.set(n,t.byteLength),i.encode("string"==typeof(r=o)?(new TextEncoder).encode(r):r)})})})}catch(e){return Promise.reject(e)}},decrypt:function(e,r){try{var t=i.decode(e),n=t.slice(0,16),o=t.slice(16);return Promise.resolve(u(r)).then(function(e){return Promise.resolve(d(e,["decrypt"])).then(function(e){return Promise.resolve(crypto.subtle.decrypt({name:"AES-GCM",iv:n},e,o)).then(function(e){return s.decode(e)})})})}catch(e){return Promise.reject(e)}},sha512:function(e){try{return Promise.resolve(crypto.subtle.digest("SHA-512",c.encode(e))).then(function(e){return t.base62.encode(new Uint8Array(e))})}catch(e){return Promise.reject(e)}}};e.Encryption=/*#__PURE__*/function(){function e(e){this.password=void 0,this.password=e}var t=e.prototype;return t.getPassword=function(e){try{return Promise.resolve(a.sha512(""+e+this.password)).then(function(e){return e.slice(0,50)})}catch(e){return Promise.reject(e)}},t.encrypt=function(e){try{var t=a.encrypt,n=JSON.stringify({id:r.generatePlainId(10),key:e.secret});return Promise.resolve(this.getPassword(e.entityId)).then(function(e){return Promise.resolve(t.call(a,n,e))})}catch(e){return Promise.reject(e)}},t.decrypt=function(e){try{var r=a.decrypt,t=e.encrypted;return Promise.resolve(this.getPassword(e.entityId)).then(function(e){return Promise.resolve(r.call(a,t,e)).then(function(e){return JSON.parse(e).key})})}catch(e){return Promise.reject(e)}},e}()});
|
|
2
|
+
//# sourceMappingURL=index.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/base86.ts","../src/crypto.ts","../src/index.ts"],"sourcesContent":["import baseX from 'base-x';\n\nlet internal = baseX(\n '!#$%()*+-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~§'\n);\n\nexport let base86 = {\n encode: (input: string | Uint8Array) =>\n internal.encode(typeof input == 'string' ? new TextEncoder().encode(input) : input),\n decode: (input: string) => internal.decode(input)\n};\n","import { base62 } from '@lowerdeck/base62';\nimport { base86 } from './base86';\n\nlet enc = new TextEncoder();\nlet dec = new TextDecoder();\n\nlet getPasswordKey = (password: string) =>\n crypto.subtle.importKey('raw', enc.encode(password), 'PBKDF2', false, ['deriveKey']);\n\nlet deriveKey = (passwordKey: CryptoKey, keyUsage: ('encrypt' | 'decrypt')[]) =>\n crypto.subtle.deriveKey(\n {\n name: 'PBKDF2',\n iterations: 250000,\n hash: 'SHA-256'\n },\n passwordKey,\n { name: 'AES-GCM', length: 256 },\n false,\n keyUsage\n );\n\nlet encryptData = async (secretData: string, password: string) => {\n let iv = crypto.getRandomValues(new Uint8Array(12));\n let passwordKey = await getPasswordKey(password);\n let aesKey = await deriveKey(passwordKey, ['encrypt']);\n let encryptedContent = await crypto.subtle.encrypt(\n {\n name: 'AES-GCM',\n iv\n },\n aesKey,\n enc.encode(secretData)\n );\n\n let encryptedContentArr = new Uint8Array(encryptedContent);\n let buff = new Uint8Array(iv.byteLength + encryptedContentArr.byteLength);\n buff.set(iv, 0);\n buff.set(encryptedContentArr, iv.byteLength);\n\n return base86.encode(buff);\n};\n\nlet decryptData = async (encryptedData: string, password: string) => {\n let encryptedDataBuff = base86.decode(encryptedData);\n let iv = encryptedDataBuff.slice(0, 16);\n let data = encryptedDataBuff.slice(16);\n let passwordKey = await getPasswordKey(password);\n let aesKey = await deriveKey(passwordKey, ['decrypt']);\n let decryptedContent = await crypto.subtle.decrypt(\n {\n name: 'AES-GCM',\n iv\n },\n aesKey,\n data\n );\n\n return dec.decode(decryptedContent);\n};\n\nlet sha512 = async (data: string) => {\n let hashBuffer = await crypto.subtle.digest('SHA-512', enc.encode(data));\n return base62.encode(new Uint8Array(hashBuffer));\n};\n\nexport let secretsCrypto = {\n encrypt: encryptData,\n decrypt: decryptData,\n sha512\n};\n","import { generatePlainId } from '@lowerdeck/id';\nimport { secretsCrypto } from './crypto';\n\nexport class Encryption {\n constructor(private readonly password: string) {}\n\n private async getPassword(entityId: string) {\n return (await secretsCrypto.sha512(`${entityId}${this.password!}`)).slice(0, 50);\n }\n\n async encrypt(input: { secret: string; entityId: string }) {\n return await secretsCrypto.encrypt(\n JSON.stringify({\n id: generatePlainId(10),\n key: input.secret\n }),\n await this.getPassword(input.entityId)\n );\n }\n\n async decrypt(info: { encrypted: string; entityId: string }) {\n let content = JSON.parse(\n await secretsCrypto.decrypt(info.encrypted, await this.getPassword(info.entityId))\n );\n\n return content.key;\n }\n}\n"],"names":["internal","baseX","enc","TextEncoder","dec","TextDecoder","getPasswordKey","password","crypto","subtle","importKey","encode","deriveKey","passwordKey","keyUsage","name","iterations","hash","length","secretsCrypto","encrypt","secretData","iv","getRandomValues","Uint8Array","Promise","resolve","then","aesKey","encryptedContent","input","encryptedContentArr","buff","byteLength","set","e","reject","decrypt","encryptedData","encryptedDataBuff","decode","slice","data","decryptedContent","sha512","digest","hashBuffer","base62","Encryption","this","_proto","prototype","getPassword","entityId","_secretsCrypto$sha","_this2","_encrypt","_JSON$stringify","JSON","stringify","id","generatePlainId","key","secret","_this2$getPassword","call","info","_this3","_decrypt","_info$encrypted","encrypted","_this3$getPassword","_secretsCrypto$decryp","parse"],"mappings":"icAEIA,oBAAWC,QACb,0FCAEC,EAAM,IAAIC,YACVC,EAAM,IAAIC,YAEVC,EAAiB,SAACC,GACpB,OAAAC,OAAOC,OAAOC,UAAU,MAAOR,EAAIS,OAAOJ,GAAW,UAAU,EAAO,CAAC,aAAa,EAElFK,EAAY,SAACC,EAAwBC,UACvCN,OAAOC,OAAOG,UACZ,CACEG,KAAM,SACNC,WAAY,KACZC,KAAM,WAERJ,EACA,CAAEE,KAAM,UAAWG,OAAQ,MAC3B,EACAJ,EACD,EA8CQK,EAAgB,CACzBC,QA7Ca,SAAUC,EAA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