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


Comparing Hashed Values to check whether it is Equal

我将密码存储为 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条件时,我总是得到错误。

我犯了什么错误?

每次调用 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
}

使用Hash::check()根据哈希验证密码。

Hash::check('secret', $hashedPassword);

文档 - 安全 - 存储密码