2017 © Pedro Peláez
 

library base62

Base62 encoder and decoder for arbitrary data

image

tuupola/base62

Base62 encoder and decoder for arbitrary data

  • Friday, June 15, 2018
  • by tuupola
  • Repository
  • 4 Watchers
  • 51 Stars
  • 64,938 Installations
  • PHP
  • 14 Dependents
  • 0 Suggesters
  • 11 Forks
  • 1 Open issues
  • 9 Versions
  • 20 % Grown

The README.md

Base62

This library implements base62 encoding. In addition to integers it can encode and decode any arbitrary data. This is useful for example when generating url safe random tokens for database identifiers., (*1)

Latest Version Packagist Software License Build Status Coverage, (*2)

Install

Install with composer., (*3)

``` bash $ composer require tuupola/base62, (*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/base62:^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 $base62 = new Tuupola\Base62;, (*6)

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


If you are encoding to and from integer use the implicit `decodeInteger()` and `encodeInteger()` methods. ``` php $integer = $base62->encodeInteger(987654321); /* 14q60P */ print $base62->decodeInteger("14q60P"); /* 987654321 */

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

``` php $string = $base62->encode("987654321"); /* KHc6iHtXW3iD / $integer = $base62->encodeInteger(987654321); / 14q60P */, (*9)


## Character sets By default Base62 uses GMP style character set. Shortcut is provided for the inverted character set which is also commonly used. You can also use any custom character set of 62 unique characters. ```php use Tuupola\Base62; print Base62::GMP; /* 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz */ print Base62::INVERTED; /* 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ */ $default = new Base62(["characters" => Base62::GMP]); $inverted = new Base62(["characters" => Base62::INVERTED]); print $default->encode("Hello world!"); /* T8dgcjRGuYUueWht */ print $inverted->encode("Hello world!"); /* t8DGCJrgUyuUEwHT */

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., (*10)

$ php --version
PHP 8.0.7 (cli) (built: Jun  4 2021 03:50:01) ( NTS )

$ make bench

+-----------------------+------------------+-----------+
| subject               | mean             | diff      |
+-----------------------+------------------+-----------+
| benchGmpDecoder       | 140,409.997ops/s | 1.10x     |
| benchGmpDecoderCustom | 154,607.297ops/s | 1.00x     |
| benchPhpDecoder       | 721.147ops/s     | 214.39x   |
| benchBcmathDecoder    | 72.191ops/s      | 2,141.64x |
+-----------------------+------------------+-----------+

+-----------------------+------------------+-----------+
| subject               | mean             | diff      |
+-----------------------+------------------+-----------+
| benchGmpEncoder       | 352,609.309ops/s | 1.00x     |
| benchGmpEncoderCustom | 350,140.056ops/s | 1.01x     |
| benchPhpEncoder       | 669.959ops/s     | 526.31x   |
| benchBcmathEncoder    | 72.956ops/s      | 4,833.21x |
+-----------------------+------------------+-----------+

Static Proxy

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

``` php use Tuupola\Base62Proxy as Base62;, (*12)

$encoded = Base62::encode(random_bytes(128)); $decoded = Base62::decode($encoded);, (*13)

$encoded2 = Base62::encodeInteger(987654321); $decoded2 = Base62::decodeInteger($encoded2);, (*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

15/06 2018

dev-master

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

Base62 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base62

28/03 2018

0.10.0

0.10.0.0 https://github.com/tuupola/base62

Base62 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base62

28/03 2018

dev-character-set

dev-character-set https://github.com/tuupola/base62

Base62 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base62

12/10 2017

dev-leading-0x00

dev-leading-0x00 https://github.com/tuupola/base62

Base62 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base62

09/10 2017

0.9.0

0.9.0.0 https://github.com/tuupola/base62

Base62 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base62

12/03 2017

0.8.0

0.8.0.0 https://github.com/tuupola/base62

Base62 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base62

09/10 2016

0.7.0

0.7.0.0 https://github.com/tuupola/base62

Base62 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base62

06/05 2016

0.6.0

0.6.0.0 https://github.com/tuupola/base62

Base62 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base62

05/05 2016

0.5.0

0.5.0.0 https://github.com/tuupola/base62

Base62 encoder and decoder for arbitrary data

  Sources   Download

MIT

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

base62