Laravel 5:使用 SHA1 代替 Bcrypt

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28982706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:07:16  来源:igfitidea点击:

Laravel 5: Using SHA1 instead of Bcrypt

phplaravelsha1laravel-5extending

提问by TheNish

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

我正在尝试扩展HashServiceProviderlaravel 5 中的默认 Bcrypt ,以使用 SHA1 加密。

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:

使用这个问题的答案:如何在 Laravel 4 中使用 SHA1 加密而不是 BCrypt?以及http://laravel.com/docs/5.0/extending#container-based-extension 上的官方文档,我设法编写了以下代码:

In app/Providers/ShaHashServiceProvider.php

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

应用程序/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

app/config/app.php

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

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

我还使用开箱即用的 LaravelAuthController来处理登录。

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!

我想知道到底出了什么问题,在哪里?我希望你们中的一些天才可以帮助我!

回答by TheNish

I'v solved the problem myself :-)

我自己解决了这个问题:-)

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

app/Providers/ShaHashServiceProvider.php 中,我覆盖了错误的方法boot(),而实际上它是register()我应该覆盖的方法。

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

    class ShaHashServiceProvider extends HashServiceProvider {

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

    }