Vuong Tran
1 min readAug 14, 2016

Using Node.js bcrypt.js module to hash password

Geek Comic for September 16th — He Needs a Better Password

Using bcrypt.js module to hash password as my first solution when develop app in Node.js. It’s easy to use and fun! Thanks to @Daniel Wirtz

First install module via npm

$ npm install --save bcryptjs

async usage

To hash a password

// ES6
import bcrypt from 'bcryptjs';
bcrypt.hash('my password', 'my salt', (err, hash) => {
// Store hash password in DB
});

To check a password

bcrypt.compare('my password', hash, (err, res) => {
// res == true or res == false
});

sync usage

To hash a password

const hash = bcrypt.hashSync('my password', 'my salt');
// Store hash password to DB

To check a password

const result = bcrypt.compareSync('my password', hash)
// result == true or result == false

That’s it!