- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们正在从 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/
我需要使用键(自定义对象)和值作为自定义对象集从映射创建哈希码,我使用 Guava 18.0 @Getter public final class StockKey { @ValidIs
我需要使用不同于默认的哈希器来建立一个哈希表,所以我这样写: class foo { public: ... private: struct myhasher { size_t opera
我在一个由 handlebarsJS 提供支持的 SPA 上使用 crossroadsJS 和 hasherJS。回想起来,我可能应该在 Ember 上构建整个东西,但已经晚了,重新开始并不明智。 我
如果我使用 hash() 或 hasher.write() 函数,像 1234 这样的数字会得到相同的结果,但是一个字节slice like b"Cool" 不会。我认为应该是一样的;为什么不是呢?
所以我刚刚设置了我的 Digital Ocean droplet(服务器)并一直在努力让这个网站正常工作,但是我遇到了一个接一个的错误。我终于让网站加载了登录页面(这是应该发生的),但是当我登录时,我
我们正在从 CakePHP 2.X 迁移应用程序,但我们需要在迁移之前实现我们的移动 API。我已经关注了我能找到的所有项目,但它们似乎都适用于 v5 或更低版本。无论我做什么,Hash::make(
我目前有这段代码: crossroads.addRoute('/login', function(){ $.ajax({ url: '/login', type
我有一个看起来像这样的数据结构: pub struct X { hasher: Box, phantom: std::marker::PhantomData, } 它的用法如下: im
我刚刚开始使用 crossroads.js和 hasher.js并且我已经成功解析了我的大部分路线。我不明白的是如何回到页面的根目录。我知道我说得不对,但这是我的问题: 我导航至 http://exa
我想说,hasher 和 key_equal 之间一定有关系。如果两个元素相同(调用 equal 返回 true)它们必须具有相同的散列,否则将在错误的桶中搜索元素。 但是http://www.cpl
在 Java 6 中,我的理解是,您可以在创建 TreeSet 时向 TreeSet 提供比较器,以覆盖集合中对象的“自然顺序”。 您是否有任何想法为什么 Java 不支持提供覆盖集合中对象的“自然散
我是一名优秀的程序员,十分优秀!