-
Notifications
You must be signed in to change notification settings - Fork 3
/
encryption.js
30 lines (26 loc) · 1.01 KB
/
encryption.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
var fs = require('fs'),
crypto = require('crypto'),
prompt = require('prompt'),
shell = require('shelljs');
// check for .password file, if not there do the encryption thing
if (!shell.test('-f', '.password')) {
prompt.logger.info('Creating encryption passkey for JIRA login');
fs.writeFileSync('.password', '');
shell.exec('openssl rand -base64 48', {silent: true}).output.to('./password');
}
var passKey = fs.readFileSync('.password').toString('utf8').trim();
module.exports = {
encrypt: function (text) {
var cipher = crypto.createCipher('aes-256-cbc', passKey);
var crypted = cipher.update(crypto.randomBytes(10).toString('base64') + '.' + text,'utf8','hex');
crypted += cipher.final('hex');
return crypted;
},
decrypt: function (text) {
var decipher = crypto.createDecipher('aes-256-cbc', passKey);
var dec = decipher.update(text,'hex','utf8');
dec += decipher.final('utf8');
dec = dec.split('.')[1];
return dec;
}
};