-
Notifications
You must be signed in to change notification settings - Fork 5
/
crypt.js
22 lines (22 loc) · 939 Bytes
/
crypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const crypto = require('crypto');
const algorithm = 'aes-256-cbc';
function encrypt(text, keystr) {
const iv = crypto.randomBytes(16);
const key = crypto.createHash("sha256").update(keystr).digest()
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return { iv: iv.toString('hex'), encryptedData: encrypted.toString('hex') };
}
function decrypt(text, keystr) {
const iv = Buffer.from(text.iv, 'hex');
const key = crypto.createHash("sha256").update(keystr).digest()
let encryptedText = Buffer.from(text.encryptedData, 'hex');
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
module.exports = {
encrypt: encrypt, decrypt: decrypt
}