zhuchunshu/hyperf-hashing
Composer 安装命令:
composer require zhuchunshu/hyperf-hashing
包简介
The Hyperf Hashing package.
README 文档
README
该组件为存储用户密码提供了安全的 Bcrypt 和 Argon2 哈希加密方式。
移植自 illuminate/hashing。
安装
composer require zhuchunshu/hyperf-hashing
发布配置
php bin/hyperf.php vendor:publish zhuchunshu/hyperf-hashing
配置文件位于
config/autoload/hashing.php。
默认配置
<?php declare(strict_types=1); return [ 'default' => 'bcrypt', 'driver' => [ 'bcrypt' => [ 'class' => \HyperfExt\Hashing\Driver\BcryptDriver::class, 'rounds' => env('BCRYPT_ROUNDS', 10), ], 'argon' => [ 'class' => \HyperfExt\Hashing\Driver\Argon2IDriver::class, 'memory' => 1024, 'threads' => 2, 'time' => 2, ], 'argon2id' => [ 'class' => \HyperfExt\Hashing\Driver\Argon2IdDriver::class, 'memory' => 1024, 'threads' => 2, 'time' => 2, ], ], ];
你可以在 config/autoload/hashing.php 配置文件中配置默认哈希驱动程序。目前支持三种驱动程序: Bcrypt 和 Argon2(Argon2i 和 Argon2id variants)。
注意:Argon2i 驱动程序需要 PHP 7.2.0 或更高版本,而 Argon2id 驱动程序则需要 PHP 7.3.0 或更高版本。
使用
你可以通过 \HyperfExt\Hashing\Hash 类来加密你的密码:
<?php declare(strict_types=1); namespace App\Http\Controller; use Hyperf\HttpServer\Request; use HyperfExt\Hashing\Hash; class UpdatePasswordController { public function update(Request $request) { // …… $user->fill([ 'password' => Hash::make($request->input('new_password')) ])->save(); } }
调整 Bcrypt 加密系数
如果使用 Bcrypt 算法,你可以在 make 方法中使用 rounds 选项来配置该算法的加密系数。然而,对大多数应用程序来说,默认值就足够了:
$hashed = Hash::make('password', [ 'rounds' => 12 ]);
调整 Argon2 加密系数
如果使用 Argon2 算法,你可以在 make 方法中使用 memory,time 和 threads 选项来配置该算法的加密系数。然后,对大多数应用程序来说,默认值就足够了:
$hashed = Hash::make('password', [ 'memory' => 1024, 'time' => 2, 'threads' => 2, ]);
有关这些选项的更多信息,请查阅 PHP 官方文档。
密码哈希验证
check 方法能为您验证一段给定的未加密字符串与给定的哈希值是否一致:
if (Hash::check('plain-text', $hashedPassword)) { // 密码匹配… }
检查密码是否需要重新哈希
needsRehash 方法可以为您检查当哈希的加密系数改变时,您的密码是否被新的加密系数重新加密过:
if (Hash::needsRehash($hashed)) { $hashed = Hash::make('plain-text'); }
使用指定驱动
$hasher = Hash::getDriver('argon2i'); $hasher->make('plain-text');
使用自定义哈希类
实现 \HyperfExt\Hashing\Contract\DriverInterface 接口,并参照配置文件中的其他算法进行配置。
zhuchunshu/hyperf-hashing 适用场景与选型建议
zhuchunshu/hyperf-hashing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.56k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「hashing」 「hyperf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zhuchunshu/hyperf-hashing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zhuchunshu/hyperf-hashing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zhuchunshu/hyperf-hashing 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A hashing library that will generate and add hashes to your laravel models.
Simple bundle for adding encryption type to Doctrine
An async event for hyperf.
php hyperf xxljob
Secure password hashing using HMAC before (BCrypt) Hash.
Allows you to define what attributes in your Eloquent Model which should be hashed.
统计信息
- 总下载量: 2.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-21