1

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!

rogervila
  • 974
  • 1
  • 10
  • 27
  • You can bind your own 'hasher' to the container as 'hash' potentially. The bcypt helper resolves 'hash' from the container and calls `->make($value, $options)` on it. – lagbox Dec 24 '15 at 18:47

1 Answers1

2

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

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291