gpt4 book ai didi

php - 比较哈希值以检查它是否相等

转载 作者:行者123 更新时间:2023-12-05 01:47:21 25 4
gpt4 key购买 nike

我将我的密码存储为 BCrypt(Laravel 自己的方式)

$NewValue = Hash::make(Input::get('Password'));

$OldValue = Auth::user()->password; // Taking the value from database
if($NewValue == $OldValue)
{
return 'Both Password are equal'
}
else
{
//go for other operation
}

但是每当我检查 if 条件时,我总是得到 false。

我做错了什么?

最佳答案

每次调用 Hash::make 时,Laravel 的哈希函数都会生成一个新的哈希。在内部它调用 password_hash然后使用 crypt。它总是会生成一个随机盐。盐包含在最终哈希中,因此在比较时可以解析它并用于再次生成相同的哈希。

要验证密码,您需要使用 Hash::check(),然后在后台使用 password_verify

$password = Input::get('Password');
$hashedPassword = Auth::user()->password; // Taking the value from database

if(Hash::check($password, $hashedPassword))
{
return 'Both Password are equal'
}
else
{
//go for other operation
}

关于php - 比较哈希值以检查它是否相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27703626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com