-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
48 lines (41 loc) · 1.25 KB
/
utils.js
File metadata and controls
48 lines (41 loc) · 1.25 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
43
44
45
46
47
48
const crypto = require("crypto");
const fs = require("fs");
const algorithm = "aes-256-cbc";
const secretKey = crypto.scryptSync("your-secret-passphrase", "salt", 32);
const passcode = "12345678";
const encrypt = (text) => {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return iv.toString("hex") + ":" + encrypted.toString("hex");
};
const decrypt = (hash) => {
const [ivHex, encryptedHex] = hash.split(":");
const decipher = crypto.createDecipheriv(
algorithm,
secretKey,
Buffer.from(ivHex, "hex")
);
const decrypted = Buffer.concat([
decipher.update(Buffer.from(encryptedHex, "hex")),
decipher.final(),
]);
return decrypted.toString();
};
const savePasswords = (passwords) => {
fs.writeFileSync("passwords.json", JSON.stringify(passwords, null, 2));
};
const loadPasswords = () => {
if (!fs.existsSync("passwords.json")) return [];
return JSON.parse(fs.readFileSync("passwords.json"));
};
const verifyPasscode = (input) => {
return input === passcode;
};
module.exports = {
encrypt,
decrypt,
savePasswords,
loadPasswords,
verifyPasscode,
};