-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.js
More file actions
42 lines (38 loc) · 1.23 KB
/
encryption.js
File metadata and controls
42 lines (38 loc) · 1.23 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import crypto from "crypto";
import fs from "fs";
const algorithm = "aes-256-cbc";
const key = crypto.scryptSync(process.env.ENCRYPTION_SECRET || "default", "salt", 32);
const iv = Buffer.alloc(16, 0); // 16-byte zeroed IV
// Encrypts a Buffer and writes it to a file
export function encryptFile(buffer, outputPath) {
return new Promise((resolve, reject) => {
try {
const cipher = crypto.createCipheriv(algorithm, key, iv);
const encrypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
fs.writeFile(outputPath, encrypted, (err) => {
if (err) reject(err);
else resolve();
});
} catch (err) {
reject(err);
}
});
}
// Decrypts an encrypted file and returns a Buffer
export function decryptFileToBuffer(inputPath) {
return new Promise((resolve, reject) => {
try {
const decipher = crypto.createDecipheriv(algorithm, key, iv);
const chunks = [];
const input = fs.createReadStream(inputPath);
input.on("data", chunk => chunks.push(decipher.update(chunk)));
input.on("end", () => {
chunks.push(decipher.final());
resolve(Buffer.concat(chunks));
});
input.on("error", reject);
} catch (err) {
reject(err);
}
});
}