password_needs_rehash

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

password_needs_rehash检测散列值是否匹配指定的选项

说明

password_needs_rehash(string $hash, string|int|null $algo, array $options = []): bool

此函数检测指定的散列值是否实现了提供的算法和选项。 如果没有,需要重新生成散列值。

参数

hash

一个由 password_hash() 创建的散列值。

algo

一个用来在散列密码时指示算法的密码算法常量

options

一个包含有选项的关联数组。详细的参数说明,请参考文档 密码算法常数

返回值

如果散列需要重新生成才能匹配指定的 algooptions, 则返回 true,否则返回 false

更新日志

版本 说明
7.4.0 现在 algo 参数可以支持 string 类型,但为了向后兼容性,同时支持 int 类型。

范例

示例 #1 password_needs_rehash()用法

<?php

$password
= 'rasmuslerdorf';
$hash = '$2y$10$YCFsG6elYca568hBi2pZ0.3LDL5wjgxct1N8w/oLR/jfHsiQwCqTS';

// 当硬件性能得到改善时,cost 参数可以再修改
$options = array('cost' => 11);

// 根据明文密码验证储存的散列
if (password_verify($password, $hash)) {
// 检测是否有更新的可用散列算法
// 或者 cost 发生变化
if (password_needs_rehash($hash, PASSWORD_DEFAULT, $options)) {
// 如果是这样,则创建新散列,替换旧散列
$newHash = password_hash($password, PASSWORD_DEFAULT, $options);
}

// 使用户登录
}
?>

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top