函式名稱:sodium_crypto_pwhash_str_verify()
適用版本:PHP 7.2.0以上
函式說明:sodium_crypto_pwhash_str_verify() 函式用於驗證密碼雜湊字串的正確性。它將驗證密碼是否與雜湊字串匹配。
語法:sodium_crypto_pwhash_str_verify(string $hash, string $password): bool
引數:
- $hash:密碼的雜湊字串,通常是在使用 sodium_crypto_pwhash_str() 函式生成的。
- $password:要驗證的密碼。
返回值:
- 如果密碼與雜湊字串匹配,則返回 true,否則返回 false。
示例:
// 生成密碼雜湊字串
$hash = sodium_crypto_pwhash_str('password', SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE);
// 驗證密碼雜湊字串的正確性
if (sodium_crypto_pwhash_str_verify($hash, 'password')) {
echo "密碼匹配";
} else {
echo "密碼不匹配";
}
在上面的示例中,我們首先使用 sodium_crypto_pwhash_str() 函式生成密碼的雜湊字串。然後,我們使用 sodium_crypto_pwhash_str_verify() 函式來驗證密碼雜湊字串的正確性。如果密碼與雜湊字串匹配,則輸出 "密碼匹配";否則輸出 "密碼不匹配"。請注意,這只是一個簡單的示例,實際使用時,應該根據具體需求進行適當的錯誤處理和安全措施。
熱門工具排行榜