Spaces:
Runtime error
Runtime error
| const crypto = require('crypto'); | |
| require('dotenv').config(); | |
| const algorithm = 'aes-256-cbc'; | |
| const secretKey = process.env.ENCRYPTION_KEY; | |
| if (!secretKey || secretKey.length !== 64) { | |
| console.error('FATAL: ENCRYPTION_KEY must be exactly 64 hex characters (32 bytes).'); | |
| process.exit(1); | |
| } | |
| const key = Buffer.from(secretKey, 'hex'); | |
| function encrypt(text) { | |
| const iv = crypto.randomBytes(16); | |
| const cipher = crypto.createCipheriv(algorithm, key, iv); | |
| let encrypted = cipher.update(text, 'utf8', 'hex'); | |
| encrypted += cipher.final('hex'); | |
| return { | |
| iv: iv.toString('hex'), | |
| encryptedData: encrypted | |
| }; | |
| } | |
| function decrypt(encryptedData, ivHex) { | |
| const iv = Buffer.from(ivHex, 'hex'); | |
| const decipher = crypto.createDecipheriv(algorithm, key, iv); | |
| let decrypted = decipher.update(encryptedData, 'hex', 'utf8'); | |
| decrypted += decipher.final('utf8'); | |
| return decrypted; | |
| } | |
| module.exports = { encrypt, decrypt }; | |