muffin/obfuscate
Composer 安装命令:
composer require muffin/obfuscate
包简介
CakePHP support for ID obfuscation
关键字:
README 文档
README
Primary key obfuscation for CakePHP using HashIds, Optimus, Base62 and/or custom obfuscation strategies.
Installation
Install the plugin using Composer:
composer require muffin/obfuscate
Load the plugin by either running this console command:
bin/cake plugin load Muffin/Obfuscate
or by manually adding the following line to src/Application.php:
$this->addPlugin('Muffin/Obfuscate');
Lastly, install the required obfuscation library depending on the strategy class you want to use and stated below.
Built-in obfuscation strategies
Use the HashIdStrategy if you want to:
- obfuscate your primary keys with short, unique, non-sequential ids
- present record ids like 347 as strings like “yr8”
composer require hashids/hashids
Use the OptimusStrategy if you want to:
- obfuscate your primary keys with integers based on Knuth's integer hash
- present record ids like 347 as integers like 372555994
composer require jenssegers/optimus
Use the Base62Strategy if you want to:
- obfuscate your primary keys with base62 strings and integers
- present record ids like 347 as strings like "vk"
composer require tuupola/base62
You can also create your own strategy classes by implementing the StrategyInterface.
Usage
1. Attaching the behavior
Prepare for obfuscation by attaching the Obfuscate behavior to your table(s) and specifying which strategy you want to use as shown in the following examples.
use Muffin\Obfuscate\Model\Behavior\Strategy\HashIdStrategy; $this->addBehavior('Muffin/Obfuscate.Obfuscate', [ // Strategy constructor parameter: // $salt - Random alpha numeric string. You can also set "Obfuscate.salt" // $minLength (optional) - The minimum hash length. Default: 0 // $alphabet (optional) - Custom alphabet to generate hash from. Default: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' // config instead of passing salt to construction. // DO NOT USE same salt as set for "Security.salt" config. 'strategy' => new HashIdStrategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B', 10, 'abcdefghijklmnopqrstuvwxyz') ]);
use Muffin\Obfuscate\Model\Behavior\Strategy\OptimusStrategy; $this->addBehavior('Muffin/Obfuscate.Obfuscate', [ // Strategy constructor parameters: // $prime - Large prime number lower than 2147483647 // $inverse - The inverse prime so that (PRIME * INVERSE) & MAXID == 1 // $random - A large random integer lower than 2147483647 // You can use vendor/bin/optimus spark to generate these set of numbers. 'strategy' => new OptimusStrategy(2123809381, 1885413229, 146808189) ]);
use Muffin\Obfuscate\Model\Behavior\Strategy\Base62Strategy; $this->addBehavior('Muffin/Obfuscate.Obfuscate', [ // Strategy constructor parameters: // $set - Random alpha-numeric set where each character must only be used exactly once 'strategy' => new Base62Strategy('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B') ]);
Please note that attaching the behavior is totally unobtrusive and will do absolutely nothing until you use one of the custom finders.
2. Using the custom finders
This plugin comes with the following two custom finders that are responsible for the actual obfuscation (cloaking) and elucidation (uncloaking) process:
findObfuscated: used to find records using an obfuscated (cloaked) primary keyfindObfuscate: used to obfuscate (cloak) all primary keys in a find result set
findObfuscated
Use this finder if you want to look up a record using an obfuscated id. The plugin will elucidate (uncloak) the obfuscated id and will execute the find using the "normal" primary key as it is used inside your database.
CakePHP example:
public function view($id) { $article = $this->Articles->find('obfuscated') ->where(['id' => $id]) // For e.g. if value for $id is 'S' it will search for actual id 1 ->first(); }
Crud plugin example:
public function view($id) { $this->Crud->on('beforeFind', function (EventInterface $event) { $event->subject()->query->find('obfuscated'); }); }
findObfuscate
Use this finder if you want the plugin to obfuscate all "normal" primary keys found in a find result set.
CakePHP example:
public function index() { $articles = $this->Articles->find('obfuscate'); }
Crud plugin example:
public function index() { $this->Crud->on('beforePaginate', function (EventInterface $event) { $event->subject()->query->find('obfuscate'); }); }
Methods
Attaching the behavior also makes the following two methods available on the table:
obfuscate(string $str)elucidate(string $str)
Pro tips
Authentication
A fairly common use case is applying obfuscation to user ids. To ensure the
Authentication plugin properly handles obfuscated ids, specify the obfuscated finder
using the finder key in your identifier's resolver config.
Patches & Features
- Fork
- Mod, fix
- Test - this is important, so it's not unintentionally broken
- Commit - do not mess with license, todo, version, etc. (if you do change any, bump them into commits of their own that I can ignore when I pull)
- Pull request - bonus point for topic branches
To ensure your PRs are considered for upstream, you MUST follow the CakePHP coding standards.
Bugs & Feedback
http://github.com/usemuffin/obfuscate/issues
License
Copyright (c) 2015-Present, Use Muffin and licensed under The MIT License.
muffin/obfuscate 适用场景与选型建议
muffin/obfuscate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.96k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2016 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cakephp」 「hash」 「obfuscate」 「muffin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 muffin/obfuscate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 muffin/obfuscate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 muffin/obfuscate 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Provides hash tools for Laravel.
Replaces the default email address spam protection with a better one. The email obfuscation is more randomized, safer and more user friendly for the website visitor.
Article CSS-ID Frontend Output Optimization
PhpPlaisio: Obfuscator
♺ Laravel Nova Hashids card. Convert your ids.
统计信息
- 总下载量: 3.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 36
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-18