| 1 | import { createCipheriv, createDecipheriv, randomBytes } from "crypto"; |
| 2 | import { env } |
| 3 | |
| 4 | function getKey(): Buffer |
| 5 | return Buffer.from |
| 6 | } |
| 7 | |
| 8 | export function encrypt(plaintext |
| 9 | const key = |
| 10 | const iv = |
| 11 | const cipher = |
| 12 | const encrypted = |
| 13 | cipher.update(plaintext |
| 14 | cipher.final(), |
| 15 | ]); |
| 16 | const tag = |
| 17 | return `${iv. |
| 18 | } |
| 19 | |
| 20 | export function safeDecrypt(value |
| 21 | try { |
| 22 | return decrypt(value |
| 23 | } catch { |
| 24 | return value; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | export function decrypt(ciphertext |
| 29 | const key = |
| 30 | const [ivHex, |
| 31 | if (!ivHex || |
| 32 | const decipher = |
| 33 | "aes-256-gcm", |
| 34 | key, |
| 35 | Buffer.from(ivHex |
| 36 | ); |
| 37 | decipher.setAuthTag(Buffer |
| 38 | return ( |
| 39 | decipher.update(Buffer |
| 40 | decipher.final("utf8 |
| 41 | ); |
| 42 | } |