-
Notifications
You must be signed in to change notification settings - Fork 8
/
crypt.js
executable file
·100 lines (85 loc) · 2.09 KB
/
crypt.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
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
93
94
95
96
97
98
99
100
"use strict";
var crypto = require('crypto');
var util = require('util');
var crypt = {
iv: '@@@@&&&&####$$$$',
encrypt: function (data,custom_key) {
var iv = this.iv;
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 cipher = crypto.createCipher('aes256',key);
var encrypted = cipher.update(data, 'binary', 'base64');
encrypted += cipher.final('base64');
return encrypted;
},
decrypt: function (data,custom_key) {
var iv = this.iv;
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');
try {
decrypted += decipher.final('binary');
} catch (e) {
util.log(util.inspect(e));
}
return decrypted;
},
gen_salt: function (length, cb) {
crypto.randomBytes((length * 3.0) / 4.0, function (err, buf) {
var salt;
if (!err) {
salt = buf.toString("base64");
}
//salt=Math.floor(Math.random()*8999)+1000;
cb(err, salt);
});
},
/* one way md5 hash with salt */
md5sum: function (salt, data) {
return crypto.createHash('md5').update(salt + data).digest('hex');
},
sha256sum: function (salt, data) {
return crypto.createHash('sha256').update(data + salt).digest('hex');
}
};
module.exports = crypt;
(function () {
var i;
function logsalt(err, salt) {
if (!err) {
console.log('salt is ' + salt);
}
}
if (require.main === module) {
var enc = crypt.encrypt('One97');
console.log('encrypted - ' + enc);
console.log('decrypted - ' + crypt.decrypt(enc));
for (i = 0; i < 5; i++) {
crypt.gen_salt(4, logsalt);
}
}
}());