Clean up random bytes

This commit is contained in:
Emil Bay 2020-06-24 13:37:07 +02:00
parent fc90cbedba
commit 4bb40aa43d

View File

@ -1,7 +1,8 @@
var assert = require('nanoassert') var assert = require('nanoassert')
var randombytes = (function () { var randombytes = (function () {
var QUOTA = 65536 // limit for QuotaExceededException var QUOTA = 65536 // limit for QuotaExceededException
var crypto = typeof global !== 'undefined' ? crypto = (global.crypto || global.msCrypto) : null var crypto = global.crypto || global.msCrypto
function browserBytes (out, n) { function browserBytes (out, n) {
for (var i = 0; i < n; i += QUOTA) { for (var i = 0; i < n; i += QUOTA) {
@ -17,19 +18,18 @@ var randombytes = (function () {
throw new Error('No secure random number generator available') throw new Error('No secure random number generator available')
} }
if (crypto && crypto.getRandomValues) {
return browserBytes
} else if (typeof require !== 'undefined') {
// Node.js.
crypto = require('crypto') crypto = require('crypto')
if (crypto && crypto.randomBytes) { if (crypto && crypto.getRandomValues) return browserBytes
return nodeBytes
} if (require != null) {
// Node.js. Bust Browserify
crypto = require('cry' + 'pto')
} }
return noImpl return noImpl
})() })()
// Make non enumerable as this is an internal function
Object.defineProperty(module.exports, 'randombytes', { Object.defineProperty(module.exports, 'randombytes', {
value: randombytes value: randombytes
}) })