Node.js hashing of passwords

前端 未结 6 662
小鲜肉
小鲜肉 2021-01-29 23:25

I am currently using the following for hashing passwords:

var pass_shasum = crypto.createHash(\'sha256\').update(req.body.password).digest(\'hex\');
6条回答
  •  梦谈多话
    2021-01-30 00:01

    I use the follwing code to salt and hash passwords.

    var bcrypt = require('bcrypt');
    
    exports.cryptPassword = function(password, callback) {
       bcrypt.genSalt(10, function(err, salt) {
        if (err) 
          return callback(err);
    
        bcrypt.hash(password, salt, function(err, hash) {
          return callback(err, hash);
        });
      });
    };
    
    exports.comparePassword = function(plainPass, hashword, callback) {
       bcrypt.compare(plainPass, hashword, function(err, isPasswordMatch) {   
           return err == null ?
               callback(null, isPasswordMatch) :
               callback(err);
       });
    };
    

提交回复
热议问题