import * as crypto from 'crypto'; export function encrypt(text: string) { const secret = process.env.KEY_SECRET; if (!secret) throw new Error('Falta la variable de entorno KEY_SECRET'); const key = crypto.scryptSync(secret, 'salt', 32); const iv = crypto.randomBytes(16); const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); let encrypted = cipher.update(text, 'utf8', 'hex'); encrypted += cipher.final('hex'); return { iv: iv.toString('hex'), encryptedData: encrypted, }; } export function decrypt(encryptedData: string, ivHex: string) { const secret = process.env.KEY_SECRET; if (!process.env.KEY_SECRET) { throw new Error('Falta la variable de entorno KEY_SECRET'); } if (!secret) throw new Error('Falta la variable de entorno KEY_SECRET'); const key = crypto.scryptSync(secret, 'salt', 32); const iv = Buffer.from(ivHex, 'hex'); const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv); let decrypted = decipher.update(encryptedData, 'hex', 'utf8'); decrypted += decipher.final('utf8'); return decrypted; }