Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Hash::check() always return false

I have profile form for user can edit own profiles. in this form I have current password. that must be match from seved into database.

Form:

{{ Form::password('currPassword', array('id'=>'currPassword')) }}

i want to have this function in Controller to check this with database.

$data = User::find($id);
if( ! Hash::check( $data->password , Input::get('currPassword') ) )
{
    return Redirect::to('/admin/profile')
        ->with('message', 'Current Password Error !')
        ->withInput();
}

hashed 123456 password into database is ok and after putting 123456 in currPassword that must be return TRUE but that return FALSE always.

like image 221
DolDurma Avatar asked Feb 01 '14 07:02

DolDurma


People also ask

What is hash :: Check in laravel?

The check method provided by the Hash facade allows you to verify that a given plain-text string corresponds to a given hash: if (Hash::check('plain-text', $hashedPassword)) { // The passwords match... }

What is Bcrypt in laravel?

The Laravel Hash facade provides secure Bcrypt hashing for storing user passwords. If you are using the AuthController controller that is included with your Laravel application, it will be take care of verifying the Bcrypt password against the un-hashed version provided by the user.


4 Answers

You're using the wrong argument order. It's Hash::check($input, $hash), not the other way around.

Short tinker example:

[1] > $pw = 123456;
// 123456
[2] > $hashed = Hash::make($pw);
// '$2y$10$xSugoyKv765TY8DsERJ2/.mPIOwLNdM5Iw1n3x1XNVymBlHNG4cX6'
[3] > Hash::check($hashed, $pw);
// false
[4] > Hash::check($pw, $hashed);
// true
like image 131
Joel Hinz Avatar answered Oct 05 '22 23:10

Joel Hinz


I had the same issue and solved it like this:

I found that I was using the Hash::make function in my RegistrationService class and more important that I had already used the setPasswordAttribute function in my User model which were quickly forgotten:

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
   ...

    /**
     * @param $value
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }
}

So the password was double hashed and of course every Hash::check call was incorrect and return false.

like image 43
Alexey Shabramov Avatar answered Oct 05 '22 23:10

Alexey Shabramov


Hash::check() has two parameters first one is plane password and another is hashed password. If password matched with hash it will return true.

Hash::check(normal_password,hashed_password);

Example :

Hash::check('123456a','$2y$10$.XB30GO4jn7bx7EauLrWkugIaCNGxiQCgrFTeFDeSSrGdQYd6Rneq');
like image 29
Hasib Kamal Chowdhury Avatar answered Oct 05 '22 23:10

Hasib Kamal Chowdhury


Though above answers are valid for the question provided, I'm adding more explanation to give details insights

Verifying A Password Against A Hash

The check method allows you to verify that a given plain-text string corresponds to a given hash. However, if you are using the LoginController included with Laravel, you will probably not need to use this directly, as this controller automatically calls this method:

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

check() method is declare in HasherInterface

This method is to Check the given plain value against a hash.

 bool check(string $value, string $hashedValue, array $options = array())

Check the given plain value against a hash.

Parameters

string $value
string $hashedValue
array $options

Return Value

bool

For your example :

$data = User::find($id);
if( ! Hash::check(Input::get('currPassword') , $data->password  ) )
{
    return Redirect::to('/admin/profile')
        ->with('message', 'Current Password Error !')
        ->withInput();
}
like image 45
Amitesh Bharti Avatar answered Oct 05 '22 22:10

Amitesh Bharti