Laravel 5:使用SHA1代替Bcrypt [英] Laravel 5: Using SHA1 instead of Bcrypt
问题描述
我正在尝试在laravel 5中扩展默认的Bcrypt HashServiceProvider
,以改用SHA1加密.
I'm trying to extend the default Bcrypt HashServiceProvider
in laravel 5, to make use of the SHA1 encryption instead.
使用此问题的答案:如何在Laravel 4中使用SHA1加密代替BCrypt?和
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:
在 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
来处理登录.
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!
推荐答案
我自己解决了这个问题:-)
I'v solved the problem myself :-)
在 app/Providers/ShaHashServiceProvider.php 中,我重写了错误的方法boot()
,而实际上我应该重写了方法register()
.
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; });
}
}
这篇关于Laravel 5:使用SHA1代替Bcrypt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!