UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 2.07 kB
{"version":3,"file":"encrypt-decrypt.mjs","names":[],"sources":["../../../src/utils/helpers/encrypt-decrypt.ts"],"sourcesContent":["import crypto from \"node:crypto\";\nimport { translate } from \"../../libs/i18n/index.js\";\nimport LucidError from \"../errors/lucid-error.js\";\n\nconst encryptionKeyToHex = (encryptionKey: string) =>\n\tcrypto\n\t\t.createHash(\"sha512\")\n\t\t.update(encryptionKey)\n\t\t.digest(\"hex\")\n\t\t.substring(0, 32);\n\nexport const encrypt = (secret: string, encryptionKey: string) => {\n\tconst key = encryptionKeyToHex(encryptionKey);\n\tconst iv = crypto.randomBytes(16).toString(\"hex\").slice(0, 16);\n\tconst cipher = crypto.createCipheriv(\"aes-256-cbc\", key, iv);\n\tlet encrypted = cipher.update(secret, \"utf8\", \"hex\");\n\tencrypted += cipher.final(\"hex\");\n\treturn `${iv}:${encrypted}`;\n};\n\nexport const decrypt = (encryptedSecret: string, encryptionKey: string) => {\n\tconst [iv, encrypted] = encryptedSecret.split(\":\");\n\tif (!iv || !encrypted) {\n\t\tthrow new LucidError({\n\t\t\tmessage: translate(\"server:core.security.secrets.encrypted.invalid\"),\n\t\t});\n\t}\n\tconst key = encryptionKeyToHex(encryptionKey);\n\tconst decipher = crypto.createDecipheriv(\"aes-256-cbc\", key, iv);\n\tlet decrypted = decipher.update(encrypted as string, \"hex\", \"utf8\");\n\tdecrypted += decipher.final(\"utf8\");\n\treturn decrypted.toString();\n};\n"],"mappings":"8HAIA,MAAM,EAAsB,GAC3B,EACE,WAAW,QAAQ,CAAC,CACpB,OAAO,CAAa,CAAC,CACrB,OAAO,KAAK,CAAC,CACb,UAAU,EAAG,EAAE,EAEL,GAAW,EAAgB,IAA0B,CACjE,IAAM,EAAM,EAAmB,CAAa,EACtC,EAAK,EAAO,YAAY,EAAE,CAAC,CAAC,SAAS,KAAK,CAAC,CAAC,MAAM,EAAG,EAAE,EACvD,EAAS,EAAO,eAAe,cAAe,EAAK,CAAE,EACvD,EAAY,EAAO,OAAO,EAAQ,OAAQ,KAAK,EAEnD,MADA,IAAa,EAAO,MAAM,KAAK,EACxB,GAAG,EAAG,GAAG,GACjB,EAEa,GAAW,EAAyB,IAA0B,CAC1E,GAAM,CAAC,EAAI,GAAa,EAAgB,MAAM,GAAG,EACjD,GAAI,CAAC,GAAM,CAAC,EACX,MAAM,IAAI,EAAW,CACpB,QAAS,EAAU,gDAAgD,CACpE,CAAC,EAEF,IAAM,EAAM,EAAmB,CAAa,EACtC,EAAW,EAAO,iBAAiB,cAAe,EAAK,CAAE,EAC3D,EAAY,EAAS,OAAO,EAAqB,MAAO,MAAM,EAElE,MADA,IAAa,EAAS,MAAM,MAAM,EAC3B,EAAU,SAAS,CAC3B"}