forked from Countly/countly-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
92 lines (89 loc) · 4.24 KB
/
utils.js
File metadata and controls
92 lines (89 loc) · 4.24 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Module for some common utility functions that needs to be separated from {@link module:api/utils/common} either due to circular references or other reasons
* @module api/utils/utils
*/
var crypto = require('crypto'),
countlyConfig = require('./../config', 'dont-enclose');
if(!countlyConfig.encryption){
countlyConfig.encryption = {};
}
/**
* Encrypt provided value
* @param {string} text - value to encrypt
* @param {string=} key - key used for encryption and decryption
* @param {string=} iv - initialization vector to make encryption more secure
* @param {string=} algorithm - name of the algorithm to use for encryption. The algorithm is dependent on OpenSSL, examples are 'aes192', etc. On recent OpenSSL releases, openssl list-cipher-algorithms will display the available cipher algorithms. Default value is aes-256-cbc
* @param {string=} input_encoding - how encryption input is encoded. Used as output for decrypting. Default utf-8.
* @param {string=} output_encoding - how encryption output is encoded. Used as input for decrypting. Default hex.
* @returns {string} encrypted value
*/
exports.encrypt = function(text, key, iv, algorithm, input_encoding, output_encoding) {
var cipher, crypted;
if(typeof key === "undefined"){
key = countlyConfig.encryption.key || "dpYheF85";
}
if(typeof iv === "undefined"){
iv = countlyConfig.encryption.iv;
}
if(typeof algorithm === "undefined"){
//The algorithm is dependent on OpenSSL, examples are 'aes192', etc.
//On recent OpenSSL releases, openssl list-cipher-algorithms will display the available cipher algorithms.
algorithm = countlyConfig.encryption.algorithm || "aes-256-cbc";
}
if(typeof input_encoding === "undefined"){
input_encoding = countlyConfig.encryption.input_encoding || "utf-8";
}
if(typeof output_encoding === "undefined"){
output_encoding = countlyConfig.encryption.output_encoding || "hex";
}
if(iv)
cipher = crypto.createCipheriv(algorithm, key, iv);
else
cipher = crypto.createCipher(algorithm, key);
crypted = cipher.update(text, input_encoding, output_encoding);
crypted += cipher.final(output_encoding);
return crypted+"[CLY]_true";
};
/**
* Decrypt provided value
* @param {string} crypted - value to decrypt
* @param {string=} key - key used for encryption and decryption
* @param {string=} iv - initialization vector used in encryption
* @param {string=} algorithm - name of the algorithm used in encryption. The algorithm is dependent on OpenSSL, examples are 'aes192', etc. On recent OpenSSL releases, openssl list-cipher-algorithms will display the available cipher algorithms. Default value is aes-256-cbc
* @param {string=} input_encoding - how decryption input is encoded. Default hex.
* @param {string=} output_encoding - how decryption output is encoded. Default utf-8.
* @returns {string} decrypted value
*/
exports.decrypt = function(crypted, key, iv, algorithm, input_encoding, output_encoding) {
if(crypted.lastIndexOf("[CLY]_true") === -1 || crypted.lastIndexOf("[CLY]_true") !== crypted.length - 10){
return crypted;
}
else{
crypted = crypted.substring(0, crypted.length - 10);
}
var decipher, decrypted;
if(typeof key === "undefined"){
key = countlyConfig.encryption.key || "dpYheF85";
}
if(typeof iv === "undefined"){
iv = countlyConfig.encryption.iv;
}
if(typeof algorithm === "undefined"){
//The algorithm is dependent on OpenSSL, examples are 'aes192', etc.
//On recent OpenSSL releases, openssl list-cipher-algorithms will display the available cipher algorithms.
algorithm = countlyConfig.encryption.algorithm || "aes-256-cbc";
}
if(typeof input_encoding === "undefined"){
input_encoding = countlyConfig.encryption.output_encoding || "hex";
}
if(typeof output_encoding === "undefined"){
output_encoding = countlyConfig.encryption.input_encoding || "utf-8";
}
if(iv)
decipher = crypto.createDecipheriv(algorithm, key, iv);
else
decipher = crypto.createDecipher(algorithm, key);
decrypted = decipher.update(crypted, input_encoding, output_encoding);
decrypted += decipher.final(output_encoding);
return decrypted;
};