Open In App

Node crypto.randomBytes() Method

Last Updated : 12 Apr, 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.

Example 1: This example demonstrates the use of crypto.randomBytes() method in Node.js:

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

Example 2: Below examples illustrate the use of crypto.randomBytes() Method in Node.js:

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

We have a Cheat Sheet on Node crypto methods where we covered all the crypto methods to check those please go through Crypto Module Complete Reference this article.


Next Article

Similar Reads