Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should I use sync or async crypto.randomBytes in web server app #457

Closed
nwoltman opened this issue Jan 23, 2017 · 2 comments
Closed

Should I use sync or async crypto.randomBytes in web server app #457

nwoltman opened this issue Jan 23, 2017 · 2 comments

Comments

@nwoltman
Copy link

  • Node.js Version: >= 4
  • OS: any
  • Scope: meta
  • Module: crypto

crypto.randomBytes() has two signatures:

// sync
const buf = crypto.randomBytes(256);

// async
crypto.randomBytes(256, (err, buf) => {
  if (err) throw err;
  // ...
});

My question is: Is it recommended to use the sync or the async version of crypto.randomBytes() in a web server application?

I'm guessing the answer will boil down to whether or not the sync version will block the process. I found this comment which suggests that the sync version will probably be "instant" when generating small amounts of bytes. So if I always generate, let's say 512 bytes or less, would it be best to use the sync version? Or perhaps even with a small amount of generated bytes the sync version isn't "fast enough" so it would still be better for latency/throughput to use the async version?

@bnoordhuis
Copy link
Member

The synchronous version can block to acquire entropy (in theory - it's complicated.) If you want to play it safe, use the asynchronous version.

@nwoltman
Copy link
Author

Okay, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants