mirror of
https://github.com/Unitech/pm2.git
synced 2025-12-08 20:35:53 +00:00
46 lines
915 B
JavaScript
46 lines
915 B
JavaScript
|
|
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', 'binary');
|
|
decipheredMessage += decipher.final("binary");
|
|
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, "binary", "hex");
|
|
cipheredData += cipher.final("hex");
|
|
return cipheredData;
|
|
} catch(e) {
|
|
return null;
|
|
}
|
|
}
|