Giter Site home page Giter Site logo

base85's Introduction

All your Base85

Latest Version Software License Build StatusCoverage

Install

Install with composer.

$ composer require tuupola/base85

This branch requires PHP 7.1 or up. The older 1.x branch supports also PHP 5.6 and 7.0.

$ composer require "tuupola/base85:^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.

$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

Pure PHP encoder seems to be faster than the GMP implementation. Below benchmarks use random_bytes(128) as data.

$ 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 |
+-----------------+-----------------+-------+

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.

base85's People

Contributors

peter279k avatar tuupola avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

base85's Issues

Wrong Z85 character set

Currently the library uses

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-:+=^!/*?&<>()[]{}@%$#

while it should be

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#

Different versions of PHP output different results

I tested "Base85" on PHP 5.X and PHP 7.X. The result of PHP 5.X seems to be abnormal. To find out the cause of the problem, I tested more text. The conclusion is as follows:

  • Decoding all successful
  • Encoding in PHP 5.X is abnormal
  • English character encoding succeeded
  • Japanese and Chinese character encoding is abnormal
  • Invisible characters are generated, but the English part is normal

The problem seems to be in unpack("N*", $data) ?
I'm confused about it.

PHP 8.x support

Hi,

Any plans to upgrade this package for it to be used with PHP 8.x? I cloned the repository and tried to modify the composer.json dependencies but it's not that simple if I want to properly test the package (phpbench doesn't run on PHP 8 either) although it works with the same examples in the Usage section.

Any change to publish an update? :)

Returns a different result on a 32-bit platform vs 64-bit

With a code like this:

echo Tuupola\Base85Proxy::encode(md5("test", true)), "\n";

amd64:

$'/lH7Np6%b2,mG-7?1o

armv7el:

$'/lH7Np6%epLbF-7?1o

PHP 7.4 on both.

--
Just reported a similar bug to another base85 package: scottchiefbaker/php-base85#2
that one has a more severe problem. "unpack" is problematic on 32-bit vs 64-bit, yours almost works, but also couldn't escape it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.