elm-pages-v3-beta/examples/todos/port-data-source.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

import kleur from "kleur";
2022-08-11 16:01:44 +03:00
import crypto from "crypto";
kleur.enabled = true;
2022-08-11 16:01:44 +03:00
/* Encrypt/decrypt code source: https://github.com/kentcdodds/kentcdodds.com/blob/43130b0d9033219920a46bb8a4f009781afa02f1/app/utils/encryption.server.ts */
2022-08-11 16:01:44 +03:00
const algorithm = "aes-256-ctr";
let secret = "not-at-all-secret";
if (process.env.MAGIC_LINK_SECRET) {
secret = process.env.MAGIC_LINK_SECRET;
} else if (process.env.NODE_ENV === "production") {
throw new Error("Must set MAGIC_LINK_SECRET");
}
2022-08-11 16:01:44 +03:00
const ENCRYPTION_KEY = crypto.scryptSync(secret, "salt", 32);
const IV_LENGTH = 16;
export function encrypt(text: string) {
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv(algorithm, ENCRYPTION_KEY, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return `${iv.toString("hex")}:${encrypted.toString("hex")}`;
}
2022-08-11 16:01:44 +03:00
export function decrypt(text: string) {
const [ivPart, encryptedPart] = text.split(":");
if (!ivPart || !encryptedPart) {
throw new Error("Invalid text.");
}
const iv = Buffer.from(ivPart, "hex");
const encryptedText = Buffer.from(encryptedPart, "hex");
const decipher = crypto.createDecipheriv(algorithm, ENCRYPTION_KEY, iv);
const decrypted = Buffer.concat([
decipher.update(encryptedText),
decipher.final(),
]);
return decrypted.toString();
}