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.
In Node.js, modules are blocks of encapsulated code that communicate with an external application on the basis of their related functionality.
Crypto is an example of core modules 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 --savelet crypto = require('crypto');
crypto.createSign()
methodWe 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.
This method accepts two parameters, as mentioned above and described below:
algorithm: A string type value. If you apply the name of a
options: An optional parameter that is used to control stream behavior. It returns an object.
This method returns a sign object.
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 moduleconst crypto = require('crypto');// Defining the algorithm to be usedconst algo = 'RSA-SHA256';// Creating Sign objectconst sign = crypto.createSign(algo);// Outputconsole.log(sign);