gpt4 book ai didi

Laravel 6 在从 CakePHP 2.x 迁移期间使用自定义 Hasher

转载 作者:行者123 更新时间:2023-12-05 02:58:14 24 4
gpt4 key购买 nike

我们正在从 CakePHP 2.X 迁移应用程序,但我们需要在迁移之前实现我们的移动 API。我已经关注了我能找到的所有项目,但它们似乎都适用于 v5 或更低版本。无论我做什么,Hash::make() 仍然会生成 Bcrypt 密码。

我真的想让 2 只鸟一 block 石头允许 sha1() 登录并在登录时更新到 Bcrypt,但我们还没有在 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) 您必须创建一个类来扩展 Illuminate\Hashing\AbstractHasher 或至少实现 Illuminate\Contracts\Hashing\Hasher。查看当前的 Bcrypt 驱动程序实现作为引用 on GitHub .您提供的 CustomHasher 类作为驱动程序应该可以正常工作,我只是重命名它以避免与命名混淆。

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 配置文件更改为您要用于散列的驱动程序。

注意:整个代码尚未经过测试,但我查看了源代码以提出应该可行的解决方案。只需评论任何未按预期工作的内容,以便我修复我的答案。

关于Laravel 6 在从 CakePHP 2.x 迁移期间使用自定义 Hasher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59147771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com