Skip to content

crypto.randomBytes shows a confusing error #15118

@benjamingr

Description

@benjamingr

Reproduced on Node.js 8.4 on Mac and 8.2 on Linux.

Currently, we show the following error when crypto.randomBytes is called with a non-integer:

> crypto.randomBytes(19660.79999999999)
TypeError: size must be a number >= 0
    at TypeError (native)

Which is confusing, since it is in fact a number bigger than 0.

To add to the confusion, we ran into this issue myself when using a third party package (myspeed) using another package (noisegen) that did division that should probably have been an integer number but wasn't.


We should either:

  • Fix the error message
  • Fix the API to accept a non-integer. By changing the check to check if a number is passed and coerce it to a UInt32. Personally I'm in favor of that.

Pinging @nodejs/crypto and @mrbar42 who discovered this

Would love it if some people weighed in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    cryptoIssues and PRs related to the crypto subsystem.discussIssues opened for discussions and feedbacks.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions