tuupola/base85

任意数据的Base85编码和解码器

2.1.0 2021-08-02 13:20 UTC

This package is auto-updated.

Last update: 2024-09-18 14:58:57 UTC


README

Latest Version Software License Build StatusCoverage

安装

使用composer进行安装。

$ composer require tuupola/base85

此分支需要PHP 7.1或更高版本。较旧的1.x分支也支持PHP 5.6和7.0。

$ composer require "tuupola/base85:^1.0"

使用方法

此包包含纯PHP和基于GMP的编码器。默认情况下,如果安装了扩展,编码器和解码器将使用GMP函数。如果GMP不可用,则将使用纯PHP编码器。

$base85 = new Tuupola\Base85;

$encoded = $base85->encode(random_bytes(128));
$decoded = $base85->decode($encoded);

如果您正在对整数进行编码和解码,请使用隐式的decodeInteger()encodeInteger()方法。

$integer = $base85->encodeInteger(987654321); /* 3o4PT */
print $base85->decodeInteger("3o4PT", true); /* 987654321 */

请注意,编码字符串和整数会产生不同的结果。

$string = $base85->encode("987654321"); /* 3B/rU2)I*E0` */
$integer = $base85->encodeInteger(987654321); /* 3o4PT */

编码模式

ASCII85编码。这是默认模式。0x00000000被压缩为z。空格不被压缩。

use Tuupola\Base85;

$ascii85 = new Base85([
    "characters" => Base85::ASCII85,
    "compress.spaces" => false,
    "compress.zeroes" => true
]);

print $ascii85->encode("Hello world!"); /* 87cURD]j7BEbo80 */

Adobe ASCII85编码与之前相同,但数据被括在<~~>之间。

use Tuupola\Base85;

$adobe85 = new Base85([
    "characters" => Base85::ASCII85,
    "compress.spaces" => false,
    "compress.zeroes" => true,
    "prefix" => "<~",
    "suffix" => "~>"
]);

print $adobe85->encode("Hello world!"); /* <~87cURD]j7BEbo80~> */

ZeroMQ (Z85)编码。注意!尽管规范说输入长度必须是4的倍数,但目前并没有强制执行。空格和零不被压缩。

use Tuupola\Base85;

$z85 = new Base85([
    "characters" => Base85::Z85,
    "compress.spaces" => false,
    "compress.zeroes" => false
]);

print $z85->encode("Hello world!"); /* NM=qnZy<MXa+]NF */

来自RFC1924的字符集,这是一个愚人节玩笑。空格和零不被压缩。

use Tuupola\Base85;

$rfc1924 = new Base85([
    "characters" => Base85::RFC1924,
    "compress.spaces" => false,
    "compress.zeroes" => false
]);

print $rfc1924->encode("Hello world!"); /* NM&qnZy<MXa%^NF */

速度

纯PHP编码器似乎比GMP实现更快。以下基准测试使用random_bytes(128)作为数据。

$ php --version
PHP 8.0.1 (cli) (built: Jan  8 2021 09:07:02) ( NTS )

$ vendor/bin/phpbench run benchmarks/ --report=default

+-----------------+-----------------+-------+
| subject         | mean            | diff  |
+-----------------+-----------------+-------+
| benchGmpDecoder | 24,515.692ops/s | 1.01x |
| benchPhpDecoder | 24,666.509ops/s | 1.00x |
+-----------------+-----------------+-------+
+-----------------+-----------------+-------+
| subject         | mean            | diff  |
+-----------------+-----------------+-------+
| benchGmpEncoder | 9,654.448ops/s  | 4.76x |
| benchPhpEncoder | 45,944.903ops/s | 1.00x |
+-----------------+-----------------+-------+

静态代理

如果您更喜欢静态语法,请使用提供的静态代理。

use Tuupola\Base85Proxy as Base85;

print Base85::encode("Hello world!") /* 87cURD]j7BEbo80 */

要更改静态代理选项,请设置Base85::$options变量。

use Tuupola\Base85;
use Tuupola\Base85Proxy as Z85;

Z85::$options = [
    "characters" => Base85::Z85,
    "compress.spaces" => false,
    "compress.zeroes" => false
];

print Z85::encode("Hello world!"); /* NM=qnZy<MXa+]NF */

测试

您可以选择手动运行测试或在每次代码更改时自动运行测试。自动测试需要entr工作。

$ make test
$ brew install entr
$ make watch

贡献

有关详细信息,请参阅CONTRIBUTING

安全性

如果您发现任何安全相关的问题,请通过电子邮件tuupola@appelsiini.net而不是使用问题跟踪器。

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。