UNPKG

svelte-kit-cookie-session-patch

Version:

⚒️ Encrypted 'stateless' cookie sessions for SvelteKit

54 lines (53 loc) 2.6 kB
"use strict"; /** * * This work is mostly copied over from `string-cipher` => `https://github.com/limplash/string-cipher`, but since it doesn't support esm(yet) i decided to copy * only the bits needed for `svelte-kit-cookie-session`. Thank you limplash! * MIT License * Copyright (c) 2021 limplash * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.makeStringEncrypterSync = exports.makeStringDecrypterSync = void 0; const crypto_1 = require("crypto"); const keyLengthHint = (algo) => { switch (algo) { case "aes-256-gcm": return 32; case "aes-192-gcm": return 24; case "aes-128-gcm": return 16; default: throw new Error(`Unsupported algorithm ${algo}`); } }; const makeStringDecrypterSync = ({ algorithm, inputEncoding = "base64", stringEncoding = "utf8", ivLength = 12, authTagLength = 16, saltLength = 32, iterations = 1, digest = "sha256", }) => (text, password) => { const buffer = Buffer.from(text, inputEncoding); // data is packed in this sequence [salt iv tag cipherTest] const tagStartIndex = saltLength + ivLength; const textStartIndex = tagStartIndex + authTagLength; const salt = buffer.slice(0, saltLength); const iv = buffer.slice(saltLength, tagStartIndex); const tag = buffer.slice(tagStartIndex, textStartIndex); const cipherText = buffer.slice(textStartIndex); const key = crypto_1.pbkdf2Sync(password, salt, iterations, keyLengthHint(algorithm), digest); const decipher = crypto_1.createDecipheriv(algorithm, key, iv, { authTagLength, }).setAuthTag(tag); //@ts-ignore return `${decipher.update(cipherText, "binary", stringEncoding)}${decipher.final(stringEncoding)}`; }; exports.makeStringDecrypterSync = makeStringDecrypterSync; const makeStringEncrypterSync = ({ algorithm, outputEncoding = "base64", stringEncoding = "utf8", authTagLength = 16, ivLength = 12, saltLength = 32, iterations = 1, digest = "sha256", }) => (text, password) => { const iv = crypto_1.randomBytes(ivLength); const salt = crypto_1.randomBytes(saltLength); const key = crypto_1.pbkdf2Sync(password, salt, iterations, keyLengthHint(algorithm), digest); const cipher = crypto_1.createCipheriv(algorithm, key, iv, { authTagLength }); const cipherText = Buffer.concat([ cipher.update(text, stringEncoding), cipher.final(), ]); return Buffer.concat([salt, iv, cipher.getAuthTag(), cipherText]).toString(outputEncoding); }; exports.makeStringEncrypterSync = makeStringEncrypterSync;