Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

What is crypto module in Node.js and how it is used ?

  • Difficulty Level : Hard
  • Last Updated : 19 Sep, 2021

Node.js is an open-source and cross-platform runtime environment built on Chrome’s V8 JavaScript engine for executing JavaScript code outside of a browser. Node.js supports a large number of third-party modules that help to perform many different kinds of tasks. Crypto module is one of the third-party modules that help encrypt or decrypt or hash any data. which we want to secure from outside the world. The main function of this module is to convert the plain text or data to the encrypted format(hashed, CipherText) which is non-readable.

The crypto and bycrypto two third parties modules are used to protect sensitive data. The main difference between crypto and crypto module is bycrypto module performs powerful hashing as compared to the crypto module. So if we want to perform the powerful hashing we will prefer bycrypto module otherwise use crypto module.

Plain text: Anything that we write or type which is human understandable is called plain text. It can contain any character(a-zA-Z0-9!,@,#….). Eg. our password

Ciphertext: sdfasc1asT67W2sqWwsdfsadf Are you able to understand this word? this was a ciphertext is, nonreadable and nonunderstandable text which is generated by passing plain text through an algorithm.  

 



The mechanism in Cryptography:

Hashing: This is a mechanism to convert a plain text to ciphertext. It is a one-way cryptographic function i.e, we can’t convert cipher text to plain text. It is widely used in authentication systems to avoid storing plain text passwords in databases but is also used to validate files, documents, and other types of data.  Message Digest 5(MD5), RSA, SHA, etc are Widely used algorithms for hashing.

Encryption and Decryption: Encryption algorithms take input and a secret key and generate a random-looking output called a ciphertext. This operation is reversible. Decryption is the reverse of encryption. This algorithm takes the same secret key and ciphertext and it returns back our original plain text. This is widely used in a messaging system like WhatsApp etc.  AES, etc re Widely used algorithms for encryption and decryption.

Features of Crypto in Node.js:  

  • It’s easy to get started
  • A lot of widely used algorithms are there with their different versions
  • Source code cleaner and consistent.
  • It uses JavaScript everywhere so you can use it with node.js

Installing module:

npm install crypto-js --save

Project Structure:

we can use this module in two ways either for the hashing or either use in encryption and decryption of the data. There are a lot of algorithms available for hashing as well as encryption and decryption of the data.



Using crypto module for Hashing the data:

index.js




// Importing module
var SHA256 = require("crypto-js/sha256");
 var plaindata = "GeeksForGeeks"
var hasheddata = SHA256(plainText).toString()
console.log(hasheddata)

Run index.js file using below command:

node index.js

Output:

Using crypto module for encryption and decryption of the data:

We will use the key for encryption and decryption of the data.A single key can be used for the encryption of the data as well as in the decryption process of the data. Below is an example of the encryption and decryption of the data using a single key.

index.js




// Importing the crypto module
const crypto=require("crypto-js")
  
var data = "This is the data that need to be encrypted"
  
var key = "password@111"
  
// Encrypte the data
var encrypted = crypto.AES.encrypt(data, key).toString();
console.log("Encrypted data")
  
// Printing the encrypted data
console.log(encrypted)
console.log("Decrypted data")
  
// Decrypting the data
var decrypted = crypto.AES.decrypt(encrypted, key)
    .toString(crypto.enc.Utf8)
console.log(decrypted)

Run index.js file using below command:

node index.js

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :