-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.js
More file actions
37 lines (31 loc) · 1.17 KB
/
crypto.js
File metadata and controls
37 lines (31 loc) · 1.17 KB
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
const crypto = require('crypto');
const IV_LENGTH = 16; // For AES, this is always 16
class Crypto {
constructor(opts) {
opts = opts || {};
if (!opts.key) {
throw new Error('No crypto key has been given.');
}
const hash = crypto.createHmac('sha256', opts.key).digest('hex');
this.key = hash.slice(0, 32);
this.method = opts.method || 'aes-256-cbc';
}
encrypt(text) {
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(this.key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
decrypt(text) {
const textParts = text.split(':');
const iv = Buffer.from(textParts.shift(), 'hex');
const encryptedText = Buffer.from(textParts.join(':'), 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(this.key), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
const decryptedText = decrypted.toString();
return decryptedText;
}
}
module.exports = Crypto;