PHPackages                             charm/uuid - 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. charm/uuid

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

charm/uuid
==========

A very fast and dependency free library to generate UUIDs (GUIDs) version 1 or 4, sortable UUIDs that validates, or 64 bit unique identifiers according to Twitters' Snowflake, Sonyflake or Instaflake algorithms.

0.0.6(4y ago)1226.2k↓32.1%2[1 issues](https://github.com/charm-php/uuid/issues)MITPHP

Since Aug 2Pushed 2y ago1 watchersCompare

[ Source](https://github.com/charm-php/uuid)[ Packagist](https://packagist.org/packages/charm/uuid)[ RSS](/packages/charm-uuid/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

A very fast UUID and snowflake generator
========================================

[](#a-very-fast-uuid-and-snowflake-generator)

After looking at other libraries, they seem very complex for the most common use case: generating UUIDs. Now, UUIDs are quite complex in themselves, and it is easy to make mistakes so that the UUID is not truly unique. That's why I feel the library should be as simple as possible, so that it is easy to review.

Features:

- UUID v4 using cryptographically strong randomness
- UUID v1
- COMB UUID v4 - All new UUIDs will be larger than the previous. The UUID is built from a 60 bit timestamp, a 14 bit sequence number and a 48 bit machine id. In this implementation the UUID will validate as a proper UUID v4, which means that any picky third party software which validates the UUID will treat the UUID as a compliant version 4 UUID.
- Snowflake UID - 64 bit increasing ID suitable for database primary keys
- Instaflake UID - a variation of the Snowflake UID
- Sonyflake UID - another variation of Snowflake

This library provides two classes and has no dependencies and performs very well. The UUID v4 is cryptographic random (via `random_bytes()`), while UUID v1 tries to find the MAC address or another unique machine ID. To reduce the risk of collisions, the process id is used to to reduce the chance of a single machine generating two identical IDs.

Benchmarks
----------

[](#benchmarks)

### First UUID is generated almost as quickly as the native PECL extension

[](#first-uuid-is-generated-almost-as-quickly-as-the-native-pecl-extension)

[![](https://camo.githubusercontent.com/83d869d7d05aa3646a545dbc047b192c0167ceec0e517c73390e154c7b348f6b/68747470733a2f2f717569636b63686172742e696f2f63686172742f72656e6465722f7a6d2d62613238343436342d316261342d343739352d613765382d6463663866303064316537313f663d2e706e67 "Comparison of bootstrap times")](https://camo.githubusercontent.com/83d869d7d05aa3646a545dbc047b192c0167ceec0e517c73390e154c7b348f6b/68747470733a2f2f717569636b63686172742e696f2f63686172742f72656e6465722f7a6d2d62613238343436342d316261342d343739352d613765382d6463663866303064316537313f663d2e706e67)

\[Edit\]() ### Faster then the native PECL extension for many UUIDs.

[](#faster-then-the-native-pecl-extension-for-many-uuids)

[![](https://camo.githubusercontent.com/c4f5a63efacbf93035560760017f23bb7eb4acc35df87c307b9eb967c44b60c5/68747470733a2f2f717569636b63686172742e696f2f63686172742f72656e6465722f7a6d2d34323136306239372d346133312d346131372d623539312d3938363736613638386230393f663d2e706e67 "Generating 100k UUIDs")](https://camo.githubusercontent.com/c4f5a63efacbf93035560760017f23bb7eb4acc35df87c307b9eb967c44b60c5/68747470733a2f2f717569636b63686172742e696f2f63686172742f72656e6465722f7a6d2d34323136306239372d346133312d346131372d623539312d3938363736613638386230393f663d2e706e67)

\[Edit\]() ### Summary

[](#summary)

See the `BENCHMARKS.md` file for full details. The noteworthy details are:

1. Less then 1 ms to generate the first UUID, *10.5 milliseconds* faster than `ramsey/uuid` for UUID v1 and 3.9 ms faster for UUID v4.
2. Overhead is related to loading of the source files.

LibraryAvg time first UUID 1Avg. time first UUID 4100 000 UUID 1100 000 UUID 4charm/uuid0.75 ms0.50 ms\*\*479.41 ms \*\*\*\*495.17 ms \*\*ramsey/uuid10.05 ms4.34 ms1,944.46 ms779.63 msNATIVE pecl:uuid extension**0.51 ms****0.16 ms**528.75 ms1,091.64 msAre these UUIDs truly unique?
-----------------------------

[](#are-these-uuids-truly-unique)

### UUID(4)

[](#uuid4)

The random UUID(4) generated by this library are industrial strength, directly using the entropy from the operating system via the `random_bytes()` function.

### UUID(1)

[](#uuid1)

The time and place based UUID(1) algorithm was implemented while taking into account bug reports and experiences made in other libraries. Subtle issues that can arise is related to forking of the process and The algorithm for building the bytes is an exact port of the C code provided in [appendix A of the RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122#appendix-A).

However, the `microtime(true)` function in PHP does not provide an accurate enough time resolution,

A lot of effort was put into defining the UUID standard, and a they are considered to be unique for all practical purposes.

UUID v4 is the simplest to use, but they require 16 bytes of storage each. They need no configuration, and they can be generated very quickly. If all companies in the world generate a total of 1 billion UIDs every second, the first collision is expected to happen after about 85 years. After those 85 years, most of these UUIDs will have vanished anyway.

Snowflake ID are only 8 bytes (64 bits) and can safely be be used inside your own databases. They were invented by Twitter and is used to generate unique IDs for tweets, users, things, images and so on. To ensure uniqueness, you may configure a unique machineId, but the library will automatically pick an available machine id from the operating system if you don't.

UUID v1 is a "coordinate" using the clock and a unique machine id derived from the mac address or another unique 48 bit machine identifier. They are also automatically configured in this library.

Warning
-------

[](#warning)

Please run some tests in your environment before using this library. Particularly, ensure that the server has enough entropy available if you're using UUID version 4. Entropy is random numbers generated from network noise and other sources.

UUIDs version 4 are considered production ready, but the other IDs need some more scrutiny before we can declare them production ready. Please let me know if you spot any problems.

Quick Start
-----------

[](#quick-start)

If you're not using a service container, the quickest way to begin is by using the function `Charm\Id::make()`. The returned value will be a cryptographically random 128 bit value in UUID v4 form (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX).

### Installation

[](#installation)

As with most PHP packages, install `composer` and run

```
# composer require charm/uuid
```

Alternatively you can of course clone the repository from and include the `./autoload.php` file we've provided.

### UUID v4

[](#uuid-v4)

This is the fastest and easiest to use, as it has no configuration options. The probability of a collission is microscopic. The number of different combinations is approximately 5,3169e+36.

*If collisions happen, it is because of bugs in the generator. The ramsay If you've heard that collisions happen, you can be quite certain that the collision happened because of using an inferior random number generator.*

Quotes from around the web:

| If you generate 103 trillion UUID v4, the probability of a collision is about 1 in a billion.

| If you have a database of 1 petabyte of UUIDs, the probability of a single collision is | one in 50 billion.

| If you generate 8 billion UUIDs per second,

```
