Home » Php » php – How to compare a string password with laravel Encrypted Password

php – How to compare a string password with laravel Encrypted Password

Posted by: admin July 12, 2020 Leave a comment

Questions:

I Had first created a Web Application in Laravel.Now I am working on its mobile Application using Ionic Frame work.

While working with laravel, laravel convert the password to its encryption.
Now while working on integration of API in Ionic with Laravel, with login functionality I am facing an issue that, How can I a compare the password entered through Mobile App with the encrypted password in laravel table.

If its would have been web App then its working fine, but for API integration I am facing this issue. Please Help me

How to&Answers:

2 ways:

1.

$hashedPassword = User::find(1)->password;

if (Hash::check('plain-text-password', $hashedPassword)) {
    // The passwords match...
}

2.

$hashedPassword = User::find(1)->password;

if (Hash::make('plain-text-password') === $hashedPassword) {
  // The passwords match...
}

However, as the official documentation says, and as a pro-DRY (Don’t Repeat Yourself) approach, if you are using the LoginController included with Laravel, you will probably not need to use these 2 ways directly, as it automatically does the first way already.

Ref: https://laravel.com/docs/5.4/hashing

IMPORTANT UPDATE

The second way posted here does not work because Laravel generates random salt on each hash function; making each hash different from the original hashed password in the the database as addional security measure. Use the first way instead.

Answer:

Hash::check is the best method to do it.

if(Hash::check('plain password', 'encrypted password')){
    //enter your code
}

Answer:

compare with result of password_hash(‘rasmuslerdorf’, PASSWORD_DEFAULT).”\n”;