Laravel 5.4 change password crypting method

269 Views Asked by At

How can I change the password crypting algorithm for the auth in Laravel. I see that in the register controller there is this function:

protected function create(array $data)
    {
        return User::create([
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

and I can change the bcrypt function here but how to make the login check the password with my custom hashing algorithm that I will make?

1

There are 1 best solutions below

0
On

You need to create your own HashServiceProvider (its a service provider) so use artisan to create your own (you may call it same as original)

$ artisan make:provider HashServiceProvider

Now copy most of the body of stock HashServiceProvider (the stock one can be found in \Illuminate\Hashing\HashServiceProvider.php) and fix namespaces and imports. Now go to config/app.php and find HashServiceProvider and change stock HashServiceProvider for your own service provider, given that you did exactly as I guided you should notice no change what so ever.

Now you have to create (implement) your own hashing functions. You have two options:

A. extend the original BcryptHasher

<?php
namespace Your\Namespaced\Hasher;
use Illuminate\Hashing\BcryptHasher as OriginalHasher;
class BcryptHasher extends OriginalHasher
{...} // and change what you need

example of this method can be found in Kyslik/django-bcrypt repository

B. create new class and implement Hasher interface

<?php
namespace Your\Namespaced\Hasher;
use Illuminate\Contracts\Hashing\Hasher;
class MyHasher implements Hasher {...} // IDE should scream at you with methods you need to implement

Finally in your service provider (HashServiceProvider) change register() method to return your implementation of hashing.

Do a $ composer dump-autoload and you may enjoy your own shiny hashing.