-
Notifications
You must be signed in to change notification settings - Fork 9
/
crypter.js
executable file
·56 lines (50 loc) · 1.42 KB
/
crypter.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
#!/usr/bin/env node
const path = require('path');
const crypto = require('crypto');
const fs = require('fs');
// paths to files
const ENCRYPTED_FILE_PATH = path.join(process.cwd(), 'app-config.json.cast5');
const DECRYPTED_FILE_PATH = path.join(process.cwd(), 'app-config.json');
/**
* Process the crypto
* @param {Object} actionFn action function for pipe
* @param {String} from File path to src
* @param {String} to File path to destination
* @return {Object} Promise resolves after the process is end
*/
function processor(actionFn, from, to) {
from = fs.createReadStream(from);
to = fs.createWriteStream(to);
from
.pipe(actionFn)
.pipe(to);
return new Promise(resolve => {
from
.on('end', resolve());
});
}
const secureKey = process.env.secureKey;
if (!secureKey) {
throw new Error('Secure key not found');
}
const action = process.argv[2];
switch (action) {
case 'enc':
{
const enc = crypto.createCipher('cast5-cbc', secureKey);
processor(enc, DECRYPTED_FILE_PATH, ENCRYPTED_FILE_PATH);
break;
}
case 'dec':
{
const dec = crypto.createDecipher('cast5-cbc', secureKey);
processor(dec, ENCRYPTED_FILE_PATH, DECRYPTED_FILE_PATH);
break;
}
default:
{
const msg = `${process.argv[1].match(/[^/]+$/g, '')}`;
console.log(`${msg} <param>\n${'-'.repeat(msg.length + 2)}^^^^^ param not found`);
break;
}
}