Stephen Knutter

  Web & Mobile JS Developer

Salt and Hash Passwords with Bcrypt

August 22, 2017

install bcrypt

$ npm i -S bcrypt

generate salt and hash

  const bcrypt = require('bcrypt');

  let _hash;
  
  /*  10 is for rounds */
  bcrypt.genSalt(10, (err, salt) => {

    console.log('salt: ', salt);

   /* Using salt and password 'foobar' to generate hash */
    bcrypt.hash('foobar', salt, (err, hash) => {

      console.log('hash: ', hash);  
      _hash = hash;

    });
  });

  /* This is used to compare a given password to the stored hash */
  bcrypt.compare('foobar', _hash, (err, isMatch) => {

    console.log('is a match: ', isMatch);

  });