Skip to content
/ base85 Public
forked from tuupola/base85

Base85 encoder and decoder for arbitrary data

License

Notifications You must be signed in to change notification settings

vivait/base85

 
 

Repository files navigation

All your Base85

Latest Version Software License Build Status Coverage

Install

Install with composer.

$ composer require tuupola/base85

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.

$base85 = new Tuupola\Base85;

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

If you are encoding to and from integer use the implicit decodeInteger() and encodeInteger() methods.

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

Note that encoding a string and an integer will yield different results.

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

Encoding modes

ASCII85 encoding. This is the default. 0x00000000 is compressed to z. Spaces are not compressed.

use Tuupola\Base85;

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

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

Adobe ASCII85 encoding is same as previous except data is enclosed between <~ and ~>.

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) encoding. NOTE! Even though specification says input length must be divisible by 4, this is not currently enforced. Spaces and zeroes are not compressed.

use Tuupola\Base85;

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

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

Character set from RFC1924 which is an April fools joke. Spaces and zeroes are not compressed.

use Tuupola\Base85;

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

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

Speed

Install GMP if you can. It is much faster pure PHP encoder. Below benchmarks are for encoding random_bytes(128) data.

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

+-----------------+-----------------+----------+
| subject         | mean            | diff     |
+-----------------+-----------------+----------+
| benchGmpEncoder | 6,604.584ops/s  | +127.21% |
| benchPhpEncoder | 15,006.318ops/s | 0.00%    |
+-----------------+-----------------+----------+

Static Proxy

If you prefer static syntax use the provided static proxy.

use Tuupola\Base85Proxy as Base85;

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

To change static proxy options set the Base85::$options variable.

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 */

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 [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

About

Base85 encoder and decoder for arbitrary data

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 97.2%
  • Makefile 2.8%