password_needs_rehash
检测散列值是否匹配指定的选项
PHP 5.5.0 及以上版本
该函数用于判断已经哈希过的密码是否需要重新哈希。当哈希算法或者选项发生改变时,使用该函数可以判断现有密码哈希值是否需要更新,从而保证密码安全性。
bool password_needs_rehash ( string $hash , int|string $algo [, array $options = [] ] )
如果密码哈希需要重新生成,则返回 true,否则返回 false。
下面的示例演示了如何使用 password_needs_rehash 来判断一个已存储的密码哈希是否需要重新哈希,并在需要时重新生成密码哈希:
$hash = '$2y$10$e0NRvPRj1hQ1hP5K4oC2U.3J1RZ1bL3P0GqXkuR2h0mjEaD7X3qae'; // 旧密码哈希 $new_algo = PASSWORD_BCRYPT; $options = ['cost' => 12]; <p>if (password_needs_rehash($hash, $new_algo, $options)) {<br> $new_hash = password_hash('用户明文密码', $new_algo, $options);<br> // 更新数据库中的密码哈希值为 $new_hash<br> }<br>
该示例首先定义一个已有的密码哈希字符串和新的哈希算法及其选项。通过调用 password_needs_rehash 判断旧哈希是否需要更新。如果需要,则使用 password_hash 生成新的哈希,并将其保存到数据库。