应用场景:老系统使用TP框架,采用的是MD5加密,现用Laravel框架重构
1. 新建MD5Hasher类文件,放在App/Helpers下
<?php
namespace App\Helpers\Hasher;
use Illuminate\Contracts\Hashing\Hasher;
class MD5Hasher implements Hasher
{
public function check($value, $hashedValue, array $options = [])
{
return $this->make($value) === $hashedValue;
}
public function needsRehash($hashedValue, array $options = [])
{
return false;
}
public function make($value, array $options = [])
{
$value = env('SALT', '').$value;
return md5($value);
}
}
2.创建服务提供者
php artisan make:provider MD5HashServiceProvider
<?php
namespace App\Providers;
use App\Helpers\Hasher\MD5Hasher;
use Illuminate\Support\ServiceProvider;
class MD5HashServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->app->singleton('hash', function () {
return new MD5Hasher;
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
public function provides()
{
return ['hash'];
}
}
3.在app/config.php的prociders中将
Illuminate\Hashing\HashServiceProvider::class,
替换为下面的或者注释掉上面的
\App\Providers\MD5HashServiceProvider::class,
4.找到项目的composer.json文件下的autoload数组 中添加classmap
"classmap": [
"app/Helpers/MD5Hasher"
],
5.执行
composer dumpautoload
6.最后即可测试