/
index.js
60 lines (52 loc) · 1.9 KB
/
index.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const {createCipher, createCipheriv, createDecipher, createDecipheriv, randomBytes} = require('crypto');
const algorithm = 'aes-256-ctr';
const key = process.env.KEY || 'b2df428b9929d3ace7c598bbf4e496b2';
const inputEncoding = 'utf8';
const outputEncoding = 'hex';
/**
* Encrypt function using only the key.
* @param {string} value to encrypt
*/
module.exports.encrypt = value => {
const cipher = createCipher(algorithm, key);
let crypted = cipher.update(value, inputEncoding, outputEncoding);
crypted += cipher.final(outputEncoding);
return crypted;
}
/**
* Decrypt function using only the key
* @param {string} value to decrypt
*/
module.exports.decrypt = value => {
const decipher = createDecipher(algorithm, key);
let dec = decipher.update(value, outputEncoding, inputEncoding);
dec += decipher.final(inputEncoding);
return dec;
}
/**
* Encrypt using an initialisation vector
* @param {string} value to encrypt
*/
module.exports.encryptIv = value => {
const iv = new Buffer(randomBytes(16));
const cipher = createCipheriv(algorithm, key, iv);
let crypted = cipher.update(value, inputEncoding, outputEncoding);
crypted += cipher.final(outputEncoding);
return `${iv.toString('hex')}:${crypted.toString()}`;
}
/**
* Decrypt using an initialisation vector
* @param {string} value value to decrypt
*/
module.exports.decryptIv = value => {
const textParts = value.split(':');
//extract the IV from the first half of the value
const IV = new Buffer(textParts.shift(), outputEncoding);
//extract the encrypted text without the IV
const encryptedText = new Buffer(textParts.join(':'), outputEncoding);
//decipher the string
const decipher = createDecipheriv(algorithm,key, IV);
let decrypted = decipher.update(encryptedText, outputEncoding, inputEncoding);
decrypted += decipher.final(inputEncoding);
return decrypted.toString();
}