Laravel 5: Using SHA1 instead of Bcrypt

孤街醉人 提交于 2019-12-09 11:42:58

问题


I'm trying to extend the default Bcrypt HashServiceProvider in laravel 5, to make use of the SHA1 encryption instead.

Using the answer from this question: How to use SHA1 encryption instead of BCrypt in Laravel 4? and the official documentation at http://laravel.com/docs/5.0/extending#container-based-extension, I'v managed to cook up the following code:

In app/Providers/ShaHashServiceProvider.php


    use App\ShaHasher;
    use Illuminate\Hashing\HashServiceProvider;

    class ShaHashServiceProvider extends HashServiceProvider {

        public function boot()
        {
            parent::boot();

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

    }

In app/ShaHasher.php


    use Illuminate\Contracts\Hashing\Hasher as HasherContract;

    class ShaHasher implements HasherContract {

        public function make($value, array $options = array()) {
            $value = env('SALT', '').$value;
            return sha1($value);
        }

        public function check($value, $hashedValue, array $options = array()) {
            return $this->make($value) === $hashedValue;
        }

        public function needsRehash($hashedValue, array $options = array()) {
            return false;
        }

    }

In app/config/app.php


    'providers' => [
            ...
            //'Illuminate\Hashing\HashServiceProvider',
            'App\Providers\ShaHashServiceProvider',
            ...
    ],

I'm also using Laravels out-of-the-box AuthController to handle logins.

But it seems that it does not work as I intended. The very first time I tried to login, everything worked perfectly fine. Then I logged out, and since then, every attempt to login has failed.

I'm not getting any errors, just the "Whoops! There were some problems with your input. These credentials do not match our records." message.

I'm wondering what exactly what went wrong, and where? I hope some of you geniuses can help me out!


回答1:


I'v solved the problem myself :-)

In app/Providers/ShaHashServiceProvider.php I overrided the wrong method boot(), when it was in fact the method register() I should have overridden.


    use App\ShaHasher;
    use Illuminate\Hashing\HashServiceProvider;

    class ShaHashServiceProvider extends HashServiceProvider {

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

    }



来源:https://stackoverflow.com/questions/28982706/laravel-5-using-sha1-instead-of-bcrypt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!