Files
yourpart3/backend/utils/encryption.js
2024-07-22 20:55:33 +02:00

23 lines
685 B
JavaScript

import crypto from 'crypto';
const algorithm = 'aes-256-cbc';
const secretKey = process.env.SECRET_KEY;
export const generateIv = () => {
return crypto.randomBytes(16);
};
export const encrypt = (text, iv) => {
const cipher = crypto.createCipheriv(algorithm, Buffer.from(secretKey, 'utf-8'), iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
};
export const decrypt = (text, iv) => {
const decipher = crypto.createDecipheriv(algorithm, Buffer.from(secretKey, 'utf-8'), iv);
let decrypted = decipher.update(text, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
};