When you need to hash and check passwords using an algorithm other than supported bcrypt
or argon
there is an easy way to add a custom one. When I needed to use md5
for a legacy project I have tried to quickly google a solution and most I had found were based on creating a ServiceProvider
which completely overrides builtin HashManager
. The other way would be to extend it instead.
So I have added a following into my AuthServiceProvider
's boot()
method:
$this->app->make('hash')->extend('md5', function() {
return new Md5Hasher;
});
Then, created a hasher itself:
<?php
namespace App\Auth\Hashing;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Hashing\AbstractHasher;
class Md5Hasher extends AbstractHasher implements HasherContract
{
/**
* Hash the given value.
*
* @param string $value
* @param array $options
* @return string
*/
public function make($value, array $options = [])
{
return md5($value);
}
/**
* Check if the given hash has been hashed using the given options.
*
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function needsRehash($hashedValue, array $options = [])
{
return false;
}
/**
* Check the given plain value against a hash.
*
* @param string $value
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function check($value, $hashedValue, array $options = [])
{
return $this->make($value) === $hashedValue;
}
}
And finally in config/hashing.php
configuration file you can set your default driver:
'driver' => 'md5',
With this method you can still use other drivers with Hash
facade:
$hash_default = Hash::make('test'); // Uses default driver
$hash_bcrypt = Hash::driver('bcrypt')->make('test'); // Uses 'bcrypt' driver
works like a charm! thanks!