Unlocking the Secrets: Mastering Bcrypt Encryption and Decryption in NodeJs
Introduction
In this article, we will be looking at the importance of encryption of passwords and how to go about it. I'll be working with the Express.js framework and MongoDB database in this article.
What is Encryption and Decryption?
Encryption is the conversion of data into an unreadable and secure format which is commonly known as ciphertext, This conversion helps to prevent unauthorized access to the encrypted data.
Decryption is the reverse action of encryption, in decryption you aim to access the encrypted data in the initial readable form also known as plaintext. To decrypt data you will need a specific key, this key has to be the exact one used during the encryption of that specific data.
When to Encrypt?
Encryption should take place when you have data that shouldn't be public, examples of such data include user passwords, wallet addresses, social security numbers, credit card info and much more.
Encryption and Decryption in NodeJs
To encrypt or decrypt in NodeJs we will need a package called bcrypt
.
Installing Bcrypt :
npm install bcrypt
Now that bcrypt
is installed let's start with our encryption function
Encryption
To encrypt data you will make use of two bcrypt methods
genSalt()
hash()
genSalt()
: This method, just as its name it's used to generate a salt. Salt can be seen as the key used in encryption, to restrict unauthorized access. The genSalt()
method takes just one value which is known as saltRound. The saltRound is an integer value that states the computational cost of the salt . The higher the saltRound the more secure it is, as expected a higher salt number would also result in a longer time to compute.
I would recommend a saltRound of 10.
const bcrypt = require("bcrypt");
const saltRounds = 10;
const Encryption = async (data) => {
try {
const salt = await bcrypt.genSalt(saltRounds);
} catch (err) {
console.log(err);
}
};
hash()
: This is the main method that encrypts the data, using the salt provided. The hash()
method receives two parameters- the data to be encrypted and the salt to be used for the encryption.
const bcrypt = require("bcrypt");
const saltRounds = 10;
const Encryption = async (data) => {
try {
//Generating Salt
const salt = await bcrypt.genSalt(saltRounds);
//Encrypting Data
const encryptedData = await bcrypt.hash(data, salt);
return encryptedData;
} catch (err) {
console.log(err);
}
};
Decryption
While working with bcrypt
the case of decryption is quite different as bcrypt
is a one-way hashing algorithm, Hence what it does is hash the value you want to compare with, using the same algorithm and salt. Then it proceeds to compare the two hashed values to see if it's a match
const Decryption = async (pswd, hash) => {
try {
var result = await bcrypt.compare(pswd, hash);
if (result) {
console.log("Password is correct");
} else {
console.log("Password is incorrect");
}
} catch (err) {
console.log(err);
}
};
Conclusion
As you have seen in the article, Encryption is the conversion of data into a protected format for security reasons, while Decryption is gaining access to the encrypted data.
Encryption and Decryption can be achieved in node.js by using an external library called Bcrypt.
If you enjoyed this piece, please like it ๐๐ผ and don't hesitate to share it with others. For more articles from this blog hit the subscribe button :)