Files
yourpart3/backend/utils/encryption.js
2024-07-22 18:14:12 +02:00

24 lines
723 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) => {
console.log(text, secretKey, 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;
};