How to extend Laravel's bcrypt method

Ad

I have made a php class that combines different hash algorithms, and I would like to implement it within the bcrypt() laravel's method.

My current solution is to access the AuthController and replace bcrypt($data['password']) by bcrypt(phashp($data['password'])), but I wonder if there is a way to modify the method without changing the code in the Illuminate Hashing vendor nor in the AuthController.

How can I extend this method?

Thank you!

Ad

Answer

Ad

What you need to do, is go into config/app.php and replace Illuminate\Hashing\HashServiceProvider::class, with custom one and you can now set your custom singleton. In above provider there is:

$this->app->singleton('hash', function () {
    return new BcryptHasher;
});

and you can do:

$this->app->singleton('hash', function () {
    return new MyCustomHasher;
});

and of course define MyCustomHasher class that will implement HasherContract interface

It should work without a problem, because when you look at bcrypt definition:

function bcrypt($value, $options = [])
{
    return app('hash')->make($value, $options);
}

you see that you run finnally run class that is bound to hash

Ad
source: stackoverflow.com
Ad