-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.js
27 lines (22 loc) · 900 Bytes
/
key.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
var crypto = require('crypto'),
algorithm = 'aes-256-cbc',
password = Buffer.from('');
const IV_LENGTH = 16;
function encrypt(text){
let iv = crypto.randomBytes(IV_LENGTH);
var cipher = crypto.createCipheriv(algorithm,password,iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
function decrypt(text){
if(!text){return;}
let textParts = text.split(':');
let iv = Buffer.from(textParts.shift(), 'hex');
let encryptedText = Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv(algorithm, password, iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
exports.decrypt = decrypt;