Skip to content

crypto.randomBytes documentation needs improvement #3081

Closed
@brendanashworth

Description

The crypto.randomBytes documentation is quite confusing on whether or not the function throws on a lack of entropy. The code shows that it does, but the note below says that it does not. I'm currently under the impression that it does not throw on low entropy, so this needs to be fixed. Here is how it looks now:

crypto.randomBytes(size[, callback])

Generates cryptographically strong pseudo-random data. Usage:

// async
crypto.randomBytes(256, function(ex, buf) {
  if (ex) throw ex;
  console.log('Have %d bytes of random data: %s', buf.length, buf);
});
// sync
try {
  var buf = crypto.randomBytes(256);
  console.log('Have %d bytes of random data: %s', buf.length, buf);
} catch (ex) {
  // handle error
  // most likely, entropy sources are drained
}

NOTE: This will block if there is insufficient entropy, although it should normally never take longer than a few milliseconds. The only time when this may conceivably block is right after boot, when the whole system is still low on entropy.

Refs to: #3073 (comment) and following discussion, e5e5980, f68a116, and RAND_bytes() docs. Also #813.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    cryptoIssues and PRs related to the crypto subsystem.docIssues and PRs related to the documentations.good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions