2017 © Pedro Peláez
 

library base58

Base58 encoder and decoder for arbitrary data

image

tuupola/base58

Base58 encoder and decoder for arbitrary data

  • Sunday, August 20, 2017
  • by tuupola
  • Repository
  • 1 Watchers
  • 6 Stars
  • 1,493 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 2 Open issues
  • 2 Versions
  • 13 % Grown

The README.md

Base58

This library implements Base58 encoding. In addition to integers it can encode and decode any arbitrary data. That said, Base58 is well suited for decoding big integers but is not designed to decode long portions of binary data., (*1)

Latest Version Software License Build StatusCoverage, (*2)

Install

Install with composer., (*3)

``` bash $ composer require tuupola/base58, (*4)


This branch requires PHP 7.1 or up. The older 1.x branch supports also PHP 5.6 and 7.0. ``` bash $ composer require "tuupola/base58:^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., (*5)

``` php $base58 = new Tuupola\Base58;, (*6)

$encoded = $base58->encode(random_bytes(128)); $decoded = $base58->decode($encoded);, (*7)


If you are encoding to and from integer use the implicit decodeInteger() and encodeInteger() methods. ``` php $integer = $base58->encodeInteger(987654321); /* 1TFvCj */ print $base58->decodeInteger("1TFvCj", true); /* 987654321 */

Also note that encoding a string and an integer will yield different results., (*8)

``` php $string = $base58->encode("987654321"); /* gE62MGeOBMPt / $integer = $base58->encodeInteger(987654321); / 1TFvCj */, (*9)


## Character sets By default Base58 uses GMP style character set. Shortcuts are provided for [Bitcoin](https://github.com/bitcoin/bitcoin/blob/master/src/base58.cpp), [Flickr](https://www.flickr.com/groups/api/discuss/72157616713786392/), [Ripple](https://wiki.ripple.com/Accounts) and [IPFS](https://github.com/richardschneider/net-ipfs-core#base58) character sets. You can also use any custom 58 characters. ```php use Tuupola\Base58; print Base58::GMP /* 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv */ print Base58::BITCOIN /* 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz */ print Base58::FLICKR /* 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ */ print Base58::RIPPLE /* rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz */ print Base58::IPFS /* 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz */ $default = new Base58(["characters" => Base58::GMP]); $bitcoin = new Base58(["characters" => Base58::BITCOIN]); print $default->encode("Hello world!"); /* 1LDlk6QWOejX6rPrJ */ print $bitcoin->encode("Hello world!"); /* 2NEpo7TZRhna7vSvL */

Base58Check support

This library supports the Base58Check encoding used in Bitcoin addresses. Decoding validates both version and the checksum. If either of them fails a RuntimeException will be thrown;, (*10)

use Tuupola\Base58;

$base58check = new Base58([
    "characters" => Base58::BITCOIN,
    "check" => true,
    "version" => 0x00
]);

print $base58check->encode("Hello world!"); /* 19wWTEnNTWna86WmtFsTAr5 */

try {
    $base58check->decode("19wWTEnNTWna86WmtFsTArX");
} catch (RuntimeException $exception) {
    /* Checksum "84fec52c" does not match the expected "84fec512" */
    print $exception->getMessage();
}

Speed

Install GMP if you can. It is much faster pure PHP encoder. Below benchmarks are for encoding random_bytes(128) data. BCMatch encoder is also included but it is mostly just a curiosity. It is too slow to be usable., (*11)

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

+-----------------------+------------------+--------------+
| subject               | mean             | diff         |
+-----------------------+------------------+--------------+
| benchGmpEncoder       | 101,832.994ops/s | 0.00%        |
| benchGmpEncoderCustom | 97,656.250ops/s  | +4.28%       |
| benchPhpEncoder       | 305.913ops/s     | +33,188.19%  |
| benchBcmathEncoder    | 32.457ops/s      | +313,643.79% |
+-----------------------+------------------+--------------+

Static Proxy

If you prefer to use static syntax use the provided static proxy., (*12)

``` php use Tuupola\Base58Proxy as Base58;, (*13)

$encoded = Base58::encode(random_bytes(128)); $decoded = Base58::decode($encoded);, (*14)


## Testing You can run tests either manually or automatically on every code change. Automatic tests require [entr](http://entrproject.org/) to work. ``` bash $ make test

bash $ brew install entr $ make watch, (*15)

Contributing

Please see CONTRIBUTING for details., (*16)

Security

If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker., (*17)

License

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

The Versions

20/08 2017

dev-master

9999999-dev https://github.com/tuupola/base58

Base58 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base58

09/06 2017

0.1.0

0.1.0.0 https://github.com/tuupola/base58

Base58 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

 

The Development Requires

base58