Sha256: bbb71f80cd24e63c060d5a106a89590f07fe6c9e9de908060a0cf82e7f95c819

Contents?: true

Size: 1.01 KB

Versions: 8

Compression:

Stored size: 1.01 KB

Contents

'use strict'

function oldBrowser () {
  throw new Error('secure random number generation not supported by this browser\nuse chrome, FireFox or Internet Explorer 11')
}

var crypto = global.crypto || global.msCrypto

if (crypto && crypto.getRandomValues) {
  module.exports = randomBytes
} else {
  module.exports = oldBrowser
}

function randomBytes (size, cb) {
  // phantomjs needs to throw
  if (size > 65536) throw new Error('requested too many random bytes')
  // in case browserify  isn't using the Uint8Array version
  var rawBytes = new global.Uint8Array(size)

  // This will not work in older browsers.
  // See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
  if (size > 0) {  // getRandomValues fails on IE if size == 0
    crypto.getRandomValues(rawBytes)
  }
  // phantomjs doesn't like a buffer being passed here
  var bytes = new Buffer(rawBytes.buffer)

  if (typeof cb === 'function') {
    return process.nextTick(function () {
      cb(null, bytes)
    })
  }

  return bytes
}

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
lanes-0.8.0 node_modules/randombytes/browser.js
minimum_viable_product-0.0.11 test/dummy/node_modules/randombytes/browser.js
brwy_rails-0.0.6 test/dummy/node_modules/randombytes/browser.js
brwy_rails-0.0.5 test/dummy/node_modules/randombytes/browser.js
brwy_rails-0.0.4 test/dummy/node_modules/randombytes/browser.js
brwy_rails-0.0.3 test/dummy/node_modules/randombytes/browser.js
brwy_rails-0.0.2 test/dummy/node_modules/randombytes/browser.js
brwy_rails-0.0.1 test/dummy/node_modules/randombytes/browser.js