wallet-core/node_modules/randombytes/browser.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-05-03 15:35:00 +02:00
'use strict'
function oldBrowser () {
throw new Error('secure random number generation not supported by this browser\nuse chrome, FireFox or Internet Explorer 11')
}
2017-08-14 05:01:11 +02:00
var Buffer = require('safe-buffer').Buffer
2017-05-03 15:35:00 +02:00
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)
}
2017-08-14 05:01:11 +02:00
// XXX: phantomjs doesn't like a buffer being passed here
var bytes = Buffer.from(rawBytes.buffer)
2017-05-03 15:35:00 +02:00
if (typeof cb === 'function') {
return process.nextTick(function () {
cb(null, bytes)
})
}
return bytes
}