Laravel 6 在从 CakePHP 迁移期间使用自定义 Hasher 2.x
Laravel 6 Use a custom Hasher during migration from CakePHP 2.x
我们正在从 CakePHP 2.X 迁移应用程序,但我们需要在迁移之前实现我们的移动 API。我已经关注了我能找到的所有项目,但它们似乎都适用于 v5 或更低版本。无论我做什么 Hash::make() 仍然会产生 Bcrypt 密码。
我真的很想通过允许 sha1() 登录并在登录时更新到 Bcrypt 来让 2 只鸟一石二鸟,但我们还没有在 CakePHP 2.x 上成功实施。所以我需要让 Hasher 工作或解决方法。我知道我可以在模型中手动散列,但这不允许 Auth 工作。
任何帮助将不胜感激
app.php 配置文件
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
//Illuminate\Hashing\HashServiceProvider::class,
App\Providers\CustomHashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
CustomHashServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Hashing\HashServiceProvider;
use App\Libs\CustomHash\CustomHasher as CustomHasher;
class CustomHashServiceProvider extends HashServiceProvider
{
public function register()
{
$this->app->singleton('hash', function () {
return new CustomHasher;
});
}
}
CustomHasher.php
<?php
namespace App\Lib\CustomHash;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
class CustomHasher implements HasherContract {
/**
* Hash the given value.
*
* @param string $value
* @return array $options
* @return string
*/
public function make($value, array $options = array()) {
//I have custom encoding / encryption here//
//Define your custom hashing logic here//
return sha1(env('SEC_SALT').$value);
}
/**
* Check the given plain value against a hash.
*
* @param string $value
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function check($value, $hashedValue, array $options = array()) {
return $this->make($value) === $hashedValue;
}
/**
* Check if the given hash has been hashed using the given options.
*
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function needsRehash($hashedValue, array $options = array()) {
return false;
}
public function info($hashedValue): array {
return $hashedValue;
}
}
更新
我根据@Mdexp 对此的回答进行了重构....但我发现除非在 Lumen
上的 app.php 中添加,否则配置将被忽略
新建app.php
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\Sha1HashServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Sha1HashServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class Sha1HashServiceProvider extends ServiceProvider {
public function register() {
//
}
public function boot() {
$this->app->make('hash')->extend('sha1', function () {
// Just create the driver instance of the class you created in the step 1
return new \App\Lib\Sha1Hash\Sha1Hasher;
});
}
}
Sha1Hasher.php
<?php
namespace App\Lib\Sha1Hash;
use Illuminate\Hashing\AbstractHasher;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use RuntimeException;
class Sha1Hasher extends AbstractHasher implements HasherContract {
public function __construct(array $options = []) {
}
public function make($value, array $options = []) {
$hash = sha1(env('SEC_SALT').$value);
if ($hash === false) {
throw new RuntimeException('Sha1 hashing not supported.');
}
return $hash;
}
public function check($value, $hashedValue, array $options = []) {
return ($this->make($value) == $hashedValue)?true:false;
}
public function needsRehash($hashedValue, array $options = array()): bool {
return false;
}
}
我会使用默认 HashServiceProvider
并在其中注册一个新驱动程序。完成过渡阶段后,它还可以更快地从 sha1 切换回 bcrypt。
1) 您必须创建一个 class 来扩展 Illuminate\Hashing\AbstractHasher
或至少实现 Illuminate\Contracts\Hashing\Hasher
。查看当前的 Bcrypt 驱动程序实现作为参考 on GitHub。
您提供的 CustomHasher
class 作为驱动程序应该可以正常工作,我只是重命名它以避免与命名混淆。
2) 现在您可以在服务提供商中注册哈希驱动程序,例如:
public function boot()
{
$this->app->make('hash')->extend('sha1', function () {
// Just create the driver instance of the class you created in the step 1
return new YourCustomSha1Hasher();
});
}
3) 然后在您的 config/hashing.php
文件中,将驱动程序设置为 'sha1'
(必须等于 extend
函数调用的第一个参数。
4) 它应该开箱即用,要选择不同的散列驱动程序,只需将 config/hashing.php
配置文件更改为您要用于散列的驱动程序。
注意: 整个代码尚未经过测试,但我查看了源代码以提出应该可行的解决方案。只是评论任何没有按预期工作的东西,所以我可以修复我的答案。
我们正在从 CakePHP 2.X 迁移应用程序,但我们需要在迁移之前实现我们的移动 API。我已经关注了我能找到的所有项目,但它们似乎都适用于 v5 或更低版本。无论我做什么 Hash::make() 仍然会产生 Bcrypt 密码。
我真的很想通过允许 sha1() 登录并在登录时更新到 Bcrypt 来让 2 只鸟一石二鸟,但我们还没有在 CakePHP 2.x 上成功实施。所以我需要让 Hasher 工作或解决方法。我知道我可以在模型中手动散列,但这不允许 Auth 工作。
任何帮助将不胜感激
app.php 配置文件
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
//Illuminate\Hashing\HashServiceProvider::class,
App\Providers\CustomHashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
CustomHashServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Hashing\HashServiceProvider;
use App\Libs\CustomHash\CustomHasher as CustomHasher;
class CustomHashServiceProvider extends HashServiceProvider
{
public function register()
{
$this->app->singleton('hash', function () {
return new CustomHasher;
});
}
}
CustomHasher.php
<?php
namespace App\Lib\CustomHash;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
class CustomHasher implements HasherContract {
/**
* Hash the given value.
*
* @param string $value
* @return array $options
* @return string
*/
public function make($value, array $options = array()) {
//I have custom encoding / encryption here//
//Define your custom hashing logic here//
return sha1(env('SEC_SALT').$value);
}
/**
* Check the given plain value against a hash.
*
* @param string $value
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function check($value, $hashedValue, array $options = array()) {
return $this->make($value) === $hashedValue;
}
/**
* Check if the given hash has been hashed using the given options.
*
* @param string $hashedValue
* @param array $options
* @return bool
*/
public function needsRehash($hashedValue, array $options = array()) {
return false;
}
public function info($hashedValue): array {
return $hashedValue;
}
}
更新 我根据@Mdexp 对此的回答进行了重构....但我发现除非在 Lumen
上的 app.php 中添加,否则配置将被忽略新建app.php
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\Sha1HashServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Sha1HashServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class Sha1HashServiceProvider extends ServiceProvider {
public function register() {
//
}
public function boot() {
$this->app->make('hash')->extend('sha1', function () {
// Just create the driver instance of the class you created in the step 1
return new \App\Lib\Sha1Hash\Sha1Hasher;
});
}
}
Sha1Hasher.php
<?php
namespace App\Lib\Sha1Hash;
use Illuminate\Hashing\AbstractHasher;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use RuntimeException;
class Sha1Hasher extends AbstractHasher implements HasherContract {
public function __construct(array $options = []) {
}
public function make($value, array $options = []) {
$hash = sha1(env('SEC_SALT').$value);
if ($hash === false) {
throw new RuntimeException('Sha1 hashing not supported.');
}
return $hash;
}
public function check($value, $hashedValue, array $options = []) {
return ($this->make($value) == $hashedValue)?true:false;
}
public function needsRehash($hashedValue, array $options = array()): bool {
return false;
}
}
我会使用默认 HashServiceProvider
并在其中注册一个新驱动程序。完成过渡阶段后,它还可以更快地从 sha1 切换回 bcrypt。
1) 您必须创建一个 class 来扩展 Illuminate\Hashing\AbstractHasher
或至少实现 Illuminate\Contracts\Hashing\Hasher
。查看当前的 Bcrypt 驱动程序实现作为参考 on GitHub。
您提供的 CustomHasher
class 作为驱动程序应该可以正常工作,我只是重命名它以避免与命名混淆。
2) 现在您可以在服务提供商中注册哈希驱动程序,例如:
public function boot()
{
$this->app->make('hash')->extend('sha1', function () {
// Just create the driver instance of the class you created in the step 1
return new YourCustomSha1Hasher();
});
}
3) 然后在您的 config/hashing.php
文件中,将驱动程序设置为 'sha1'
(必须等于 extend
函数调用的第一个参数。
4) 它应该开箱即用,要选择不同的散列驱动程序,只需将 config/hashing.php
配置文件更改为您要用于散列的驱动程序。
注意: 整个代码尚未经过测试,但我查看了源代码以提出应该可行的解决方案。只是评论任何没有按预期工作的东西,所以我可以修复我的答案。