Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

browser.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict'
  2. // limit of Crypto.getRandomValues()
  3. // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
  4. var MAX_BYTES = 65536
  5. // Node supports requesting up to this number of bytes
  6. // https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js#L48
  7. var MAX_UINT32 = 4294967295
  8. function oldBrowser () {
  9. throw new Error('Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11')
  10. }
  11. var Buffer = require('safe-buffer').Buffer
  12. var crypto = global.crypto || global.msCrypto
  13. if (crypto && crypto.getRandomValues) {
  14. module.exports = randomBytes
  15. } else {
  16. module.exports = oldBrowser
  17. }
  18. function randomBytes (size, cb) {
  19. // phantomjs needs to throw
  20. if (size > MAX_UINT32) throw new RangeError('requested too many random bytes')
  21. var bytes = Buffer.allocUnsafe(size)
  22. if (size > 0) { // getRandomValues fails on IE if size == 0
  23. if (size > MAX_BYTES) { // this is the max bytes crypto.getRandomValues
  24. // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
  25. for (var generated = 0; generated < size; generated += MAX_BYTES) {
  26. // buffer.slice automatically checks if the end is past the end of
  27. // the buffer so we don't have to here
  28. crypto.getRandomValues(bytes.slice(generated, generated + MAX_BYTES))
  29. }
  30. } else {
  31. crypto.getRandomValues(bytes)
  32. }
  33. }
  34. if (typeof cb === 'function') {
  35. return process.nextTick(function () {
  36. cb(null, bytes)
  37. })
  38. }
  39. return bytes
  40. }