Laravel 5:使用SHA1而不是Bcrypt

时间:2015-03-11 09:26:26

标签: php laravel sha1 laravel-5 extending

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

使用这个问题的答案:How to use SHA1 encryption instead of BCrypt in Laravel 4?http://laravel.com/docs/5.0/extending#container-based-extension的官方文档,我设法编写了以下代码:

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();
            });
        }

    }

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;
        }

    }

app / config / app.php


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

我也使用Laravels开箱即用的AuthController来处理登录。

但它似乎不像我预期的那样有效。我第一次尝试登录时,一切都运行得很好。然后我退出了,从那时起,每次登录尝试都失败了。

我没有收到任何错误,只是“哎呀!您的输入存在一些问题。这些凭据与我们的记录不符。”消息。

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

1 个答案:

答案 0 :(得分:7)

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

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; });
        }

    }