-
Notifications
You must be signed in to change notification settings - Fork 3
/
cryptoUtil.js
executable file
·57 lines (47 loc) · 2.05 KB
/
cryptoUtil.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
/*
| crypto.js
| ----
| some basic, general, crypto functions; requires the dev to provide the base salt when getting the root object
|
*/
var crypto = require('crypto');
//** provides an instance of the crypto module using the provided base salt
module.exports = function(baseSalt) {
var lib = {
//** generates sha1 and sha256 hashes, with the option to specify encoding, defaulting to hex
sha1: function(data, encoding) {
return crypto.createHash('sha1').update(data).digest(encoding||'hex');
},
sha256: function(data, encoding) {
return crypto.createHash('sha256').update(data).digest(encoding||'hex');
},
md5: function(data, encoding) {
return crypto.createHash('md5').update(data).digest(encoding||'hex');
},
hash: function(type, data, encoding) {
//** type: md5, sha1, sha256, sha512
return crypto.createHash(type).update(data).digest(encoding||'hex');
},
//** creates a hash for a password using a salt and key strengthening, using the default encoding 'hex'
password: function(text, hashFn, salt) {
salt = salt||baseSalt;
hashFn = hashFn||lib.sha256; //** default the password hash function to sha256
var hash = text + salt;
for(var i=0;i<100;i++) //** hash & re-hash the text and salt 100x to strenthen it
hash = hashFn(hash + text + salt);
return hash;
},
aesEncrypt: function(data, encoding) {
encoding = encoding||'base64';
var cipher = crypto.createCipher('aes-256-cbc', baseSalt);
var encpw = cipher.update(data, 'utf8', encoding);
return (encpw += cipher.final(encoding));
},
aesDecrypt: function(data, encoding) {
var decipher = crypto.createDecipher('aes-256-cbc', baseSalt);
var pw = decipher.update(data, encoding||'base64', 'utf8');
return (pw += decipher.final('utf8'));
}
}
return lib;
}