Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / node_modules / request / node_modules / uuid / lib / rng-browser.js
root on 7 May 2019 1 KB Initial commit
  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.  
  6. // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
  7. var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues.bind(crypto)) ||
  8. (typeof(msCrypto) != 'undefined' && msCrypto.getRandomValues.bind(msCrypto));
  9. if (getRandomValues) {
  10. // WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
  11. var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
  12.  
  13. module.exports = function whatwgRNG() {
  14. getRandomValues(rnds8);
  15. return rnds8;
  16. };
  17. } else {
  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.  
  24. module.exports = function mathRNG() {
  25. for (var i = 0, r; i < 16; i++) {
  26. if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
  27. rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
  28. }
  29.  
  30. return rnds;
  31. };
  32. }
Buy Me A Coffee