PHPackages                             tuupola/base62 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tuupola/base62

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tuupola/base62
==============

Base62 encoder and decoder for arbitrary data

2.1.1(5mo ago)1994.3M↓29%19[3 issues](https://github.com/tuupola/base62/issues)20MITPHPPHP ^7.1|^8.0CI failing

Since May 5Pushed 4mo ago6 watchersCompare

[ Source](https://github.com/tuupola/base62)[ Packagist](https://packagist.org/packages/tuupola/base62)[ Docs](https://github.com/tuupola/base62)[ RSS](/packages/tuupola-base62/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelogDependencies (5)Versions (18)Used By (20)

Base62
======

[](#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](https://paragonie.com/blog/2015/09/comprehensive-guide-url-parameter-encryption-in-php).

[![Latest Version](https://camo.githubusercontent.com/1f8fb8517eaffd262079572da55c44b958a616bb78b74d84487e977cde4f058f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f747575706f6c612f6261736536322e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tuupola/base62)[![Packagist](https://camo.githubusercontent.com/9630511906a0654fc8967129d2e1cd4b749e963cf89578e3bf7cc54a5a16ae8d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f747575706f6c612f6261736536322e737667)](https://packagist.org/packages/tuupola/base62)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build Status](https://camo.githubusercontent.com/bf0d8b9c950a7bf7bc66fe4100dd1de4f2022ce42fd5b41b63444d55c992b02e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f747575706f6c612f6261736536322f74657374732e796d6c3f6272616e63683d322e78267374796c653d666c61742d737175617265)](https://github.com/tuupola/base62/actions)[![Coverage](https://camo.githubusercontent.com/115f7630a7cc1be382a304b71733706a18e6bf399bb0c629c256b1c6476fb5ac/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f747575706f6c612f6261736536322e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/tuupola/base62)

Install
-------

[](#install)

Install with [composer](https://getcomposer.org/).

```
$ 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
-----

[](#usage)

This package has both pure PHP and [GMP](http://php.net/manual/en/ref.gmp.php) 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
--------------

[](#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 */
print $default->encodeInteger(27);  /* R */
print $inverted->encodeInteger(27);  /* r */
```

Speed
-----

[](#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.

```
$ 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 |
+-----------------------+------------------+-----------+

```

If you are sure you have GMP installed you can use the `GmpEncoder` directly.

```
if (function_exists("gmp_init")) {
    $base62 = new Tuupola\Base62\GmpEncoder();
    $base62->encode("Hello world!"); /* T8dgcjRGuYUueWht */
} else {
    /* Handle error... */
}
```

Static Proxy
------------

[](#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
-------

[](#testing)

You can run tests either manually or automatically on every code change. Automatic tests require [entr](http://entrproject.org/) to work.

```
$ make test
```

```
$ brew install entr
$ make watch
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

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

License
-------

[](#license)

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

###  Health Score

65

—

FairBetter than 99% of packages

Maintenance74

Regular maintenance activity

Popularity60

Solid adoption and visibility

Community33

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 94.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~221 days

Recently: every ~642 days

Total

17

Last Release

127d ago

Major Versions

0.11.1 → 1.0.0-rc12018-12-07

1.x-dev → 2.0.02018-12-30

2.1.1 → 3.x-dev2026-01-11

PHP version history (6 changes)0.5.0PHP ^5.5 || ^7.0

0.11.1PHP ^5.6 || ^7.0

1.0.0-rc1PHP ^5.6|^7.0

2.0.0PHP ^7.1

2.1.0PHP ^7.1|^8.0

3.x-devPHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/3325405a7d8a43bc40dd0e760a4b7f268fba32a7150cf0327f64f13d1661df0b?d=identicon)[tuupola](/maintainers/tuupola)

---

Top Contributors

[![tuupola](https://avatars.githubusercontent.com/u/21913?v=4)](https://github.com/tuupola "tuupola (153 commits)")[![1ma](https://avatars.githubusercontent.com/u/1456708?v=4)](https://github.com/1ma "1ma (4 commits)")[![magnetik](https://avatars.githubusercontent.com/u/345029?v=4)](https://github.com/magnetik "magnetik (1 commits)")[![Rican7](https://avatars.githubusercontent.com/u/742384?v=4)](https://github.com/Rican7 "Rican7 (1 commits)")[![scybulski](https://avatars.githubusercontent.com/u/10341108?v=4)](https://github.com/scybulski "scybulski (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (1 commits)")

---

Tags

base62decoderencoderphpbase62

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tuupola-base62/health.svg)

```
[![Health](https://phpackages.com/badges/tuupola-base62/health.svg)](https://phpackages.com/packages/tuupola-base62)
```

###  Alternatives

[elfsundae/laravel-hashid

A simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.

415246.3k2](/packages/elfsundae-laravel-hashid)[tuupola/ksuid

K-Sortable Globally Unique IDs

1081.2M4](/packages/tuupola-ksuid)[xobotyi/basen

Text and integers encoding utilities for PHP with no extensions dependencies. Base32, Base58, Base64 and much more!

1219.6k](/packages/xobotyi-basen)[base62/base62

base62 encoder and decoder also for big numbers with Laravel integration

169.5k](/packages/base62-base62)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
