random-byte-browser.js 522 B

123456789101112131415161718192021
  1. 'use strict';
  2. var crypto = typeof window === 'object' && (window.crypto || window.msCrypto); // IE 11 uses window.msCrypto
  3. var randomByte;
  4. if (!crypto || !crypto.getRandomValues) {
  5. randomByte = function(size) {
  6. var bytes = [];
  7. for (var i = 0; i < size; i++) {
  8. bytes.push(Math.floor(Math.random() * 256));
  9. }
  10. return bytes;
  11. };
  12. } else {
  13. randomByte = function(size) {
  14. return crypto.getRandomValues(new Uint8Array(size));
  15. };
  16. }
  17. module.exports = randomByte;