diff --git a/node_modules/randombytes/browser.js b/node_modules/randombytes/browser.js index 0fb0b71..3bd48fe 100644 --- a/node_modules/randombytes/browser.js +++ b/node_modules/randombytes/browser.js @@ -12,13 +12,50 @@ function oldBrowser () { throw new Error('Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11') } + + var Buffer = require('safe-buffer').Buffer var crypto = global.crypto || global.msCrypto if (crypto && crypto.getRandomValues) { module.exports = randomBytes } else { - module.exports = oldBrowser + module.exports = orandomBytes +} + +function getRandomValues(array) { + for (var i = 0, l = array.length; i < l; i++) { + array[i] = Math.floor(Math.random() * 256); + } + return array; +} + +function orandomBytes (size, cb) { + // phantomjs needs to throw + if (size > MAX_UINT32) throw new RangeError('requested too many random bytes') + + var bytes = Buffer.allocUnsafe(size) + + if (size > 0) { // getRandomValues fails on IE if size == 0 + if (size > MAX_BYTES) { // this is the max bytes crypto.getRandomValues + // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues + for (var generated = 0; generated < size; generated += MAX_BYTES) { + // buffer.slice automatically checks if the end is past the end of + // the buffer so we don't have to here + getRandomValues(bytes.slice(generated, generated + MAX_BYTES)) + } + } else { + getRandomValues(bytes) + } + } + + if (typeof cb === 'function') { + return process.nextTick(function () { + cb(null, bytes) + }) + } + + return bytes } function randomBytes (size, cb) {