password_hash() 函數(shù)用于檢測(cè)散列值是否匹配指定的選項(xiàng)。
PHP 版本要求: PHP 5 >= 5.5.0, PHP 7
bool password_needs_rehash ( string $hash , int $algo [, array $options ] )
參數(shù)說明:
此函數(shù)檢測(cè)指定的散列值是否實(shí)現(xiàn)了提供的算法和選項(xiàng)。 如果沒有,需要重新生成散列值。
<?php
$password = 'rasmuslerdorf';
$hash = '$2y$10$YCFsG6elYca568hBi2pZ0.3LDL5wjgxct1N8w/oLR/jfHsiQwCqTS';
// 當(dāng)硬件性能得到改善時(shí),cost 參數(shù)可以再修改
$options = array('cost' => 11);
// 根據(jù)明文密碼驗(yàn)證儲(chǔ)存的散列
if (password_verify($password, $hash)) {
// 檢測(cè)是否有更新的可用散列算法
// 或者 cost 發(fā)生變化
if (password_needs_rehash($hash, PASSWORD_DEFAULT, $options)) {
// 如果是這樣,則創(chuàng)建新散列,替換舊散列
$newHash = password_hash($password, PASSWORD_DEFAULT, $options);
}
// 使用戶登錄
}
?>