semperton / multibase
支持多字节字符字母表的基转换器。
2.1.0
2022-11-11 14:43 UTC
Requires
- php: >=7.4
- ext-mbstring: *
Requires (Dev)
- phpbench/phpbench: ^1.2
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.24
Suggests
- ext-gmp: Install GMP extension for fast conversion up to base 62.
README
Semperton Multibase
支持多字节字符字母表的基转换器。
安装
只需使用Composer
composer require semperton/multibase
Multibase 需要 PHP 7.4+ 和 mbstring 扩展
提示:安装 GMP 扩展以加快基转换速度至 base 62
包
包含的基转换器
- Base58
- Base62
- Base85 RFC 1924
用法
Multibase 转换器能够转换任何任意数据。用作 URL / MD5 缩短,密码生成器,Base64 替代方案...
use Semperton\Multibase\Base62; use Semperton\Multibase\Base85; // basic usage $base62 = new Base62(); $base62->encode('Hello World'); // 73XpUgyMwkGr29M $base62->decode('73XpUgyMwkGr29M'); // Hello World // shorten md5 hash $hash = md5('Hello World'); // b10a8db164e0754105b7a99be72e3fe5 $short = $base62->encode(hex2bin($hash)); // 5O4SoozqXEOwlYtvkC5zkr $short = $base62->encode(md5('Hello World', true)); // same as above $decoded = $base62->decode($short); $hash === bin2hex($decoded); // true // password generation $bytes = openssl_random_pseudo_bytes(16); $password = (new Base85())->encode($bytes); // e.g. Ncg>RWSYO+2t@~G8PO0J
自定义转换器
您可以使用自己的字母表(支持多字节)创建自定义转换器。只为了好玩,一个表情符号转换器怎么样?
use Semperton\Multibase\Transcoder\BaseTranscoder; $emojiTranscoder = new BaseTranscoder( '🧳🌂☂️🧵🪡🪢🧶👓🕶🥽🥼🦺👔👕👖🧣🧤🧥🧦👗👘🥻🩴🩱🩲' . '🩳👙👚👛👜👝🎒👞👟🥾🥿👠👡🩰👢👑👒🎩🎓🧢⛑🪖💄💍💼' ); $encoded = $emojiTranscoder->encode('Hello World'); // ☂🪢👟🩴🩰🥻👚👙🧢🩲🧥🥽🎩👙👝🎒 $emojiTranscoder->decode($encoded); // Hello World