-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
58 lines (48 loc) · 1.6 KB
/
index.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
var crypto = require('crypto');
var rtg = module.exports = {};
// Module defaults
rtg.STRONG_KEY_LEN = 32;
rtg.WEAK_KEY_LEN = 16;
rtg.generateKey = function(opts, done) {
// Setup the option defaults
if (typeof opts === 'function') {
done = opts;
opts = {};
}
opts = opts || {};
opts.len = opts.len || (opts.strong ? rtg.STRONG_KEY_LEN : rtg.WEAK_KEY_LEN);
opts.done = opts.done || done || function() {};
// String can be boolean or a string representing an encoding
if ((!Buffer.isEncoding(opts.string) && opts.string !== false) || opts.string === 'base64' || opts.string === 'utf8') {
opts.string = 'hex';
}
// in some encodings then string is twice the number of characters as the number of bytes
if (opts.string === 'hex') {
opts.len = opts.string ? opts.len / 2 : opts.len;
}
// strong or weak key?
var fnc = opts.strong ? rtg._generateStrongKey : rtg._generateWeakKey;
return fnc(opts.len, opts.string, opts.retry, opts.done);
};
rtg._generateStrongKey = function(len, str, retry, done) {
// Generate the random bytes
return crypto.randomBytes(len, function(err, key) {
if (err && retry === false) {
return done(err);
} else if (err) {
return rtg._generateStrongKey(len, str, false, done);
}
done(null, str ? key.toString(str) : key);
});
};
rtg._generateWeakKey = function(len, str, retry, done) {
// Generate the psuedo-random bytes
return crypto.pseudoRandomBytes(len, function(err, key) {
if (err && retry === false) {
return done(err);
} else if (err) {
return rtg._generateWeakKey(len, str, false, done);
}
done(null, str ? key.toString(str) : key);
});
};