xavierfaucon/bravoids
Composer 安装命令:
composer require xavierfaucon/bravoids
包简介
Converts Ids (base10) to Hashids (base50 or 62)
README 文档
README
Converts IDs (base10) to Hashed IDs (base50 or 62).
The BravoIDs class offers two methods : BravoIDs::encrypt and BravoIDs::decrypt.
BravoIDs::encrypt
Description
<?php /** * * @param int $number The ID you want to convert into a hash * @param string $passPhrase A passphrase that you must provide * @param int $minHashLength Specify the minimum length for the output. * @param bool $safeCharacters Remove all the vowels to generate the output to avoid curse words * * @return string The hash of a converted ID. */ BravoIDs::encrypt($number, $passPhrase, $minHashLength = 0, $safeCharacters = true); ?>
BravoIDs::encrypt
Description
<?php BravoIDs::decrypt(string $hash, string $passPhrase, int $minHashLength = 0, bool $safeCharacters = true); ?>
Returns an ID from a hash.
Parameters:
- $hash : The hash you want to convert into an ID
- $passPhrase : A passphrase that you must provide
- $minHashLength (optional - default is 0) : Specify the minimum length for the output.
- $safeCharacters (optional - default is true) : Remove all the vowels to generate the output to avoid curse words
NB : in order to decrypt a hash, you must specify the exact same variables for $passPhrase, $minHashLength and $safeCharacters taht you used for BravoID::encrypt.
Basic Usage:
To simply generate IDS :
<?php echo BravoIDs::encrypt (123, 'myPassPhrase'); // "tr" echo BravoIDs::decrypt ('tr', 'myPassPhrase'); // "123" ?>
Advanced Usage:
To increase hash length :
<?php echo BravoIDs::encrypt (123, 'myPassPhrase', 4); // "kNtr" echo BravoIDs::decrypt ('kNtr', 'myPassPhrase', 4); // "123" ?>
NB: if you use a 32-bit builds of PHP you won't be able to set a $minHashLengh > 5. This is explained in the Limitations part.
To allow all characters including vowels (cursing words could appear) :
<?php echo BravoIDs::encrypt (123, 'myPassPhrase', 4, false); // "5u59" echo BravoIDs::decrypt ('5u59', 'myPassPhrase', 4, false); // "123" ?>
Limitations
Depending on your build of PHP you will be limited:
- 32-bit builds of PHP : Integers can be from -2147483648 to 2147483647
- 64-bit builds of PHP: Integers can be from -9223372036854775808 to 9223372036854775807
I did not run tests on 64-bit build of php.
On 32-bit, the limitation means that you'll be able to convert ids up to 2 147 483 647 and not one more.
Moreover, when you use the variable $minHashLength, you reduce the number of available ids.
A computation is done to find the minimum value that your number should have to reach the required HashLength):
For example, to get a hash of at least X characters, you'll need a number >= base * (X - 1).
Where base is the number of characters used to convert your numbers (base50 with $safeCharacters = true and base62 with $safeCharacters = false).
Once we know what is this minimum value, all your ids are "scaled" to that value, '0' would become 6 250 000 (base50 and $minHashLength = 4).
In this case, this has for effect that your 2 147 483 647 has 6 250 000 less value possible ('0' to '6 249 999').
Thanks
Thanks to ivanakimov and https://github.com/ivanakimov/hashids.php from which I borrowed the _consistent_shuffle method.
Thanks to Kevin van Zonneveld who wrote this article from which I took the idea of removing the vowels to get safe Words: http://kvz.io/blog/2009/06/10/create-short-ids-with-php-like-youtube-or-tinyurl/
BravoID is named after the function offered on this article which was called AlphaID :-)
xavierfaucon/bravoids 适用场景与选型建议
xavierfaucon/bravoids 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ids」 「hashids」 「base62」 「base10」 「base50」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xavierfaucon/bravoids 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xavierfaucon/bravoids 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xavierfaucon/bravoids 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dynamically create a hash of an Eloquent model id value to avoid exposing a record's actual database id.
Hashids for Yii2
base62 encoder and decoder also for big numbers with Laravel integration
A collection of observer classes that can be use in your Laravel application.
Backend Optimizing Bundle
Provides hash tools for Laravel.
统计信息
- 总下载量: 35
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-02