PHPackages                             gpslab/base64uid - 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. gpslab/base64uid

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

gpslab/base64uid
================

Generate UID like YouTube

v1.2.0(6y ago)68103.4k↓25.5%8MITPHPPHP &gt;=5.3.0CI failing

Since Jun 20Pushed 6y ago1 watchersCompare

[ Source](https://github.com/gpslab/base64uid)[ Packagist](https://packagist.org/packages/gpslab/base64uid)[ Docs](https://github.com/gpslab/payload)[ RSS](/packages/gpslab-base64uid/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (6)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/6e8fd554652d82074ccc038cb0f952841bcb916ef0c7edceababa922462bf83f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6770736c61622f6261736536347569642e7376673f6d61784167653d33363030266c6162656c3d737461626c65)](https://packagist.org/packages/gpslab/base64uid)[![Total Downloads](https://camo.githubusercontent.com/b88d7cdad7b82886e6dcd4619fe46bf0bb2864cc1a30c94f74f6e06e5786591a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6770736c61622f6261736536347569642e7376673f6d61784167653d33363030)](https://packagist.org/packages/gpslab/base64uid)[![Build Status](https://camo.githubusercontent.com/78e2ea6bf4b4eb1a754188a141283d4a0f9fef8065ab8c452b274ef1ae3bb31c/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6770736c61622f6261736536347569642e7376673f6d61784167653d33363030)](https://travis-ci.org/gpslab/base64uid)[![Coverage Status](https://camo.githubusercontent.com/07bb9bcb1b1a43a328974bdad26d4e44c1cf076ae415c70a4ace5876da40a4c8/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6770736c61622f6261736536347569642e7376673f6d61784167653d33363030)](https://coveralls.io/github/gpslab/base64uid?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/28c1df208c0f4b8966ad6092b700927a8871444bc732a11f88d5af007be94a47/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6770736c61622f6261736536347569642e7376673f6d61784167653d33363030)](https://scrutinizer-ci.com/g/gpslab/base64uid/?branch=master)[![SensioLabs Insight](https://camo.githubusercontent.com/81d7e9da263fb093061433ede2bdfa2edb27e5d07cab12a6495f18360fa7ad54/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f30666562323262372d623634642d343632642d623862612d6461343965353438626537302e7376673f6d61784167653d33363030266c6162656c3d534c496e7369676874)](https://insight.sensiolabs.com/projects/0feb22b7-b64d-462d-b8ba-da49e548be70)[![StyleCI](https://camo.githubusercontent.com/85627889b3b36bda784d0b6d718890fa4a3311addbb7f46259fcabbeb7141f1a/68747470733a2f2f7374796c6563692e696f2f7265706f732f39343838353239392f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/94885299)[![License](https://camo.githubusercontent.com/86c45cf417813f1a730d66a9db62710429488742960700c6c760af79ce427fc2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6770736c61622f6261736536347569642e7376673f6d61784167653d33363030)](https://github.com/gpslab/base64uid)

Base64 UID
==========

[](#base64-uid)

Generate UID like YouTube.

Introduction
------------

[](#introduction)

The library generates a unique identifier consisting of 64 characters and a length of 10 characters *(you can change the length of the identifier).* This gives us 6410 = 260 = 1 152 921 504 606 846 976 combinations.

To represent this number, imagine that in order to get all possible values of identifiers with a length of **10**characters and generating an ID every microsecond, it takes **36 559** years.

[UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) works on the same principle, but its main drawback is that it's too long. It is not convenient to use it as a public identifier, for example in the URL. In order to get the same number of combinations as the UUID, we need 2128 = 6421 lines 21 characters long, that is, almost 2 times shorter than the UUID (37 characters). And if we take an identifier of the same length as the UUID, then we get 6437 = 2222 against 2128 for the UUID.

The most important advantage of this approach is that you ourselves control the number of combinations by changing the length of the string and the character set. This will optimize the length of the identifier for your business requirements.

Collision
---------

[](#collision)

The probability of collision of identifiers can be calculated by the formula:

```
p(n) ≈ 1 - exp(N * (ln(N - 1) - ln(N - n)) + n * (ln(N - n) - ln(N) - 1) - (ln(N - 1) - ln(N) - 1))

```

Where

- *N* - number of possible options;
- *n* - number of generated keys.

Take an identifier with a length of 11 characters, like YouTube, which will give us *N* = 6411 = 266 and we will get:

- p(225) ≈ 7.62 \* 10-6
- p(230) ≈ 0.0077
- p(236) ≈ 0.9999

That is, by generating 236 = 68 719 476 736 identifiers you are almost guaranteed to get a collision.

*For calculations with large numbers, i recommend [this](https://web2.0calc.com/) online calculator.*

Installation
------------

[](#installation)

Pretty simple with [Composer](http://packagist.org), run:

```
composer require gpslab/base64uid
```

Usage
-----

[](#usage)

```
use GpsLab\Component\Base64UID\Base64UID;

$uid = Base64UID::generate(); // iKtwBpOH2E
```

With length 6 chars (646 = 68 719 476 736 combinations).

```
$uid = Base64UID::generate(6); // nWzfgA
```

The floating-length identifier will give more unique identifiers (648 + 649 + 6410 = 1 171 217 378 093 039 616 combinations).

```
$uid = Base64UID::generate(random_int(8, 10));
```

You can customize charset.

```
$charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/';
$uid = Base64UID::generate(11, $charset);

$charset = '0123456789abcdef';
$uid = Base64UID::generate(11, $charset);
```

### Other algorithms for generate UID

[](#other-algorithms-for-generate-uid)

#### Random char

[](#random-char)

Generate random characters of a finite UID from a charset.

```
$generator = new RandomCharGenerator();
$uid = $generator->generate(); // iKtwBpOH2E
```

Limit the length of the UID and the charset.

```
$charset = '0123456789abcdef';
$generator = new RandomCharGenerator(6, $charset);
$uid = $generator->generate(); // fa6c7d
```

#### Random bytes

[](#random-bytes)

Generate random bytes and encode it in Base64.

```
$generator = new RandomBytesGenerator();
$uid = $generator->generate(); // YCfGKBxd9k4
```

```
$generator = new RandomBytesGenerator(5);
$uid = $generator->generate(); // Mm7dpkM
```

#### Encoded random bits

[](#encoded-random-bits)

Generate bitmap with random bits and encode it in Base64. The bitmap length is 64 bits and it require 64-bit mode of processor architecture.

```
$binary_generator = new RandomBinaryGenerator(32);
$encoder = new HexToBase64BitmapEncoder();
$generator = new EncodeBitmapGenerator($binary_generator, $encoder);
$uid = $generator->generate(); // 7MWx2BuWJUw
```

#### Encoded bitmap of time

[](#encoded-bitmap-of-time)

Generate bitmap with current time in microseconds and encode it in Base64. The bitmap length is 64 bits and it require 64-bit mode of processor architecture.

```
$binary_generator = new TimeBinaryGenerator();
$encoder = new HexToBase64BitmapEncoder();
$generator = new EncodeBitmapGenerator($binary_generator, $encoder);
$uid = $generator->generate(); // koLfRhzAoI0
$uid = $generator->generate(); // zALfRhzAovg
$uid = $generator->generate(); // 18LfRhzAoQw
```

Generated bitmap has a structure:

```
{first bit}{random prefix}{current time}{random suffix}

```

- *first bit* - bitmap limiter for fixed size of bitmap;
- *prefix* - random bits used in prefix of bitmap. The length of the generated bits can be configured from `$prefix_length`;
- *time* - bits of current time in microseconds.
- *suffix* - random bits used in suffix of bitmap. The length is calculated from `64 - 1 - $prefix_length - $time_length`.

Responsibly select the number of bits allocated to store the current time. The `$time_length` defines the limit of the stored date:

Bits limitMaximum available bitmapUnix TimestampDate40-bits`1111111111111111111111111111111111111111``1099511627775`2004-11-03 19:53:48 (UTC)41-bits`11111111111111111111111111111111111111111``2199023255551`2039-09-07 15:47:36 (UTC)42-bits`111111111111111111111111111111111111111111``4398046511103`2109-05-15 07:35:11 (UTC)43-bits`1111111111111111111111111111111111111111111``8796093022207`2248-09-26 15:10:22 (UTC)44-bits`11111111111111111111111111111111111111111111``17592186044415`2527-06-23 06:20:44 (UTC)45-bits`111111111111111111111111111111111111111111111``35184372088831`3084-12-12 12:41:29 (UTC)To reduce the size of the saved time, you can use a `$time_offset` that allows you to move the starting point of time:

Offset microsecondsOffset dateMaximum available date for 41-bits01970-01-01 00:00:00 (UTC)2039-09-07 15:47:36 (UTC)15778368000002020-01-01 00:00:00 (UTC)2089-09-06 15:47:36 (UTC)#### Encoded bitmap of floating time

[](#encoded-bitmap-of-floating-time)

It is similar to the previous generator `TimeBinaryGenerator`, but the position with bits of the current time is floating. That is, the length of the prefix and suffix is randomly generated each time. Simultaneously generated identifiers have less similarity, but the likelihood of collision increases.

```
$binary_generator = new FloatingTimeGenerator();
$encoder = new HexToBase64BitmapEncoder();
$generator = new EncodeBitmapGenerator($binary_generator, $encoder);
$uid = $generator->generate(); // 5mqhb6MPH7g
$uid = $generator->generate(); // kFvow8joJys
$uid = $generator->generate(); // 8QRC30YeP3E
```

#### Snowflake-id

[](#snowflake-id)

Snowflake-id use time in microseconds and generator id. This allows you to customize the generator to your environment and reduce the likelihood of a collision, but the identifiers are very similar to each other and the identifier reveals the scheme of your internal infrastructure. Snowflake-id used in Twitter, Instagram, etc.

```
$generator_id = 0; // value 0-1023
$binary_generator = new SnowflakeGenerator($generator_id);
$encoder = new HexToBase64BitmapEncoder();
$generator = new EncodeBitmapGenerator($binary_generator, $encoder);
$uid = $generator->generate(); // gBFKQeuAAAA
$uid = $generator->generate(); // gBFKQeuAAAE
$uid = $generator->generate(); // gBFKQevAAAA
```

Domain-driven design (DDD)
--------------------------

[](#domain-driven-design-ddd)

How to usage in your [domain](https://en.wikipedia.org/wiki/Domain-driven_design).

For example create a `ArticleId` ValueObject:

```
class ArticleId
{
    private $id;

    public function __construct(string $id)
    {
        $this->id = $id;
    }

    public function id()
    {
        return $this->id;
    }
}
```

Repository interface for Article:

```
interface ArticleRepository
{
    public function nextId();

    // more methods ...
}
```

Concrete repository for Article:

```
use GpsLab\Component\Base64UID\Base64UID;

class ConcreteArticleRepository implements ArticleRepository
{
    public function nextId()
    {
        return new ArticleId(Base64UID::generate());
    }

    // more methods ...
}
```

Now we can create a new entity with `ArticleId`:

```
$article = new Article(
    $repository->nextId(),
    // more article parameters ...
);
```

License
-------

[](#license)

This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 98.9% 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 ~234 days

Total

5

Last Release

2316d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9a6415c83577efe7b70d9ae4a3bb12958adc11c16e530ff844ff217b0fd0c54a?d=identicon)[Peter Gribanov](/maintainers/Peter%20Gribanov)

---

Top Contributors

[![peter-gribanov](https://avatars.githubusercontent.com/u/1954436?v=4)](https://github.com/peter-gribanov "peter-gribanov (88 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

phpuidUID

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gpslab-base64uid/health.svg)

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

###  Alternatives

[symfony/uid

Provides an object-oriented API to generate and represent UIDs

610280.0M754](/packages/symfony-uid)[hidehalo/nanoid-php

A copy of nanoid in PHP

8124.1M41](/packages/hidehalo-nanoid-php)[paragonie/random-lib

A Library For Generating Secure Random Numbers

703.3M26](/packages/paragonie-random-lib)[jasonhinkle/php-gpg

GPG / PGP port written in pure PHP with no binary dependencies

116147.2k1](/packages/jasonhinkle-php-gpg)[wyrihaximus/react-child-process-messenger

Messenger decorator for react/child-process

32279.4k4](/packages/wyrihaximus-react-child-process-messenger)[identifier/identifier

Common Interfaces and Factories for Identifiers

3226.2k1](/packages/identifier-identifier)

PHPackages © 2026

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