Base58 Encoding and Decoding Library for PHP

Overview

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:

Comments
  • PHP Warning: Illegal string offset '4.1268548415' in BCMathService.php on line 91

    PHP Warning: Illegal string offset '4.1268548415' in BCMathService.php on line 91

    master @ 430ba54a3c3144f03e8a03e1679e672c688cb8a6

    3.19.0-26-generic #28-Ubuntu SMP Tue Aug 11 14:16:32 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

    No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 15.04 Release: 15.04 Codename: vivid

    PHP 5.6.4-4ubuntu6.2 (cli) (built: Jul 2 2015 15:29:28) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies with Xdebug v2.2.6, Copyright (c) 2002-2014, by Derick Rethans

    // If there's still a remainder, append it
    if ($decimal > 0) {
        $output .= $this->alphabet[$decimal];    // line 91
    }
    
    opened by shine-on 8
  • More infos added in Exception Message for analysis this error:

    More infos added in Exception Message for analysis this error:

    More infos added in Exception Message for analysis this error:

    PHP Fatal error:  Uncaught exception 'InvalidArgumentException' with message 'Argument $base58 contains invalid characters.'
    
    opened by jonaswebdev 1
  • Bcscale

    Bcscale

    It appears that some distributions of PHP override the default value of "bcmath.scale" to something other than zero. This means we must always set bcdiv()'s $scale parameter to zero.

    opened by stephen-hill 0
  • Fix typographical error(s)

    Fix typographical error(s)

    @stephen-hill, I've corrected a typographical error in the documentation of the base58php project. Specifically, I've changed comparision to comparison. You should be able to merge this pull request automatically. However, if this was intentional or if you enjoy living in linguistic squalor, please let me know and create an issue on my home repository.

    opened by orthographic-pedant 0
  • Performance Improvements

    Performance Improvements

    Performance improvements to the GMP encoding and decoding service provider.

    The performance is gained by a quicker way to convert a string to base10, and back again.

    opened by stephen-hill 0
  • GMP, Service Injection and Benchmarks

    GMP, Service Injection and Benchmarks

    Encoding/Decoding is now done through a service provider class which implements ServiceInterface. The Base58 class by default chooses between GMP and BCMath (in that order), or you can inject your own.

    The included service providers (GMPServer and BCMathService) are tested and benchmarked.

    opened by stephen-hill 0
  • Php service, PHP 7.2 support

    Php service, PHP 7.2 support

    Background

    In the course of my day job, I needed to add base58 support but could not use the bcmath or gmp modules. To support that I wrote a pure PHP service.

    To work with our environment I had to make a couple of changes to support PHP 7.2

    Deficiencies

    I'm aware it has (at least) the following issues:

    • [ ] Need to separate PHPService updates from PHP 7.2 compatibility updates
    • [ ] Need to make it PHP 5.3 compatible but I wanted to get discussion started so I can avoid repeating any other mistakes I have made

    Performance

    According to the included benchmarks, on my machine the pure PHP implementation is actually faster than the bcmath implementation:

    StephenHill\Benchmarks\Base16Event
        Method Name    Iterations    Average Time      Ops/second
        ------------  ------------  --------------    -------------
        encodeBase16: [10,000    ] [0.0000000098705] [101,311,690.82126]
        decodeBase16: [10,000    ] [0.0000001375437] [7,270,417.75004]
    
    
    StephenHill\Benchmarks\Base58BCMathEvent
        Method Name    Iterations    Average Time      Ops/second
        ------------  ------------  --------------    -------------
        encodeBase58: [10,000    ] [0.0000283010483] [35,334.38020]
        decodeBase58: [10,000    ] [0.0000288946390] [34,608.49604]
    
    
    StephenHill\Benchmarks\Base58GMPEvent
        Method Name    Iterations    Average Time      Ops/second
        ------------  ------------  --------------    -------------
        encodeBase58: [10,000    ] [0.0000095309734] [104,921.07725]
        decodeBase58: [10,000    ] [0.0000168622017] [59,304.23668]
    
    
    StephenHill\Benchmarks\Base58PHPEvent
        Method Name    Iterations    Average Time      Ops/second
        ------------  ------------  --------------    -------------
        encodeBase58: [10,000    ] [0.0000179829836] [55,608.12504]
        decodeBase58: [10,000    ] [0.0000192520618] [51,942.48845]
    
    
    StephenHill\Benchmarks\Base64Event
        Method Name    Iterations    Average Time      Ops/second
        ------------  ------------  --------------    -------------
        encodeBase64: [10,000    ] [0.0000000110626] [90,394,482.75863]
        decodeBase64: [10,000    ] [0.0000000604630] [16,539,053.62776]
    

    I have an idea that this is at least partially because I do a direct base change from 256 to 58, while the included services do 256->10->58 instead. I envision experimenting with altering the bcmath and gmp algorithms similarly to see if they also speed up.

    enhancement 
    opened by kalifg 1
  • Add composer suggestion for bcmath, gmp extensions or ...

    Add composer suggestion for bcmath, gmp extensions or ...

    ... make the project strictly fail installing by separation into three packages:

    • base58php requires base58php-service
    • base58php-bcmath provides base58php-service
    • base58php-gmp provides base58php-service

    ( see provide )

    enhancement 
    opened by mcd-php 1
  • Static Class and Helper Functions

    Static Class and Helper Functions

    This pull request adds the following classes:

    • StephenHill\StaticBase58
    • \Base58

    and adds the following functions:

    • base58_encode
    • base58_decode

    This pull request is directly related to issue #14.

    The master branch will be tagged with v1.2 when this branch is merged.

    Still todo is document the changes in readme.md.

    enhancement 
    opened by stephen-hill 2
  • Helper class for static call of encode/decode methods

    Helper class for static call of encode/decode methods

    Hi,

    This is kind of problem that to call encode/decode methods we need to initiate StephenHill\Base58 instance. There are not so many things to configure, and I guess that 99% users of this library will not override encoding service provider. And passing instance of base58 to DI container also does not look very reasonable.

    It will be great to have some kind of StephenHill\StaticBase58 class that provides static encode/decode methods.

    Anyway, @stephen-hill, thank you so much for base58php! It's implemented really great.

    enhancement 
    opened by barbushin 4
