-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.js
62 lines (52 loc) · 1.29 KB
/
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
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
const path = require('path');
const fs = require('fs');
let key, string;
// process argv
const processArgs = () => {
return new Promise((resolve, reject) => {
process.argv.slice(2).forEach(arg => {
const pre = arg.split('=');
if (pre[0] === '--string') {
string = pre[1];
} else if (pre[0] === '--key') {
key = pre[1];
} else {
// assume it's part of the string and append it
string = `${string} ${pre[1]}`;
}
});
resolve();
});
};
// open key for use
const openKeyfile = () => {
return new Promise((resolve, reject) => {
fs.readFile(path.resolve(key), (err, keyfile) => {
if (err) {
reject(err);
}
resolve(JSON.parse(keyfile));
});
});
};
// shift text string using key
const shiftText = (keyfile) => {
return new Promise((resolve, reject) => {
let encrypted = [];
string.split(' ').forEach(word => {
if (keyfile[word.toUpperCase()]) {
encrypted.push(keyfile[word.toUpperCase()].toLowerCase());
} else {
encrypted.push(word.toLowerCase());
}
});
resolve(encrypted.join(' '));
});
};
// encrypt
const encrypt = async () => {
await processArgs();
const keyfile = await openKeyfile();
console.log(await shiftText(keyfile));
};
encrypt();