How to use the hashAsync function from bcrypt-nodejs

Find comprehensive JavaScript bcrypt-nodejs.hashAsync code examples handpicked from public code repositorys.

7
8
9
10
11
12
13
14
15
16
    return
  }

  return bcrypt
    .genSaltAsync(SALT_FACTOR)
    .then(salt => bcrypt.hashAsync(user.password, salt, null))
    .then(hash => {
      user.setDataValue('password', hash)
    })
}
fork icon197
star icon506
watch icon37

41
42
43
44
45
46
47
48
49
50
// normalize email address
self.set('email', self.get('email').toLowerCase().trim());

// salt and hash
return bcrypt.genSaltAsync(10).then(function(salt){
  return bcrypt.hashAsync(self.get('password'), salt, null).then(function(hash){
      self.set('password', hash);
      self.set('salt', salt);
      return;
  });
fork icon24
star icon51
watch icon14

62
63
64
65
66
67
68
69
70
71

// generates hash async
User.generateHash = function(password) {
  return bcrypt.genSaltAsync(8)
    .then(function(salt) {
      return bcrypt.hashAsync(password, salt, null);
    });
};

// checks password async with stored password
fork icon10
star icon0
watch icon8

56
57
58
59
60
61
62
63
64
65
// generates hash async
User.generateHash = function(password) {
  // Use a work factor of 12 for hashing
  return bcrypt.genSaltAsync(12)
    .then(function(salt) {
      return bcrypt.hashAsync(password, salt, null)
      .then(function (passHash) {
        console.log('Salt:', salt, 'Passhash:', passHash);
        return passHash;
      });
fork icon3
star icon0
watch icon2

40
41
42
43
44
45
46
47
48
49
  next(new Error('Failed to connect to database'));
}
if (rows.length !== 0) {
  next(new Error('User already exists'));
} else {
   bcrypt.hashAsync(password, null, null)
    .then(function(hash) {
      db.user.post(username, hash, function (err, rows) {
        if (err) {
          next(new Error('Failed to create user'));
fork icon9
star icon0
watch icon1

6
7
8
9
10
11
12
13
14
15
            callback(err, hash);
        });
    }
    else {
        //no idea why empty callback has to be used, but its necessary for promise to work
        return bcrypt.hashAsync(password, null, function() {});
    }
}

function retrievePassword(password, hash, callback, async=false) {
fork icon4
star icon4
watch icon2

3
4
5
6
7
8
9
10
11
12

var instanceMethods = {};
instanceMethods.hashPassword = function (newUser) {
  return bcrypt.genSaltAsync(10)
  .then(function (salt) {
    return bcrypt.hashAsync(newUser.password, salt, null);
  })
  .then(function (hash) {
    return newUser.set('password', hash);
  })
fork icon3
star icon0
watch icon2

84
85
86
87
88
89
90
91
92
93
  return sequelize.Promise.reject("not modified");
}

// bcrypt가 async이기 때문에 promise
return bcrypt.genSaltAsync(SALT_FACTOR).then(function(salt) {
  return bcrypt.hashAsync(member.password, salt, null);
}).then(function(hash) {
  member.setDataValue('password', hash);
}).catch(function(err) {
  return sequelize.Promise.reject(err);
fork icon1
star icon4
watch icon7

5
6
7
8
9
10
11
12
13
14
function hashPassword (password) {
  const SALT_FACTOR = 8

  return bcrypt
  .genSaltAsync(SALT_FACTOR)
  .then(salt => bcrypt.hashAsync(password, salt, null))
  .then(hash => hash)
}

const User = {
fork icon0
star icon0
watch icon1