password_needs_rehash()
判断已有哈希是否需要按新参数重新计算。
password_needs_rehash(string $hash, string|int|null $algo, array $options = []): bool
参数
| 参数 | 类型 | 说明 |
|---|---|---|
$hash | string | 已有哈希 |
$algo | string|int | 目标算法 |
$options | array | 目标参数,如 cost |
返回值
bool — 需要重算返回 true。
示例
if (password_needs_rehash($row['password_hash'], PASSWORD_DEFAULT, ['cost' => 12])) {
$new = password_hash($password, PASSWORD_DEFAULT, ['cost' => 12]);
// 更新数据库
}注意
- 用户登录成功时顺手检查,是升级哈希强度的标准时机。
- 不要在用户不登录时批量重算,你没有明文密码。