random.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.randomBytes = void 0;
  4. /**
  5. * A **Cryptographically Secure Random Value** is one that has been
  6. * generated with additional care take to prevent side-channels
  7. * from allowing others to detect it and prevent others from through
  8. * coincidence generate the same values.
  9. *
  10. * @_subsection: api/crypto:Random Values [about-crypto-random]
  11. */
  12. const crypto_js_1 = require("./crypto.js");
  13. let locked = false;
  14. const _randomBytes = function (length) {
  15. return new Uint8Array((0, crypto_js_1.randomBytes)(length));
  16. };
  17. let __randomBytes = _randomBytes;
  18. /**
  19. * Return %%length%% bytes of cryptographically secure random data.
  20. *
  21. * @example:
  22. * randomBytes(8)
  23. * //_result:
  24. */
  25. function randomBytes(length) {
  26. return __randomBytes(length);
  27. }
  28. exports.randomBytes = randomBytes;
  29. randomBytes._ = _randomBytes;
  30. randomBytes.lock = function () { locked = true; };
  31. randomBytes.register = function (func) {
  32. if (locked) {
  33. throw new Error("randomBytes is locked");
  34. }
  35. __randomBytes = func;
  36. };
  37. Object.freeze(randomBytes);
  38. //# sourceMappingURL=random.js.map