-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (63 loc) · 2.58 KB
/
index.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
#!/usr/bin/env node
console.log('How to encrypt: chaabi "<text>"\nHow to decrypt: chaabi open');
const program = require('commander');
const co = require('co');
const prompt = require('co-prompt');
const fs = require("fs");
program
.version('0.0.3')
.arguments('<text>')
.option('-p, --password <password>', 'The user\'s password')
.option('-s, --secret <secret>', 'Secret text to decrypt')
.action(function(text) {
co(function*() {
if (text == 'open') {
//var secret = yield prompt('secret: ');
var password = yield prompt.password('password: ');
//console.log("Decrypt \""+secret);
//console.log(decrypt(secret, password));
fs.access("./.chaabi", fs.F_OK, function(err) {
if (!err) {
fs.readFile("./.chaabi", 'utf8', (err1, data) => {
if (err1) throw err1;
// console.log(data);
//data.replace(/(\r\n|\n|\r)/gm, "");
var encryptedDataArray = data.split(/\r?\n\n/);
for (var i = 0; i < encryptedDataArray.length; i++) {
console.log(decrypt(encryptedDataArray[i], password));
}
});
} else {
console.log("Chaabi does not exist");
}
});
} else if (text == "pop") {
//TODO: remove last entry. Ask for confirmation, default No.
} else {
var password = yield prompt.password('password: ');
console.log("Encrypt \"" + text);
var encryptedText = encrypt(text, password);
console.log(encryptedText);
//var tag = yield prompt('tag: ');
fs.appendFile(".chaabi", encryptedText + "\n\n", 'utf8', (err) => {
if (err) throw err;
console.log('chaabi added');
});
}
});
})
.parse(process.argv);
var crypto = require('crypto'),
algorithm = 'aes-256-ctr';
function encrypt(text, password) {
var cipher = crypto.createCipher(algorithm, password)
var crypted = cipher.update(text, 'utf8', 'hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text, password) {
var decipher = crypto.createDecipher(algorithm, password)
var dec = decipher.update(text, 'hex', 'utf8')
dec += decipher.final('utf8');
return dec;
}