Microservices Encryption and Decryption in Node.JS using Bcrypt

Vakindu Philliam
3 min readJan 9, 2020

--

Photo with the words Crypto Dollar

Today we look at a micro-services implementation of how to encrypt and decrypt passwords using the ‘bcrypt’ node module. A micro-services architecture means you split a software project into multiple directories to ease product design and management.
Data encryption and decryption is indispensable from modern software development. Encryption and decryption technologies are used for building web authentication securities, securing blockchain nodes and protecting user privacy in communication channels.
This tutorial assumes you have prior knowledge and experience in working with JavaScript, Node, NPM (Node Package Manager) and Bcrypt.

Step 1: Download Node.js and install dependency modules.
Download and install the node server by visiting and following the steps on their portal http://nodejs.org.
The latest versions of Node.JS come installed with NPM, so you don’t need to re-install the Node Package Manager.

Step 2: Initialize your project
Create a folder for your project, for example /MyEncryption.
Then initialize the project by running the command below in that project folder.

npm init

The above command will create a package.json file that will keep a track on all your project’s dependency modules.

Step 3: Install bcrypt
Run the command below to install the ‘bcrypt’ module.

npm install bcrypt — save

Step 4: Split project
Create a new folder inside /MyEncryption and name it, for example /helpers
Then create a new file inside the /helpers folder called helper.js

Step 5: Start coding
Copy and paste the code below into the helper.js file.

// helper.js
// Import dependency modules
const bcrypt = require(‘bcrypt’);

// Password encryption Function
const encryptPWD =(password)=>{

// Hash password and salt with md5 encryption
return bcrypt.hashSync(password, bcrypt.genSaltSync(10),null);
};

// Password comparison Function
const comparePWD = (password1, password2) => {

// Compare two passwords
return bcrypt.compareSync(password1, password2);
};

// Export module
module.exports={encryptPWD, comparePWD};

Explanation:
We import the ‘bcrypt’ module into our file.
We create a function called ‘encryptPWD’ that takes in an argument and hashes it with md5 salted encryption. The function then return the encrypted value.
We go on to create another function called ‘comparePWD’ that takes in the two password arguments that we wish to compare. Remember that, one is hashed, the other is not. The function then compares their hashed values and returns a true or false, an if or not whether they match.
The functions are then exported from the helpers folder to the main project file.

Step 6: Import helper.js into main project
Finally we navigate back to our main /MyEncryption folder and create a new file in that directory called index.js.
Then copy and paste the code below into that index.js file.

//Import helper function
const {encryptPWD,comparePWD} = require(‘./helper’);

// Encryption example
const encryptedPWD = encryptPWD(“Vakindu Philliam”)
console.log(encryptedPWD);

// Decryption example if Passwords match
const matcher_True = comparePWD(“Vakindu Philliam”, encryptedPWD)
console.log(matcher_True); // True

// Decryption example if Passwords do not match
const matcher_False = comparePWD(“Pyramid IO”, encryptedPWD)
console.log(matcher_False); // False

Explanation
We import the helper.js functions into our main index.js file.
We then pass parameters to the functions and execute the node project by running the command below.

node index.js

The ‘encryptPWD’ function will return an encrypted value.
You’ll notice that the ‘matcherTrue’ will read ‘true’.
And you’ll get a false reading for ‘matcherFalse’ since the passwords don’t match.

Done and dusted!!
This article is a micro-services implementation of an article I published earlier on Node password encryption and decryption using ‘Bcrypt’. Find the article here: http://medium.com/@VakinduPhilliam/hashed

Conclusion:
Thanks for reading this post. Hope it helps you build something amazing.
Find me on;
Github: http://Github.com/VakinduPhilliam
Twitter: http://Twitter.com/VakinduPhilliam

--

--

Vakindu Philliam
Vakindu Philliam

Written by Vakindu Philliam

Below average chess player. Imperfect. A Work in Progress. Backend Developer. Blockchain Developer. Data Science. Christ loved me first. 1 John 4:19

No responses yet