Need help with base62?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

tuupola
140 Stars 14 Forks MIT License 144 Commits 2 Opened issues

Description

Base62 encoder and decoder for arbitrary data

Services available

!
?

Need anything else?

Contributors list

# 6,197
PHP
Shell
token-a...
JavaScr...
132 commits
# 82,542
supervi...
http2-p...
php7
Symfony
4 commits
# 22,077
whois-l...
php7
Nette
zend-fr...
1 commit
# 102,866
PHP
aws-lam...
lambda
Symfony
1 commit
# 38,595
PHP
Shell
Go
backoff
1 commit

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.

Latest Version Packagist Software License Build Status Coverage

Install

Install with composer.

$ composer require tuupola/base62

This branch requires PHP 7.1 or up. The older

1.x
branch supports also PHP 5.6 and 7.0.
$ 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.

$base62 = new Tuupola\Base62;

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

If you are encoding to and from integer use the implicit

decodeInteger()
and
encodeInteger()
methods.
$integer = $base62->encodeInteger(987654321); /* 14q60P */
print $base62->decodeInteger("14q60P"); /* 987654321 */

Note that encoding a string and an integer will yield different results.

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

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.

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.
$ vendor/bin/phpbench run benchmarks/ --report=default

+-----------------------+------------------+-----------+ | subject | mean | diff | +-----------------------+------------------+-----------+ | benchGmpEncoder | 110,619.469ops/s | 1.00x | | benchGmpEncoderCustom | 106,157.113ops/s | 1.04x | | benchPhpEncoder | 373.204ops/s | 296.40x | | benchBcmathEncoder | 35.494ops/s | 3,116.58x | +-----------------------+------------------+-----------+

Static Proxy

If you prefer to use static syntax use the provided static proxy.

use Tuupola\Base62Proxy as Base62;

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

$encoded2 = Base62::encodeInteger(987654321); $decoded2 = Base62::decodeInteger($encoded2);

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.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.