Override laravel 4's authentication methods to use custom hasing function

后端 未结 4 699
长发绾君心
长发绾君心 2021-02-08 16:09

I have a table in my database with users. Their password are generated with my own custom hashing function.

How do i override the Authentication methods in laravel 4 to

4条回答
  •  青春惊慌失措
    2021-02-08 16:41

    This is how ended up solving the problem:

    libraries\CustomHasherServiceProvider.php

    use Illuminate\Support\ServiceProvider;
    
    class CustomHasherServiceProvider extends ServiceProvider {
    
        public function register()
        {
            $this->app->bind('hash', function()
            {
                return new CustomHasher;
            });
        }
    
    }
    

    libraries\CustomHasher.php

    class CustomHasher implements Illuminate\Hashing\HasherInterface {
    
    private $NUMBER_OF_ROUNDS = '$5$rounds=7331$';
    
    
    public function make($value, array $options = array())
    {
    
        $salt = uniqid();
        $hash = crypt($password, $this->NUMBER_OF_ROUNDS . $salt);
        return substr($hash, 15);
    }
    
    public function check($value, $hashedValue, array $options = array())
    {
        return $this->NUMBER_OF_ROUNDS . $hashedValue === crypt($value, $this->NUMBER_OF_ROUNDS . $hashedValue);
    }
    
    }
    

    And then I replaced 'Illuminate\Hashing\HashServiceProvider' with 'CustomHasherServiceProvider' in the providers array in app/config/app.php

    and added "app/libraries" to autoload classmap in composer.json

提交回复
热议问题