Node.js hashing of passwords

前端 未结 6 622
小鲜肉
小鲜肉 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:02

    You can use the bcrypt-js package for encrypting the password.

    1. Try npm i bcryptjs
    2. var bcrypt = require('bcryptjs') in top.
    3. To hash a password:
    bcrypt.genSalt(10, function(err, salt) {
        bcrypt.hash("B4c0/\/", salt, function(err, hash) {
            // Store hash in your password DB.
        });
    });
    
    1. To check your password,
    // Load hash from your password DB.
    bcrypt.compare("B4c0/\/", hash, function(err, res) {
        // res === true
    });
    

    You can visit https://www.npmjs.com/package/bcryptjs for more information on bcryptjs.

提交回复
热议问题