Open In App

Node.js crypto.randomUUID( ) Function

The crypto.randomUUID() is  an inbuilt application programming interface of class Crypto within crypto module which is used to generate a random RFC 4122 Version 4 UUID.

Syntax:



const crypto.randomUUID([options])

Parameters: This function takes the disableEntropyCache as a parameter

Return Value: This function returns a random RFC 4122 Version 4 UUID.



Example 1:




<script>
  // Node.js program to demonstrate the  
  // crypto.randomUUID() api
  
  // Importing crypto module
  const crypto = require('crypto')
  
  // getting a random RFC 4122 Version 4 UUID
  // by using randomUUID() method
  const val = crypto.randomUUID({disableEntropyCache : true});
  
  // display the result
  console.log("RFC 4122 Version 4 UUID : " + val)
</script>

Run the index.js file using the following command.

node index.js

Output:

RFC 4122 Version 4 UUID : 88368f2a-d5db-47d8-a05f-534fab0a0045

Example 2:




<script>
  // Node.js program to demonstrate the  
  // crypto.randomUUID() api
  
  // Importing crypto module
  const crypto = require('crypto')
  
  // getting a random RFC 4122 Version 4 UUID
  // by using randomUUID() method
  console.log(crypto.randomUUID())
</script>

Run the index.js file using the following command.

node index.js

Output:

e2d3286f-2d8f-471a-bacb-1e5d28d8727e

Reference: https://nodejs.org/dist/latest-v15.x/docs/api/crypto.html#crypto_crypto_randomuuid_options


Article Tags :