Releases(v1.1.5)
  • v1.1.5(Aug 22, 2019)

  • v1.1.4(Jul 20, 2016)

    It appears that some distributions of PHP override the default value of "bcmath.scale" to something other than zero.

    This release overcomes this issue by always settings bcdiv()'s $scale parameter to zero.

    This should fix issues #17, #12, and #10.

    Source code(tar.gz)
    Source code(zip)
Owner
Stephen Hill
Stephen Hill
A bundle to handle encoding and decoding of parameters using OpenSSL and Doctrine lifecycle events.

SpecShaper Encrypt Bundle A bundle to handle encoding and decoding of parameters using OpenSSL and Doctrine lifecycle events. Features include: Master

Mark Ogilvie 48 Nov 4, 2022
PHP Class Encoding featuring popular Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings.

forceutf8 PHP Class Encoding featuring popular \ForceUTF8\Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strin

Sebastián Grignoli 1.6k Dec 22, 2022
Dobren Dragojević 6 Jun 11, 2023
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

null 2 Nov 20, 2021
:date: The VObject library for PHP allows you to easily parse and manipulate iCalendar and vCard objects

sabre/vobject The VObject library allows you to easily parse and manipulate iCalendar and vCard objects using PHP. The goal of the VObject library is

sabre.io 532 Dec 25, 2022
JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

Eboubaker Eboubaker 2 Jul 31, 2022
Backwards compatibility Extension and Library for PHP 8.x and later

colopl_bc Provides various compatibility functions required for PHP (temporary) migration. WARNING This extension is intended for temporary use only.

COLOPL,Inc. 10 Jun 13, 2023
PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP language

php-text-analysis PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP l

null 464 Dec 28, 2022
Columnar analytics for PHP - a pure PHP library to read and write simple columnar files in a performant way.

Columnar Analytics (in pure PHP) On GitHub: https://github.com/envoymediagroup/columna About the project What does it do? This library allows you to w

Envoy Media Group 2 Sep 26, 2022
PHP library providing retry functionality with multiple backoff strategies and jitter support

PHP Backoff Easily wrap your code with retry functionality. This library provides: 4 backoff strategies (plus the ability to use your own) Optional ji

Signature Tech Studio 145 Dec 21, 2022
Currency is a simple PHP library for current and historical currency exchange rates & crypto exchange rates. based on the free API exchangerate.host

Currency Currency is a simple PHP library for current and historical currency exchange rates & crypto exchange rates. based on the free API exchangera

Amr Shawky 19 Dec 12, 2022
A high-level machine learning and deep learning library for the PHP language.

Rubix ML A high-level machine learning and deep learning library for the PHP language. Developer-friendly API is delightful to use 40+ supervised and

Rubix 1.7k Jan 1, 2023
PHP library to create and validate html forms

FormManager Note: this is the documentation of FormManager 6.x For v5.x version Click here Installation: This package requires PHP>=7.1 and is availab

Oscar Otero 145 Sep 20, 2022
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022
Library for PHP 7.4+ to detect Browsers and Devices

This library requires PHP 7.4+. Also a PSR-3 compatible logger and a PSR-16 compatible cache are required. In

Thomas Müller 37 Oct 2, 2022
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022
This shell script and PHP file create a browseable HTML site from the Zig standard library source.

Browseable Zig standard library This shell script and PHP file create a browseable HTML site from the Zig standard library source. The idea is to inve

Dave Gauer 3 Mar 20, 2022
A library for reading and writing DNA test kit files in PHP.

php-dna Requirements php-dna 1.0+ requires PHP 8.0 (or later). Installation There are two ways of installing php-dna. Composer To install php-dna in y

Family Tree 365 4 Aug 31, 2022
A small library to help run PHP servers easily and quickly.

PHP Server A small library to help run PHP servers easily and quickly. Installation composer require ahmard/php-server Usage PHP Built-In Server An i

Ahmad Mustapha 9 Dec 31, 2022