-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
23 lines (21 loc) · 1012 Bytes
/
main.js
File metadata and controls
23 lines (21 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const crypto = require('crypto');
const {Buffer} = global;
exports.encrypt = function(text, password) {
if (process.versions.openssl <= '1.0.1f') {
throw new Error('Please upgrade your OPENSSL this version is heartbleed vulnerable');
};
let iv = crypto.randomBytes(16);
let cyphs = crypto.createCipheriv('aes-256-cbc', Buffer.from(password), iv);
let encryptedstring = cyphs.update(text);
let finalencrypted = Buffer.concat([encryptedstring, cyphs.final()]);
return `${iv.toString('hex')}:${finalencrypted.toString('hex')}`;
};
exports.decrypt = function(text, password) {
let parts = text.split(":");
let iv = Buffer.from(parts.shift(), 'hex');
let encryptedstring = Buffer.from(parts.join(':'), 'hex');
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(password), iv);
let decryptedstring = decipher.update(encryptedstring);
let finalstring = Buffer.concat([decryptedstring, decipher.final()]);
return finalstring.toString();
};