Giter VIP home page Giter VIP logo

avalanche-cb58's Introduction

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.

Latest Version Software License Build StatusCoverage

Install

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 "butterfly-team/avalanche-cb58:^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.

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

Character sets

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

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;

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.

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

use Tuupola\Base58Proxy as Base58;

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

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.

avalanche-cb58's People

Contributors

boppy avatar peter279k avatar tuupola avatar

Watchers

 avatar  avatar

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.