-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.js
25 lines (21 loc) · 906 Bytes
/
encrypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import dotenv from "dotenv";
import crypto from "crypto";
dotenv.config();
const algorithm = "aes-256-ctr";
export function encrypt(text) {
const secretKey = Buffer.from(process.env.ENCRYPTION_KEY, "hex");
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return { iv: iv.toString("hex"), encryptedData: encrypted.toString("hex") };
}
export function decrypt(text) {
const secretKey = Buffer.from(process.env.ENCRYPTION_KEY, "hex");
let iv = Buffer.from(text.iv, "hex");
let encryptedText = Buffer.from(text.encryptedData, "hex");
let decipher = crypto.createDecipheriv(algorithm, secretKey, iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}