Giter Club home page Giter Club logo

base58php's Introduction

Base58 Encoding and Decoding Library for PHP

Build Status Packagist Release MIT License Flattr this

Long Term Support

Each major version of this library will be supported for 5 years after it's initial release. Support will be provided for security and bug fixes.

Version 1 will therefore be supported until the 11th September 2019.

Background

I wanted a replacement for Base64 encoded strings and the Base58 encoding used by Bitcoin looked ideal. I looked around for an existing PHP library which would directly convert a string into Base58 but I couldn't find one, or at least one that worked correctly and was also well tested.

So I decided to create a library with the following goals:

  • Encode/Decode PHP Strings
  • Simple and easy to use
  • Fully Tested
  • Available via Composer

Requirements

This library has the following requirements:

  • PHP => 5.3
  • BC Math Extension

Installation

I recommend you install this library via Composer.

{
    "require": {
        "stephenhill/base58": "~1.0"
    }
}

Basic Usage

require_once('vendor/autoload.php');

$base58 = new StephenHill\Base58();

$base58->encode('Hello World');
$base58->decode('JxF12TrwUP45BMd');

Advanced Usage

By default this library chooses the encoding service provider to use, either GMPService or BCMathService (in that order). If you want to specify one of the included services or your own, you can inject it into the constructor.

require_once('vendor/autoload.php');

$gmp = new StephenHill\GMPService();
$base58 = new StephenHill\Base58(null, $gmp);

$base58->encode('Hello World');
$base58->decode('JxF12TrwUP45BMd');

Also by default, this library uses Bitcoin's Base58 alphabet. If you want to use another variant, you can do this in the constructor.

require_once('vendor/autoload.php');

// Flickr's Base58 Alphabet
$base58 = new StephenHill\Base58('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ');

$base58->encode('Hello World');
$base58->decode('iXf12sRWto45bmC');

Testing

This library is tested using PHPUnit.

$ bin/phpunit

Benchmarking

You can benchmark this library using Athletic. The benchmarking suite also benchmarks PHP's built-in Base64 and Base16 encoding for comparison.

$ bin/athletic -p benchmarks

Example output.

StephenHill\Benchmarks\Base16Event
    Method Name    Iterations    Average Time      Ops/second
    ------------  ------------  --------------    -------------
    encodeBase16: [10,000    ] [0.0000010839939] [922,514.40637]
    decodeBase16: [10,000    ] [0.0000011516809] [868,296.03561]


StephenHill\Benchmarks\Base58BCMathEvent
    Method Name    Iterations    Average Time      Ops/second
    ------------  ------------  --------------    -------------
    encodeBase58: [10,000    ] [0.0001500048161] [6,666.45263]
    decodeBase58: [10,000    ] [0.0001741812706] [5,741.14540]


StephenHill\Benchmarks\Base58GMPEvent
    Method Name    Iterations    Average Time      Ops/second
    ------------  ------------  --------------    -------------
    encodeBase58: [10,000    ] [0.0001168665648] [8,556.76730]
    decodeBase58: [10,000    ] [0.0001385705233] [7,216.54199]


StephenHill\Benchmarks\Base64Event
    Method Name    Iterations    Average Time      Ops/second
    ------------  ------------  --------------    -------------
    encodeBase64: [10,000    ] [0.0000009050369] [1,104,927.29189]
    decodeBase64: [10,000    ] [0.0000009787321] [1,021,730.04312]

Contributing

I welcome everyone to contribute to this library. Please see the Contributing document for details.

License

This library is license under the MIT License (MIT). Please see License File for more information.

Credits

This library was forked from Jeremy Johnstone's Base58 methods on Gist https://gist.github.com/jsjohnst/126883.

Some of the unit tests were based on the following:

base58php's People

Contributors

stephen-hill avatar jsjohnst avatar fil avatar jonaswebdev avatar orthographic-pedant avatar

Stargazers

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