Generating cryptographically secure random byte sequences
When building applications that leverage cryptography, it's very common to have to generate random byte sequences, and we'll encounter that in every chapter of this book. For example, we'll use random byte sequences as encryption keys (as in Chapter 4, Symmetric Encryption in Node.js) and as salt for hashes (Chapter 3, File and Password Hashing with Node.js).
Thankfully, Node.js already includes a function to generate random data in the crypto
module: randomBytes(size, callback)
.
The importance of randomness
In this book, just as in real-life applications, we're going to use random byte sequences for highly sensitive operations, such as generating encryption keys. Because of that, it's of the utmost importance to be able to have something as close as possible to true randomness. That is: given a number returned by our random number generator, an attacker should not be able to guess the next...