/
Cipher.js
50 lines (43 loc) · 1.05 KB
/
Cipher.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
/**
* Copyright 2013 the PM2 project authors. All rights reserved.
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*/
var crypto = require('crypto');
const CIPHER_ALGORITHM = 'aes256';
var Cipher = module.exports = {};
/**
* Description
* @method decipherMessage
* @param {} msg
* @return ret
*/
Cipher.decipherMessage = function(msg, key) {
var ret = {};
try {
var decipher = crypto.createDecipher(CIPHER_ALGORITHM, key);
var decipheredMessage = decipher.update(msg, 'hex', 'utf8');
decipheredMessage += decipher.final('utf8');
ret = JSON.parse(decipheredMessage);
} catch(e) {
return null;
}
return ret;
}
/**
* Description
* @method cipherMessage
* @param {} data
* @param {} key
* @return
*/
Cipher.cipherMessage = function(data, key) {
try {
var cipher = crypto.createCipher(CIPHER_ALGORITHM, key);
var cipheredData = cipher.update(data, 'utf8', 'hex');
cipheredData += cipher.final('hex');
return cipheredData;
} catch(e) {
return null;
}
}