forked from apim-haufe-io/wicked.env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypt-tools.js
42 lines (33 loc) · 1.14 KB
/
crypt-tools.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
'use strict';
var crypto = require('crypto');
var ALGORITHM = 'aes-256-ctr';
var cryptTools = function () { };
cryptTools.createRandomId = function() {
return crypto.randomBytes(20).toString('hex');
};
function getCipher(keyText) {
var key = keyText.toString("binary");
var cipher = crypto.createCipher(ALGORITHM, key);
return cipher;
}
cryptTools.apiEncrypt = function (keyText, text) {
//debug('apiEncrypt() - clear text: ' + text);
var cipher = getCipher(keyText);
// Add random bytes so that it looks different each time.
var cipherText = cipher.update(cryptTools.createRandomId() + text, 'utf8', 'hex');
cipherText += cipher.final('hex');
return cipherText;
};
function getDecipher(keyText) {
var key = keyText.toString("binary");
var decipher = crypto.createDecipher(ALGORITHM, key);
return decipher;
}
cryptTools.apiDecrypt = function(keyText, cipherText) {
var decipher = getDecipher(keyText);
var text = decipher.update(cipherText, 'hex', 'utf8');
text += decipher.final('utf8');
text = text.substring(40); // Strip random bytes
return text;
};
module.exports = cryptTools;