How does the crypto module work in Node.js
Overview
The crypto module in Node.js provides functionalities such as encrypting, decrypting, and hashing any type of data in a Node.js application. In this shot, we will learn how to check if our Node.js application supports the crypto module.
Check crypto module availability
Below is the code that we can use to check if the crypto module is supported in our Node.js application.
Code
// create a variable called cryptolet crypto;// use the try-catch blocktry {// import crypto if availablecrypto = require('crypto');console.log("crypto is enabled!!!")// log out the moduleconsole.log(crypto)} catch (err) {// log out errorconsole.log('crypto support is disabled!');}
Explanation
- Line 2: We create a variable that will be instantiated using the
cryptomodule. - Line 5: Using the
try-catchblock, we try to access thecryptomodule. - Line7: We import the module if available.
- Line 11: We log the
cryptomodule to the console (if available). - Line 12: We use the
catchblock to catch any errors or exceptions that thetryblock may have produced.
What can the crypto module do for us?
In general, here are some cool things that the crypto module can do for us!
- Cipher: This is a class of the
cryptomodule that is responsible for encrypting our data. For instance, we can use this to encrypt passwords. It only takes an algorithm, a random number, and the data that we want to encrypt. The example below shows how to use cipher, followed by an explanation of the code:
// crypto moduleconst crypto = require("crypto");// the algorithm we are usingconst algorithm = "aes-256-cbc";// generate 16 bytes of random data known as vectorconst randBytes = crypto.randomBytes(16);// sensitive dataconst data = "my password";// secret key generate 32 bytes of random dataconst Securitykey = crypto.randomBytes(32);// the cipher functionconst cipher = crypto.createCipheriv(algorithm, Securitykey, randBytes);// encrypt the message// input encoding// output encodinglet encryptedData = cipher.update(data, "utf-8", "hex");encryptedData = encryptedData + cipher.final("hex")console.log(encryptedData)
In the code above:
- Line 2: We import the crypto module.
- Line 5: We specify the algorithm we are using for encryption, namely
aes-256-cbc. - Line 8: We create a random byte, also known as the vector.
- Line 11: We enter the data that we want to protect.
- Line 14: We create a security code made of 32 bytes that will be used for the encryption.
- Line 17: We create a
cipherfunction using thecreateCipheriv()method. It takes the algorithm, security key, and random bytes as parameters. Another method that we can use iscreateDecipher(). Unfortunately, however, it is deprecated. - Line 22: Using the
update()method of the cipher, we encrypt the data inUFT-8encoding and in a hexadecimal format. - Line 24: We call the
final()method, which ends the encryption. - Line 25: We log the data in the encrypted format to the console.
2. Decipher: This is the opposite of cipher. It decrypts encrypted data.
3. Hash: With the crypto module, we can also create hashes. A hash cannot be converted back to its original form.
Other classes the crypto module uses include certificate, diffieHellman, ECDH, HMAC, Sign, and more.