简体   繁体   English

如何检查Bcrypt密码是否正确?

[英]How do I check if Bcrypt password is correct?

I was using md5 to hash my passwords but learned that using bcrypt was more secure. 我使用md5来哈希我的密码,但了解到使用bcrypt更安全。

When using md5, it was easy to check whether a password entered in a form was correct. 使用md5时,很容易检查表单中输入的密码是否正确。 I simply done 我干脆完成了

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

So how do I do this using bcrypt? 那么如何使用bcrypt来做到这一点? I tried 我试过了

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

But that isn't working. 但这不起作用。

Use the attempt() method: 使用attempt()方法:

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

The attempt method accepts an array of key/value pairs as its first argument. attempt方法接受一个键/值对数组作为其第一个参数。 The values in the array will be used to find the user in your database table. 数组中的值将用于查找数据库表中的用户。

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

Under the hood attempt() uses password_verify() method to check password. 在hood下attempt()使用password_verify()方法来检查密码。

You could also use the check method of the Hash Facade 您还可以使用Hash Facade的check方法

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM