PHPackages                             adambean/randflake-id-php - 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. adambean/randflake-id-php

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

adambean/randflake-id-php
=========================

A distributed, uniform, unpredictable, unique random ID generator: PHP implementation of Randflake ID and Sparx 64-bit.

1.0.0(1mo ago)76MITPHPPHP ^8.1

Since May 27Pushed 1mo agoCompare

[ Source](https://github.com/Adambean/randflake-id-php)[ Packagist](https://packagist.org/packages/adambean/randflake-id-php)[ RSS](/packages/adambean-randflake-id-php/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Randflake ID
============

[](#randflake-id)

A distributed, uniform, unpredictable, unique random ID generator:

This is a PHP port based on lemon-mint at GoSuda's specification, primarily based on the TypeScript implementation:

This repository provides a library to:

- Generate a secret key.
- Generate IDs.
- Encrypt and decrypt existing IDs.
- Encode and decode existing IDs to and from Base32Hex.
- Inspect existing IDs to retrieve their timestamp, node ID, and sequence.

Includes a port of a library for the [SPARX](https://www.research.ed.ac.uk/en/publications/sparx-a-family-of-arx-based-lightweight-block-ciphers-provably-se/) 64-bit block cipher, used to encrypt raw ID numbers into unpredictable ID numbers.

You may not need this library directly in your project:

- For a [Symfony](https://symfony.com/) project with Doctrine ORM/DBAL integration, see my [Symfony bundle](https://github.com/adambean/randflake-id-bundle).

Is Randflake ID for my project?
-------------------------------

[](#is-randflake-id-for-my-project)

That's for you to decide. A short description of it:

> Inspired by Snowflake ID, works well in a distributed system, unpredictable, unique random ID generator, timestamp resolution of 1000ms, works on more distributed machines 131072 (2^17) than snowflake's 1024 (2^10).

There is a good comparison of ideas for unique IDs here:

Randflake ID seems to strike a good balance of a large ID pool, wide distribution, encryption, unpredictability, naturally lexicographic, cursor-based pagination, with 34 years of life.

Requirements
------------

[](#requirements)

- PHP† 8.1 or later
- Composer

† **This library requires a 64-bit build of PHP to function.** Check your `PHP_INT_SIZE`, which **must** be at least `8`:

```
php -r 'echo PHP_INT_SIZE;'
```

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

[](#installation)

Add a Composer package as is usual for PHP projects:

```
composer require adambean/randflake-id-php
```

Usage
-----

[](#usage)

First, generate yourself a 16-byte secret string if you don't have one yet:

```
