我想在将密码存储在数据库中之前使用node.js bcrypt哈希密码。
此链接提供了文档。
https://github.com/kelektiv/node.bcrypt.js
这是有关密码哈希的示例。
var bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 's0/\/\P4$$w0rD';
var salt = bcrypt.genSaltSync(saltRounds);
var hash = bcrypt.hashSync(myPlaintextPassword, salt);
// Store hash in your password DB.
这是检查密码的代码。
// Load hash from your password DB.
bcrypt.compareSync(myPlaintextPassword, hash); // true
这是我不明白的。在
bcrypt.compareSync
中,为什么没有参数salt
?由于哈希是从salt生成的,为什么比较明文密码不涉及哈希中使用的原始salt? 最佳答案
盐是bcrypt存储在数据库中的字符串的一部分,例如,参见Do I need to store the salt with bcrypt?上的答案