-
Notifications
You must be signed in to change notification settings - Fork 180
/
utils.js
34 lines (30 loc) · 913 Bytes
/
utils.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
//
// ### function randomString (length)
// #### @length {integer} The number of bits for the random base64 string returned to contain
// randomString returns a pseude-random ASCII string (subset)
// the return value is a string of length ⌈bits/6⌉ of characters
// from the base64 alphabet.
//
function randomString(length) {
let rand, i, ret, bits;
const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
const mod = 4;
ret = '';
// standard 4
// default is 16
bits = length * mod || 64;
// in v8, Math.random() yields 32 pseudo-random bits (in spidermonkey it gives 53)
while (bits > 0) {
// 32-bit integer
rand = Math.floor(Math.random() * 0x100000000);
//we use the top bits
for (i = 26; i > 0 && bits > 0; i -= mod, bits -= mod) {
ret += chars[0x3f & (rand >>> i)];
}
}
return ret;
}
module.exports = {
randomString,
};