Open In App

Node crypto.randomBytes() Method

Last Updated : 18 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

​The crypto.randomBytes() method in Node.js is used to generate cryptographically strong pseudo-random data, which is essential for creating secure keys, tokens, and identifiers. This method is part of the built-in crypto module and is available in Node.js versions 10.x and later.

Syntax: 

crypto.randomBytes( size, callback )

Parameters:

This method accepts two parameters as mentioned above and described below:  

  • size (Number):
    The number of random bytes to generate.
  • callback (Function, optional):
    A function with two arguments: err and buf.
    • If provided, random bytes are generated asynchronously, and the callback is executed once data is ready.
    • If not provided, the method returns the random bytes synchronously as a Buffer.

Returns:

  • With callback: The method returns undefined and invokes the callback with (err, buf) when the random bytes are ready.
  • Without callback: The method returns a Buffer containing the requested number of random bytes.​

The randomBytes method is a part of the Crypto Module. It securely generates random data that can be used as keys, tokens and ids.

Explore the Node.js Crypto Complete Reference to uncover detailed explanations, practical usage examples, and expert tips for harnessing its powerful features to efficiently manage cryptographic operations such as encryption, decryption, secure key generation, signing, and hashing in your Node.js applications.

How to Use crypto.randomBytes()

1. Asynchronous Usage

You can also generate random bytes asynchronously by providing a callback function. This is useful in situations where you don't want to block the execution of other code while waiting for the random bytes to be generated.

Now, let us understand with the help of the example:

JavaScript
// Node.js program to demonstrate the 
// crypto.randomBytes() method

// Including crypto module
const crypto = require('crypto');

// Calling randomBytes method with callback
crypto.randomBytes(127, (err, buf) => {
    if (err) {
        // Prints error
        console.log(err);
        return;
    }

    // Prints random bytes of generated data
    console.log("The random data is: "
        + buf.toString('hex'));
});

Output: Here, callback function is provided so random bytes are generated asynchronously. 

The random data is: 
074e48c8e3c0bc19f9e22dd7570037392e5d0bf80cf9dd51bb7808872a511b3c1cd91053fca873a4cb7b25
49ec1010a9a1a4c2a6aceead9d115eb9d60a1630e056f3accb10574cd563371296d4e4e898941231d06d8
dd5de35690c4ba94ca12729aa316365145f8a00c410a859c40a46bbb4d 5d51995241eec8f6b7a90415e

2. Synchronous Usage

The most common use case for crypto.randomBytes() is generating random data synchronously. In this case, the method directly returns the random bytes as a Buffer.

Now, let us understand with the help of the example:

JavaScript
// Node.js program to demonstrate the 
// crypto.randomBytes() method

// Including crypto module
const crypto = require('crypto');

// Calling randomBytes method without callback
const buf = crypto.randomBytes(60);

// Prints random bytes of generated data
console.log("The random bytes of data generated is: "
    + buf.toString('hex'));

Output: Here, callback function is not provided so bytes are generated synchronously 

The random bytes of data generated is: 
865f38a9950699794e81fcd91584f8612f5a42aec5b7bbed48c1683832c
519c22c836c91fe1afc0330a2ea02dea0a31a1f509dfde1a780ce82ec0eb1

Conclusion

The crypto.randomBytes() method is a powerful tool for generating cryptographically secure random data in Node.js. It is ideal for generating tokens, passwords, salts, and other values that require a high degree of randomness and security. Whether you use it synchronously or asynchronously, it ensures that the random data produced is suitable for cryptographic purposes. By integrating this method into your applications, you can enhance security and build more robust systems.


Next Article

Similar Reads