ofilin/base64uid

Class for generating UID

0.2 2019-06-27 12:20 UTC

This package is auto-updated.

Last update: 2024-05-27 23:51:05 UTC


README

Generate UID like YouTube.

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 a lot of combinations.

64^10 = 2^60 = 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 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.

Due to the fact that Base64 UID uses 64 chars instead of 36, the identifier turns out to be noticeably shorter. Also you have the opportunity to manage the long identifier and the number of possible values. This will optimize the length of the identifier for your business requirements.

Installation

Pretty simple with Composer, run:

composer require ofilin/base64uid

Usage

use ofilin\base64uid\Base64UID;

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

With length 6 chars

// 64^6 = 68 719 476 736 (combinations)
$uid = Base64UID::generate(6); // nWzfgA

The floating-length identifier will give more unique identifiers.

// 64^10 + 64^9 + 64^8 = 1 171 217 378 093 039 616 (combinations)
$uid = Base64UID::generate(random_int(8, 10));

License

This bundle is under the MIT license. See the complete license in the file: LICENSE