Explain the use of crypto module in Node.js
Last Updated :
31 Mar, 2023
In this article, we will explore the crypto module and what are its uses in Node.js. NodeJS supports a large number of third-party modules. These modules can be used for performing different kinds of tasks. The crypto module is also a 3rd party module that can be imported and used in NodeJS. This module can be used for encrypting, decrypting, or hashing any type of data. This encryption and decryption basically help to secure and add a layer of authentication to the data. The main use case of the crypto module is to convert the plain readable text to an encrypted format and decrypt it when required.
The Crypto and the ByCrypto are the two different 3rd parties module that can be used for the protection of sensitive data. The main difference between Crypto and ByCrypto is that ByCrypto provides more powerful hashing than that compared to the Crypto module.Â
The plain text is in the human-readable form and generally consists of Alphabet and words. After encrypting this text using the Crypto module, it will be changed to a computer-readable format. For Eg: Something like this: sdfasc1asT67W2sqWwsdfsadf
Cryptography Mechanism:
Hashing: In this mechanism, a series of plain text is basically converted to Ciphertext. This is a one-way Cryptographic algorithm since we cannot convert this cipher text to plain text again. This method is basically used during the user authentication of Systems while providing Sensitive passwords. Since passwords cannot be stored, these ciphertexts will be stored instead. Some popular hashing algorithms are: Message Digest 5(MD5), RSA, SHA, etc are Widely used algorithms for hashing.
Encryption and Decryption: In this mechanism, a series of plain text is converted to encrypted text with the help of a secret key and then decrypted using the same key. The encrypted text cannot be converted to the original text without a secret key. This algorithm takes input for encrypted text and the secret key which will return the original text as an output. This mechanism is mostly used in Messaging systems to prevent the network from any type of data leak. Some popular mechanisms are AES, DES, etc.
Features:Â
- Easy to use
- Widely used algorithms with multiple options to encrypt and decrypt
- Cleaner and Consistent Code
- Can be easily integrated with Javascript code in NodeJS
Installing module:
npm install crypto-js --save
Example 1: Using SHA256 from the crypto-js module.
JavaScript
// Importing module
const SHA256 = require("crypto-js/sha256");
// Initializing the original data
const originalData = "Welcome To GeeksForGeeks"
// Hashing the Original data
const hasheddata = SHA256(originalData).toString()
// Printing hashed data
console.log("Hashed Data is: " + hasheddata)
Output:Â
Hashed Data is: ecf0cc86124bb4191a1a10f48b1eb2a7b3b3c7aa8c38ac8de8ad6c0a1502b985
Example 2: Using a crypto module for encryption and decryption of the data. We can use a single key for the encryption and then use the same key for decryption as well.
JavaScript
// Importing the crypto module
const crypto = require("crypto-js")
// Initializing the original data
const data = "This is the data that need to be encrypted"
// Defining the secret key
const key = "pwd@1234"
// Encrypting the data using the password key
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 using the same password key
const decrypted = crypto.AES.decrypt(encrypted, key)
.toString(crypto.enc.Utf8)
console.log(decrypted)
Output:

Reference: https://www.npmjs.com/package/crypto-js
Similar Reads
Node JS | Password Hashing with Crypto module In real-life applications with User authentication functionality, storing the user passwords as the original string in the database is not practical. Still, it is good practice to hash the password and then store them in the database. Crypto module for Node JS helps developers to hash user passwords
5 min read
Password Encryption in Node.js using bcryptjs Module When developing applications, one of the critical aspects of user authentication is ensuring that passwords are stored securely. Plain text storage of passwords is a significant security risk. Instead, passwords should be encrypted using strong hashing algorithms. In Node.js, one of the popular modu
3 min read
What is the purpose of module.exports in node.js ? The module.exports is actually a property of the module object in node.js. module. Exports is the object that is returned to the require() call. By module.exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() meth
3 min read
What is Crypto Module in Node.js and How it is used ? The crypto module in Node.js provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions. This module enables you to perform various security operations, such as hashing, encryption, and decryption, directly in your Node
4 min read
What are modules in Node JS ? In NodeJS, modules are encapsulated units of code that can be reused across different parts of an application. Modules help organize code into smaller, manageable pieces, promote code reusability, and facilitate better maintainability and scalability of NodeJS applications. Types of Modules:Core Mod
2 min read