-
Notifications
You must be signed in to change notification settings - Fork 3
/
generateKey.js
71 lines (58 loc) · 1.61 KB
/
generateKey.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const { AES, enc, SHA256 } = require("crypto-js");
const crypto = require("crypto");
var fs = require('fs');
const encode = (password) => {
if (!password) {
throw new Error("Usage: node generateKey.js password");
}
const privateKey = crypto.randomBytes(32).toString("hex");
// This doesn't improve the encryption security, but slows down password
// attempts in the front-end.
for (let i = 0; i < 100000; i++) {
password = SHA256(password);
}
password = password.toString();
// Decrypt
let cipher;
try {
console.log(privateKey);
cipher = AES.encrypt(privateKey, password).toString();
} catch (err) {
console.error(err);
return;
}
return cipher;
}
const decode = (cipher, password) => {
if (!cipher || !password) {
throw new Error("Usage: node generateKey.js cipher password");
}
for (let i = 0; i < 100000; i++) {
password = SHA256(password);
}
password = password.toString();
// Decrypt
let privateKey;
try {
console.log(privateKey);
privateKey = AES.decrypt(cipher.toString(), password).toString(enc.Utf8);
} catch (err) {
console.error(err);
return;
}
return privateKey;
}
const main = (argv) => {
const cipher = encode(...argv.slice(2));
// Append to .env
fs.appendFile(
'.env',
`\nREACT_APP_PRIVATE_KEY_CIPHER="${cipher}"`,
'utf8',
function (error) {
if (error) throw error;
console.log("Private key cipher stored in `.env`");
}
);
}
main(process.argv);