-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·63 lines (55 loc) · 2.14 KB
/
index.js
File metadata and controls
executable file
·63 lines (55 loc) · 2.14 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env node --harmony
'use strict';
const crypto = require('crypto');
const cli = require('commander');
const chalk = require('chalk');
const md5 = require('md5');
const fs = require('fs');
cli
.version('0.1.0', '-v, --version')
.arguments('<file>')
.option('-p, --password <password>', 'Password used to encrypt and decrypt')
.option('-a, --action <action>', 'Either encrypt or decrypt', /^(encrypt|decrypt)$/i)
.action(function(file) {
if(cli.action != 'encrypt' && cli.action != 'decrypt') {
return console.error(chalk.bold.red("Invalid Action"));
}
if(!fs.existsSync(file)) {
return console.error(chalk.bold.red("File Not Found"));
}
let encKey = md5(cli.password);
fs.readFile(file, function(err, data) {
if(err) {
return console.error(chalk.bold.red("Error occurred while reading a file"));
}
let content = data.toString();
let processedContent = null;
processedContent = cli.action == 'encrypt'? encrypt(encKey, content): decrypt(encKey, content);
if(processedContent != null) {
fs.writeFile(file, processedContent, function(err, data) {
if(err) {
return console.error(chalk.bold.red("Error occurred while write a file"));
}
return console.log(chalk.bold.green("Action " + cli.action + " ran successfully" ));
});
}
});
})
.parse(process.argv);
function encrypt(encKey, content) {
let iv_length = 16;
let iv = crypto.randomBytes(iv_length);
let cipher = crypto.createCipheriv('AES-256-CBC', Buffer.from(encKey), iv);
let encrypted = cipher.update(content);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
function decrypt(encKey, content){
let textParts = content.split(':');
let iv = Buffer.from(textParts.shift(), 'HEX');
let encryptedContent = Buffer.from(textParts.join(':'), 'HEX');
let decipher = crypto.createDecipheriv('AES-256-CBC', Buffer.from(encKey), iv);
let decrypted = decipher.update(encryptedContent);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}