23 lines
685 B
JavaScript
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;
|
|
};
|