Introduction

In this article, we will be using the bcryptjs javascript library for hashing and comparing passwords. Here, we will build a simple API for registering and logging. We will hash the password when the user registers, and then compare that password with the hash when they log in.

What is bcrypt?

bcrypt is a password hashing function designed by Niels Provos and David Mazières, based on the Blowfish cipher, and presented at USENIX in 1999. Besides incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function. Over time, the iteration count can be increased to make it slower, so it remains resistant to brute-force search attacks even with increasing computation power.

What is hashing?

Hashing is a one-way function (well, a mapping). It's irreversible, you apply the secure hash algorithm and you cannot get the original string back. The most you can do is to generate what's called "a collision", that is, finding a different string that provides the same hash. Cryptographically secure hash algorithms are designed to prevent the occurrence of collisions. You can attack a secure hash using a rainbow table, which you can counteract by applying salt to the hash before storing it.

Setup Project Folder

Setup Node In Project

Install Packages

About packages

  1. Express: It is a framework on which our application will be built.
  2. Body-parser: Extract the entire body portion of an incoming request stream and expose it on the req. body.
  3. Mongoose: Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js.It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.
  4. Bcryptjs: this is a Javascript library by which we can hash and compare passwords.

Create Model

Now we set our application start point

Output

References