tuupola/base32
Composer 安装命令:
composer require tuupola/base32
包简介
Base32 encoder and decoder for arbitrary data
关键字:
README 文档
README
This library implements Base32 encoding. In addition to integers it can encode and decode any arbitrary data.
Install
Install with composer.
$ composer require tuupola/base32
This branch requires PHP 7.1 or up. The older 1.x branch supports also PHP 5.6 and 7.0.
$ composer require "tuupola/base32:^1.0"
Usage
This package has both pure PHP and GMP based encoders. By default encoder and decoder will use GMP functions if the extension is installed. If GMP is not available pure PHP encoder will be used instead.
$base32 = new Tuupola\Base32; $encoded = $base32->encode(random_bytes(128)); $decoded = $base32->decode($encoded);
If you are encoding to and from integer use the implicit decodeInteger() and encodeInteger() methods.
$integer = $base32->encodeInteger(987654321); /* 5N42FR== */ print $base32->decodeInteger("5N42FR=="); /* 987654321 */
Also note that encoding a string and an integer will yield different results.
$integer = $base32->encodeInteger(987654321); /* 5N42FR== */ $string = $base32->encode("987654321"); /* FHE4DONRVGQZTEMI= */
Encoding Modes
RCF4684
RCF4684 is the default when encoder is created without passing any parameters. Explicit parameters shown below.
use Tuupola\Base32; $base32 = new Base32([ "characters" => Base32::RFC4648, "padding" => "=" ]); print $base32->encode("Hello world!"); /* JBSWY3DPEB3W64TMMQQQ==== */
RCF4684 HEX
RCF4684 base32hex encoding is identical to previous, except for the character set. This encoding is lexically sortable.
$base32hex = new Base32([ "characters" => Base32::HEX, "padding" => "=" ]); print $base32->encode("Hello world!"); /* 91IMOR3F41RMUSJCCGGG==== */
GMP
GMP encoding is identical to previous. Example below is shown with padding disabled.
$gmp = new Base32([ "characters" => Base32::GMP, "padding" => false ]); print $gmp->encode("Hello world!"); /* 91IMOR3F41RMUSJCCGGG */
Crockford's Base32
When decoding, upper and lower case letters are accepted, and i and l will be treated as 1 and o will be treated as 0. When encoding, only upper case letters are used. Hyphens are ignored during decoding.
$crockford = new Base32([ "characters" => Base32::CROCKFORD, "padding" => false, "crockford" => true, ]); print $crockford->encode("Hello world!"); /* 91JPRV3F41VPYWKCCGGG */ print $crockford->decode("91JPRV3F41VPYWKCCGGG"); /* Hello world! */ print $crockford->decode("91jprv3f41vpywkccggg"); /* Hello world! */ print $crockford->decode("9ljprv3f4lvpywkccggg"); /* Hello world! */
Z-base-32 [WIP]
http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
Character Sets
By default Base32 uses RFC4648 character set. Shortcut is provided for other commonly used character sets. You can also use any custom character set of 32 unique characters.
use Tuupola\Base32; print Base32::CROCKFORD; /* 0123456789ABCDEFGHJKMNPQRSTVWXYZ */ print Base32::RFC4648; /* ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 */ print Base32::ZBASE32; /* ybndrfg8ejkmcpqxot1uwisza345h769 */ print Base32::GMP; /* 0123456789ABCDEFGHIJKLMNOPQRSTUV */ print Base32::HEX; /* 0123456789ABCDEFGHIJKLMNOPQRSTUV */ $default = new Base32(["characters" => Base32::RFC4648]); $crockford = new Base32(["characters" => Base32::CROCKFORD]); print $default->encode("Hello world!"); /* JBSWY3DPEB3W64TMMQQQ==== */ print $inverted->encode("Hello world!"); /* 91JPRV3F41VPYWKCCGGG==== */
Speed
Install GMP if you can. It is reasonably faster pure PHP encoder. Below benchmarks are for encoding random_bytes(128) data.
$ make benchmark
benchmark: Base32Bench
+-----------------------+----------------+-------+
| subject | mean | diff |
+-----------------------+----------------+-------+
| benchGmpEncoder | 8,361.204ops/s | 1.00x |
| benchGmpEncoderCustom | 8,393.487ops/s | 1.00x |
| benchPhpEncoder | 6,881.365ops/s | 1.22x |
+-----------------------+----------------+-------+
Static Proxy
If you prefer to use static syntax use the provided static proxy.
use Tuupola\Base32Proxy as Base32; $encoded = Base32::encode(random_bytes(128)); $decoded = Base32::decode($encoded);
Testing
You can run tests either manually or automatically on every code change. Automatic tests require entr to work.
$ make test
$ brew install entr $ make watch
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
tuupola/base32 适用场景与选型建议
tuupola/base32 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 608.72k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2017 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「base32」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tuupola/base32 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tuupola/base32 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tuupola/base32 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Sets of digits or characters that may be used for base conversions, encoding and decoding tasks, and input validation
Implementation of bitcoin cash base32 addresses
Base 32 encoder / decoder
Safe number conversion between arbitrary bases.
Convert integers between arbitrary bases with custom alphabets, salt, padding, and big-integer support.
PHP library for Base32 encode/decode operations. (fork from deleted ejz/base32)
统计信息
- 总下载量: 608.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 20
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-26