| 9 | const SECRET = process.env.NEXTAUTH_SECRET!; |
| 10 | |
| 11 | export function encrypt(dataToEncrypt: string): string { |
| 12 | if (!SECRET) { |
| 13 | throw new Error('missing secret'); |
| 14 | } |
| 15 | const salt = randomBytes(16).toString('hex'); |
| 16 | const key = scryptSync(SECRET, salt, 32); |
| 17 | const iv = Buffer.alloc(16, 0); // Initialization vector. |
| 18 | const cipher = createCipheriv(algorithm, key, iv); |
| 19 | let encrypted = cipher.update(dataToEncrypt, 'utf8', 'hex'); |
| 20 | encrypted += cipher.final('hex'); |
| 21 | return `${salt}:${encrypted}`; |
| 22 | } |
| 23 | |
| 24 | export function decrypt(dataToDecrypt: string): string { |
| 25 | if (!SECRET) { |