Node.js crypto.setFips() Function

The crypto.setFips() method is an inbuilt application programming interface of class Crypto within the crypto module which is used to enable the FIPS compliant crypto provider in a FIPS-enabled Node.js build.

Syntax:

const crypto.setFips(bool)

Parameters: This API takes the Boolean value as a parameter.

Return Value: This API does not return anything.

 

Example 1:

index.js




// Node.js program to demonstrate the  
// crypto.setFips() method
  
// Importing crypto module
const crypto = require('crypto')
  
// Checking for error
try {
  
    // Enabling the FIPS compliant 
    // crypto provider by using
    // setFips() method
    const val = crypto.setFips(true);
  
    // Display the result
    console.log("FIPS compliant crypto"
        + " provider has been enabled")
  
} catch (e) {
      
    // Display error if any
    console.log("FIPS mode is not available.");
}


Run the index.js file using the following command:

node index.js

Output:

FIPS mode is not available.

Example 2:

index.js




// Node.js program to demonstrate the  
// crypto.setFips() method
  
// Importing crypto module
const crypto = require('crypto')
  
// Enabling the FIPS compliant 
// crypto provider by using
// setFips() method
try {
  
    // Function call
    const val = crypto.setFips(false);
  
    // Display the result
    console.log("FIPS compliant crypto "
        + "provider has been disabled");
} catch (e) {
    console.log("ERR_CRYPTO_FIPS_UNAVAILABLE")
}


Output:

ERR_CRYPTO_FIPS_UNAVAILABLE

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



Contact Us