mojgenie/hashids
Composer 安装命令:
composer require mojgenie/hashids
包简介
Generate short, unique, non-sequential ids (like YouTube and Bitly) from numbers
README 文档
README
Hashids is a small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database numeric ids to users: https://hashids.org/php
Getting started
Require this package, with Composer, in the root directory of your project.
composer require hashids/hashids
Then you can import the class into your application:
use Hashids\Hashids; $hashids = new Hashids(); $hashids->encode(1);
Note Hashids require either
bcmathorgmpextension in order to work.
Quick Example
use Hashids\Hashids; $hashids = new Hashids(); $id = $hashids->encode(1, 2, 3); // o2fXhV $numbers = $hashids->decode($id); // [1, 2, 3]
More Options
A few more ways to pass input ids to the encode() function:
use Hashids\Hashids; $hashids = new Hashids(); $hashids->encode(1, 2, 3); // o2fXhV $hashids->encode([1, 2, 3]); // o2fXhV $hashids->encode('1', '2', '3'); // o2fXhV $hashids->encode(['1', '2', '3']); // o2fXhV
Making your output ids unique
Pass a project name to make your output ids unique:
use Hashids\Hashids; $hashids = new Hashids('My Project'); $hashids->encode(1, 2, 3); // Z4UrtW $hashids = new Hashids('My Other Project'); $hashids->encode(1, 2, 3); // gPUasb
Use padding to make your output ids longer
Note that output ids are only padded to fit at least a certain length. It doesn't mean that they will be exactly that length.
use Hashids\Hashids; $hashids = new Hashids(); // no padding $hashids->encode(1); // jR $hashids = new Hashids('', 10); // pad to length 10 $hashids->encode(1); // VolejRejNm
Using a custom alphabet
use Hashids\Hashids; $hashids = new Hashids('', 0, 'abcdefghijklmnopqrstuvwxyz'); // all lowercase $hashids->encode(1, 2, 3); // mdfphx
Encode hex instead of numbers
Useful if you want to encode Mongo's ObjectIds. Note that there is no limit on how large of a hex number you can pass (it does not have to be Mongo's ObjectId).
use Hashids\Hashids; $hashids = new Hashids(); $id = $hashids->encodeHex('507f1f77bcf86cd799439011'); // y42LW46J9luq3Xq9XMly $hex = $hashids->decodeHex($id); // 507f1f77bcf86cd799439011
Pitfalls
-
When decoding, output is always an array of numbers (even if you encoded only one number):
use Hashids\Hashids; $hashids = new Hashids(); $id = $hashids->encode(1); $hashids->decode($id); // [1]
-
Encoding negative numbers is not supported.
-
If you pass bogus input to
encode(), an empty string will be returned:use Hashids\Hashids; $hashids = new Hashids(); $id = $hashids->encode('123a'); $id === ''; // true
-
Do not use this library as a security measure. Do not encode sensitive data with it. Hashids is not an encryption library.
Randomness
The primary purpose of Hashids is to obfuscate numeric ids. It's not meant or tested to be used as a security or compression tool. Having said that, this algorithm does try to make these ids random and unpredictable:
There is no pattern shown when encoding multiple identical numbers (3 shown in the following example):
use Hashids\Hashids; $hashids = new Hashids(); $hashids->encode(5, 5, 5); // A6t1tQ
The same is true when encoding a series of numbers vs. encoding them separately:
use Hashids\Hashids; $hashids = new Hashids(); $hashids->encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // wpfLh9iwsqt0uyCEFjHM $hashids->encode(1); // jR $hashids->encode(2); // k5 $hashids->encode(3); // l5 $hashids->encode(4); // mO $hashids->encode(5); // nR
Curse words! #$%@
This code was written with the intent of placing the output ids in visible places, like the URL. Therefore, the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other:
c, f, h, i, s, t, u
mojgenie/hashids 适用场景与选型建议
mojgenie/hashids 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 218 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「youtube」 「hash」 「encode」 「decode」 「bitly」 「ids」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mojgenie/hashids 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mojgenie/hashids 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mojgenie/hashids 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Encode integer IDs using a preset or customized symbol set
Runn Me! Serialization Library
Article CSS-ID Frontend Output Optimization
♺ Laravel Nova Hashids card. Convert your ids.
With the JSON codec you will be able to encode into and decode from JSON data in an object oriented way. It wraps PHP's functions `json_encode()` and `json_decode()`.
统计信息
- 总下载量: 218
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-29