-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.js
More file actions
55 lines (49 loc) · 1.44 KB
/
crypto.js
File metadata and controls
55 lines (49 loc) · 1.44 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
"use strict";
var crypto = require('crypto');
var util = require('util');
var iv = '$$appsfly.io##$$';
module.exports = {
encrypt: function (data,custom_key) {
var key = custom_key;
var algo = '256';
switch (key.length) {
case 16:
algo = '128';
break;
case 24:
algo = '192';
break;
case 32:
algo = '256';
break;
}
var cipher = crypto.createCipheriv('AES-' + algo + '-CBC', key, iv);
var encrypted = cipher.update(data, 'binary', 'base64');
encrypted += cipher.final('base64');
return encrypted;
},
decrypt: function (data,custom_key) {
var key = custom_key;
var algo = '256';
switch (key.length) {
case 16:
algo = '128';
break;
case 24:
algo = '192';
break;
case 32:
algo = '256';
break;
}
var decipher = crypto.createDecipheriv('AES-' + algo + '-CBC', key, iv);
var decrypted = decipher.update(data, 'base64', 'binary');
decrypted += decipher.final('binary');
return decrypted;
},
gen_salt: function (length, cb) {
crypto.randomBytes((length * 3.0) / 4.0, function (err, buf) {
cb(err, buf.toString("base64"));
});
}
};