password_hash 的盐存储在哪里?

Where is the salt stored for password_hash?

根据(相对)新的 PHP 文档:

password_hash 函数使用随机盐(我们不应该担心.. O_O),所以如果我理解正确的话,盐必须存储在某个地方,否则用户不会'注册网站后无法登录(不同的 salt => 不同的哈希值。)

函数文档没有说明与数据库交互的任何信息,并且由于我认为存储每个用户的数据只能通过数据库进行扩展,那么该函数到底在哪里存储随机盐? txt 类似会话数据的文件?

password_hash manual 状态

The used algorithm, cost and salt are returned as part of the hash. Therefore, all information that's needed to verify the hash is included in it. This allows the password_verify() function to verify the hash without needing separate storage for the salt or algorithm information.

因此盐已经包含在您保存在数据库中的散列中。

让我们从其他人告诉你的例子中学习:

$options = [
    'cost' => 11,
    'salt' => 'abcdefghijklmnopqrstuv',
];
echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT, $options)."\n";

输出:

y$11$abcdefghijklmnopqrstuu7aZVUzfW85EB4mHER81Oudv/rT.rmWm

粗体部分是您的成本和盐,分别嵌入到生成的哈希中。

您可以将其吐回到 password_verify 中,它会相应地处理它:

print_r(password_verify('rasmuslerdorf', 'y$abcdefghijklmnopqrstuu7aZVUzfW85EB4mHER81Oudv/rT.rmWm')); // true