PHP8 password_needs_rehash

2024-03-13 11:25 更新

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

password_needs_rehash — 檢測散列值是否匹配指定的選項

說明

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

此函數(shù)檢測指定的散列值是否實(shí)現(xiàn)了提供的算法和選項。 如果沒有,需要重新生成散列值。

參數(shù) 

hash

一個由 password_hash() 創(chuàng)建的散列值。

algo

一個用來在散列密碼時指示算法的密碼算法常量。

options

一個包含有選項的關(guān)聯(lián)數(shù)組。詳細(xì)的參數(shù)說明,請參考文檔 密碼算法常數(shù)。

返回值 

如果散列需要重新生成才能匹配指定的 algo 和 options, 則返回 true,否則返回 false。

更新日志 

版本說明
7.4.0現(xiàn)在 algo 參數(shù)可以支持 string 類型,但為了向后兼容性,同時支持 int 類型。

示例 

示例 #1 password_needs_rehash()用法

<?php

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

$algorithm = PASSWORD_BCRYPT;
// bcrypt 的成本參數(shù)隨著硬件的改進(jìn)而可能發(fā)生變化
$options = ['cost' => 12];

// 根據(jù)明文密碼驗證儲存的散列
if (password_verify($password, $hash)) {
    // 檢查算法或選項是否已經(jīng)更改
    if (password_needs_rehash($hash, $algorithm, $options)) {
        // 如果是這樣,則創(chuàng)建新散列,替換舊散列
        $newHash = password_hash($password, $algorithm, $options);
        
        // 使用 $newHash 更新用戶記錄
    }

    // 執(zhí)行登錄
}
?>


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號