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.
Install with composer.
$ composer require tuupola/base58
This branch requires PHP 7.1 or up. The older 1.x branch supports also PHP 5.6 and 7.0.
$ composer require "tuupola/base58:^1.0"
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.
$base58 = new Tuupola\Base58;
$encoded = $base58->encode(random_bytes(128));
$decoded = $base58->decode($encoded);
If you are encoding to and from integer use the implicit decodeInteger() and encodeInteger() methods.
$integer = $base58->encodeInteger(987654321); /* 1TFvCj */
print $base58->decodeInteger("1TFvCj", true); /* 987654321 */
Also note that encoding a string and an integer will yield different results.
$string = $base58->encode("987654321"); /* gE62MGeOBMPt */
$integer = $base58->encodeInteger(987654321); /* 1TFvCj */
By default Base58 uses GMP style character set. Shortcuts are provided for Bitcoin, Flickr, Ripple and IPFS character sets. You can also use any custom 58 characters.
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 */
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;
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();
}
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.
$ 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% |
+-----------------------+------------------+--------------+
If you prefer to use static syntax use the provided static proxy.
use Tuupola\Base58Proxy as Base58;
$encoded = Base58::encode(random_bytes(128));
$decoded = Base58::decode($encoded);
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
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.