Base62 encoder and decoder for arbitrary data
PHP
Latest commit 10f2b64 Jan 4, 2017 @tuupola Hello 2017

README.md

All your Base62

Latest Version Software License Build Status HHVM Status Coverage

Install

Install with composer.

$ composer require tuupola/base62

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.

use Tuupola\Base62;

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

Or if you prefer to use object syntax.

use Tuupola\Base62\Encoder as Base62;

$base62 = new Base62;

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

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.

$ phpbench run benchmarks/ --report=default

+--------------------+---------------+---------+
| subject            | mean          | diff    |
+--------------------+---------------+---------+
| benchGmpEncoder    | 50.900μs      | 0.00%   |
| benchPhpEncoder    | 39,044.400μs  | +99.87% |
| benchBcmathEncoder | 139,278.500μs | +99.96% |
+--------------------+---------------+---------+

Why yet another Base62 encoder?

Because all encoders I found were encoders only for integer numbers. I needed to be able to encode arbitrary data. This is usefull for example when generating url safe random tokens for database identifiers.

$token = Base62::encode(random_bytes(9));

If you are already using UUIDs, they can also be encoded.

use Ramsey\Uuid\Uuid;
use Tuupola\Base62;

$uuid = Uuid::fromString("d84560c8-134f-11e6-a1e2-34363bd26dae");
Base62::encode($uuid->getBytes()); /* 6a630O1jrtMjCrQDyG3D3O */
$uuid = Uuid::fromBytes(Base62::decode("6a630O1jrtMjCrQDyG3D3O"));
print $uuid; /* d84560c8-134f-11e6-a1e2-34363bd26dae */

Note that if you are encoding to and from integer you need to pass boolean true as the second argument for decode() method. This is because decode() method does not know if the original data was an integer or binary data.

$integer =  Base62::encode(987654321); /* 14q60P */
print Base62::decode("14q60P", true); /* 987654321 */

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

$integer =  Base62::encode(987654321); /* 14q60P */
$string = Base62::encode("987654321"); /* KHc6iHtXW3iD */

Testing

You can run tests either manually...

$ composer test

... or automatically on every code change. This requires entr to work.

$ composer watch

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.