Skip to content

Bycrypt

siddharthakonakanchi edited this page Dec 5, 2017 · 14 revisions

Packages and API used

  1. Description
  2. Installation
  3. API
  4. [Code Snippet](##Code Snippet)
  5. [Database Insertion](##Database Insertion)
  6. Reference

bcrypt

Description

This API or package is used to protect the password to be stored in Database. It uses hash functions to encrypt and to decrypt It incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function.

Installation

npm install bcrypt

Example

var bcrypt = require('bcrypt'); const saltRounds = 10; const myPlaintextPassword = 's0//\P4$$w0rD'; const someOtherPlaintextPassword = 'not_bacon';

Usage in our project

This bycrypt API is used in our project in order to store the passwords in Mongo DB during sign up and reset password API calls

API

/users/signup In order to compute hash and use salt function and encrypt the password /users/reset/:token In order to accept the new password and encrypt it

Code Snippet

/users/signup

bcrypt.genSalt(10, function (err, salt) { bcrypt.hash(password, salt, function (err, hash) {

                        db.collection('users')
                            .update({ _id: tokenInfo.userId }, { $set: { password: hash } })
                            .then((success) => {
                                db.collection('token').remove({ userId: tokenInfo.userId });
                                res.render('response');
                            })

/users/reset/:token

var salt = bcrypt.genSaltSync(10); newUser.password = bcrypt.hashSync(newUser.password, salt);

Database Insertion

birthday : "May 5, 1992" gender : "Male" bio : "nothing" password : "$2a$10$7kGeNWu2hP/LEBoQnBYzzuCvjYmRNYYqkEr2FNIkYREBhGzzK9rp." userId : "U2"

Reference

More information can be obtained from https://www.npmjs.com/package/bcrypt

Clone this wiki locally