jenssegers/optimus
Composer 安装命令:
composer require jenssegers/optimus
包简介
Id obfuscation based on Knuth's integer hash method
README 文档
README
With this library, you can transform your internal id's to obfuscated integers based on Knuth's integer hash. It is similar to Hashids, but will generate integers instead of random strings. It is also super fast.
Installation
Install using composer:
composer require jenssegers/optimus
If you will be running your code on a 32 bit system or will be working with large prime numbers it is suggested that you install the GMP extension. For debian/ubuntu you can install the extension with one of these commands:
apt-get install php7.4-gmp apt-get install php8.0-gmp apt-get install php8.1-gmp
Usage
To get started you will need 3 things;
- Large prime number lower than
2147483647 - The inverse prime so that
(PRIME * INVERSE) & MAXID == 1 - A large random integer lower than
2147483647
Luckily for you, I have included a console command that can do all of this for you. To get started, just run the following command:
> php vendor/bin/optimus spark
Prime: 2123809381
Inverse: 1885413229
Random: 146808189
If you prefer to choose your own prime number (from this list for example), you can pass it to the command to calculate the remaining numbers:
> php vendor/bin/optimus spark 1580030173
Prime: 1580030173
Inverse: 59260789
Random: 1163945558
Using those numbers, you can start creating instances of Optimus($prime, $inverted, $random):
use Jenssegers\Optimus\Optimus; new Optimus(1580030173, 59260789, 1163945558);
NOTE: Make sure that you are using the same constructor values throughout your entire application!
Encoding and decoding
To encode id's, use the encode method:
$encoded = $optimus->encode(20); // 1535832388
To decode the resulting 1535832388 back to its original value, use the decode method:
$original = $optimus->decode(1535832388); // 20
Framework Integrations
Laravel
This is an example service provider which registers a shared Optimus instance for your entire application:
<?php namespace App\Providers; use Jenssegers\Optimus\Optimus; use Illuminate\Support\ServiceProvider; class OptimusServiceProvider extends ServiceProvider { public function register() { $this->app->singleton(Optimus::class, function ($app) { return new Optimus(1580030173, 59260789, 1163945558); }); } }
Once you have created the service provider, add it to the providers array in your config/app.php configuration file:
App\Providers\OptimusServiceProvider::class,
Laravel's automatic injection will pass this instance where needed. Example controller:
<?php namespace App\Http\Controllers; use Jenssegers\Optimus\Optimus; use App\Http\Controllers\Controller; class UserController extends Controller { public function show($id, Optimus $optimus) { $id = $optimus->decode($id); } }
More information: https://laravel.com/docs/5.3/container#resolving
Third-party integrations
- An integration with Laravel is provided by the propaganistas/laravel-fakeid package.
- Laravel Optimus with multiple connections provided by the cybercog/laravel-optimus package.
- An integration with Silex 2 is provided by the jaam/silex-optimus-provider package.
- An integration with Laravel is provided by the elfsundae/laravel-hashid package.
- A PSR-15 middleware provided by the icanhazstring/optimus-middleware package.
Security contact information
To report a security vulnerability, follow these steps.
License
The MIT License.
jenssegers/optimus 适用场景与选型建议
jenssegers/optimus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.96M 次下载、GitHub Stars 达 1.27k, 最近一次更新时间为 2015 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ids」 「hashids」 「obfuscation」 「optimus」 「id obfuscation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jenssegers/optimus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jenssegers/optimus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jenssegers/optimus 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple captcha generator
Dynamically create a hash of an Eloquent model id value to avoid exposing a record's actual database id.
Hashids for Yii2
AMWSCAN (Antimalware Scanner) is a php antimalware/antivirus scanner console script written in php for scan your project. This can work on php projects and a lot of others platform.
Laravel package for obfuscating ids in urls.
A collection of observer classes that can be use in your Laravel application.
统计信息
- 总下载量: 4.96M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1276
- 点击次数: 38
- 依赖项目数: 26
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-03