Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 31 Mar, 2023
Improve Article
Save Article
Like Article

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 humanly 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, a 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 messaging systems like WhatsApp etc.  AES, etc are 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 different versions
  • The source code is 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 a crypto module for Hashing the data:

Javascript




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

Run the index.js file using the below command:

node index.js

Output:

Using a 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.

Javascript




// Importing the crypto module
const crypto = require("crypto-js")
const data = "This is the data that need to be encrypted"
const key = "password@111"
 
// Encrypte the data
const 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
const decrypted = crypto.AES.decrypt(encrypted, key)
                                    .toString(crypto.enc.Utf8)
console.log(decrypted)

Run the index.js file using the below command:

node index.js

Output:


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!