What is Node crypto.createSign( algorithm, options )?

Node.js

Node is an open-source, cross-platform JavaScript runtime environment based on Google’s V8 JavaScript Engine, used to run web applications outside the client’s browser.

Node.js is used to create server-side web applications, which is perfect for data-intensive applications since it uses an asynchronous, event-driven model.

Node.js modules

In Node.js, modules are blocks of encapsulated code that communicate with an external application on the basis of their related functionality.

Modules are of three types:

  • Core modules
  • Local modules
  • Third-party modules

Crypto is an example of core modules in Node JS.

Crypto in Node.js

Crypto is a module in Node.js which deals with an algorithm that performs data encryption and decryption.

It is used for security purposes like user authentication where the password is stored in the database in encrypted form.

The Crypto module provides a set of classes like hash, HMAC, cipher, decipher, sign, and verify. The instance of the class is used to create encryption and decryption.

Install the Crypto module in Node.js in the following way:

npm install crypto --save
let crypto = require('crypto');

Node.js | crypto.createSign() method

We use the crypto.createSign() method to create a sign object using the stated algorithm. The crypto.getHashes() method is used to access the names of all the available digest algorithms syntax.

Parameters:

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

  • algorithm: A string type value. If you apply the name of a signature algorithmFor example, ‘RSA-SHA256’ in place of a digest algorithm, a sign instance can be created.

  • options: An optional parameter that is used to control stream behavior. It returns an object.

Return value:

This method returns a sign object.

Example

The example below illustrates the use of the crypto.createSign() method in Node.js:

// Node.js program to demonstrate the
// crypto.createSign() method
// Including crypto module
const crypto = require('crypto');
// Defining the algorithm to be used
const algo = 'RSA-SHA256';
// Creating Sign object
const sign = crypto.createSign(algo);
// Output
console.log(sign);

Free Resources