【问题标题】:How do I check if Bcrypt password is correct?如何检查 Bcrypt 密码是否正确?
【发布时间】:2017-04-22 00:59:50
【问题描述】:

我使用 md5 对我的密码进行哈希处理,但了解到使用 bcrypt 更安全。

使用 md5 时,很容易检查表单中输入的密码是否正确。我刚刚完成了

if(md5($request->password) == $user->password)
   //Login or whatever

那么我该如何使用 bcrypt 呢?我试过了

if(bcrypt($request->password) == $user->password)

但这不起作用。

【问题讨论】:

  • 另请注意,在您的示例中,您正在对数据库中的值进行哈希处理并将其与用户键入的内容进行比较。这是倒退的——你想对用户输入的值进行哈希处理并将其与数据库进行比较。
  • 哦,是的,我明白了,只是一个错字。已编辑。

标签: php laravel bcrypt


【解决方案1】:

使用attempt() 方法:

if (Auth::attempt(['email' => $email, 'password' => $password]))

attempt 方法接受一个键/值对数组作为其第一个参数。数组中的值将用于在您的数据库表中查找用户。

https://laravel.com/docs/5.4/authentication#authenticating-users

在后台attempt() 使用password_verify() 方法检查密码。

【讨论】:

    【解决方案2】:

    您也可以使用Hash Facade 的check 方法

    if (Hash::check($request->password, $user->password)) {
        // The passwords match...
    }
    

    https://laravel.com/docs/5.4/hashing#basic-usage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 2015-12-28
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 2016-09-19
      • 2016-11-25
      • 1970-01-01
      相关资源
      最近更新 更多