Socket
Socket
Sign inDemoInstall

randombytes

Package Overview
Dependencies
1
Maintainers
4
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.6 to 2.1.0

30

browser.js
'use strict'
// limit of Crypto.getRandomValues()
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
var MAX_BYTES = 65536
// Node supports requesting up to this number of bytes
// https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js#L48
var MAX_UINT32 = 4294967295
function oldBrowser () {

@@ -18,15 +26,19 @@ throw new Error('Secure random number generation is not supported by this browser.\nUse Chrome, Firefox or Internet Explorer 11')

// 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)
if (size > MAX_UINT32) throw new RangeError('requested too many random bytes')
// This will not work in older browsers.
// See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
var bytes = Buffer.allocUnsafe(size)
if (size > 0) { // getRandomValues fails on IE if size == 0
crypto.getRandomValues(rawBytes)
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
crypto.getRandomValues(bytes.slice(generated, generated + MAX_BYTES))
}
} else {
crypto.getRandomValues(bytes)
}
}
// XXX: phantomjs doesn't like a buffer being passed here
var bytes = Buffer.from(rawBytes.buffer)
if (typeof cb === 'function') {

@@ -33,0 +45,0 @@ return process.nextTick(function () {

{
"name": "randombytes",
"version": "2.0.6",
"version": "2.1.0",
"description": "random bytes from browserify stand alone",

@@ -5,0 +5,0 @@ "main": "index.js",

var test = require('tape')
var randomBytes = require('./')
var MAX_BYTES = 65536
var MAX_UINT32 = 4294967295
test('sync', function (t) {
t.plan(4)
t.plan(9)
t.equals(randomBytes(0).length, 0, 'len: ' + 0)

@@ -10,6 +12,17 @@ t.equals(randomBytes(3).length, 3, 'len: ' + 3)

t.equals(randomBytes(300).length, 300, 'len: ' + 300)
t.equals(randomBytes(17 + MAX_BYTES).length, 17 + MAX_BYTES, 'len: ' + 17 + MAX_BYTES)
t.equals(randomBytes(MAX_BYTES * 100).length, MAX_BYTES * 100, 'len: ' + MAX_BYTES * 100)
t.throws(function () {
randomBytes(MAX_UINT32 + 1)
})
t.throws(function () {
t.equals(randomBytes(-1))
})
t.throws(function () {
t.equals(randomBytes('hello'))
})
})
test('async', function (t) {
t.plan(4)
t.plan(9)

@@ -39,20 +52,32 @@ randomBytes(0, function (err, resp) {

})
})
if (process.browser) {
test('requesting to much throws', function (t) {
t.plan(1)
t.throws(function () {
randomBytes(65537)
randomBytes(17 + MAX_BYTES, function (err, resp) {
if (err) throw err
t.equals(resp.length, 17 + MAX_BYTES, 'len: ' + 17 + MAX_BYTES)
})
randomBytes(MAX_BYTES * 100, function (err, resp) {
if (err) throw err
t.equals(resp.length, MAX_BYTES * 100, 'len: ' + MAX_BYTES * 100)
})
t.throws(function () {
randomBytes(MAX_UINT32 + 1, function () {
t.ok(false, 'should not get here')
})
})
test('requesting to much throws async', function (t) {
t.plan(1)
t.throws(function () {
randomBytes(65537, function () {
t.ok(false, 'should not get here')
})
t.throws(function () {
randomBytes(-1, function () {
t.ok(false, 'should not get here')
})
})
}
t.throws(function () {
randomBytes('hello', function () {
t.ok(false, 'should not get here')
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc