我的同事有一个存储帐户信息的数据库;帐户的 SHA256 哈希密码和盐值作为原始二进制数据 (blob) 存储在列中。
密码在 PHP 中使用它进行哈希处理(true 表示原始输出):
hash("sha256", $salt . $password, true);
我正在尝试在必须从 PHP 取回存储在数据库中的相同哈希密码的 Node.js 服务器上实现身份验证,这似乎不起作用:
/**
* Validates a password sent by an end user by comparing it to the
* hashed password stored in the database. Uses the Node.js crypto library.
*
* @param password The password sent by the end user.
* @param dbPassword The hashed password stored in the database.
* @param dbSalt The encryption salt stored in the database.
*/
function validatePassword(password, dbPassword, dbSalt) {
// Should the dbSalt be a Buffer, hex, base64, or what?
var hmac = crypto.createHmac("SHA256", dbSalt);
var hashed = hmac.update(password).digest('base64');
console.log("Hashed user password: " + hashed);
console.log("Database password: " + dbPassword.toString('base64'));
return hashed === dbPassword;
}