1 | // Unique ID creation requires a high quality random # generator. In the
|
2 | // browser this is a little complicated due to unknown quality of Math.random()
|
3 | // and inconsistent support for the `crypto` API. We do the best we can via
|
4 | // feature-detection
|
5 | var rng;
|
6 |
|
7 | var crypto = global.crypto || global.msCrypto; // for IE 11
|
8 | if (crypto && crypto.getRandomValues) {
|
9 | // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
|
10 | var rnds8 = new Uint8Array(16);
|
11 | rng = function whatwgRNG() {
|
12 | crypto.getRandomValues(rnds8);
|
13 | return rnds8;
|
14 | };
|
15 | }
|
16 |
|
17 | if (!rng) {
|
18 | // Math.random()-based (RNG)
|
19 | //
|
20 | // If all else fails, use Math.random(). It's fast, but is of unspecified
|
21 | // quality.
|
22 | var rnds = new Array(16);
|
23 | rng = function() {
|
24 | for (var i = 0, r; i < 16; i++) {
|
25 | if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
|
26 | rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
|
27 | }
|
28 |
|
29 | return rnds;
|
30 | };
|
31 | }
|
32 |
|
33 | module.exports = rng;
|