Generating deterministic UUIDs from arbitrary strings with Symfony
Tags: symfony, php, uid, uuid
Published: 2024-06-26 09:26:00

---
title: Generating deterministic UUIDs from arbitrary strings with Symfony
published: true
description: 
tags: symfony, php, uid, uuid
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0vd7sumabegmqhmcg66u.jpg
# Use a ratio of 100:42 for best results.
published_at: 2024-06-26 09:26 +0000
---

[UUIDs](https://en.wikipedia.org/wiki/Universally_unique_identifier) are 128-bit numbers used to identify items uniquely. You've probably seen or used UUIDs in your applications before. They are usually represented as an hexadecimal value with the format 8-4-4-4-12 (e.g. `6ba7b814-9dad-11d1-80b4-00c04fd430c8`).

Most developers use random UUIDs, which don't contain any information about where or when they were generated. These are technically called UUIDv4 and they are one of the eight different types of UUIDs available.

Symfony provides the [UID component](https://symfony.com/uid) to generate UUIDs:

```php
use Symfony\Component\Uid\Uuid;

// $uuid is an instance of Symfony\Component\Uid\UuidV4
// and its value is a random UUID
$uuid = Uuid::v4();
```

In this article, we'll focus on UUIDv5, which generates UUIDs based on a name and a namespace. Before diving into it, let's explain the problem they solve.

Imagine that you work on an e-commerce application and need to include the product ID in some URLs (e.g. `/show/{productId}/{productSlug}`). The product IDs are unique within your application, but it's internal information and you are not comfortable sharing it publicly.

If the product IDs are numbers, then you can use tools like [Sqids](https://sqids.org/php) (formerly known as Hashids) to generate deterministic (and reversible) unique IDs from a given number(s). However, this problem can also be solved with UUIDs.

UUIDv5 generate UUIDs whose contents are based on a given `name` (any arbitrary string) and a `namespace`. The `namespace` is used to ensure that all the names that belong to it are unique within that namespace. The spec defines a few standard namespaces (for generating UUIDs based on URLs, DNS entries, etc.) but you can use any other UUID (e.g. a random UUID) as the namespace:

```php
use Symfony\Component\Uid\Uuid;
// ...

final readonly class ProductHandler
{
    // this value was generated randomly using Uuid::v4()
    private const string UUID_NAMESPACE = '8be5ecb9-1eba-4927-b4d9-73eaa98f8b65';

    // ...

    public function setUuid(Product $product): void
    {
        $namespace = Uuid::fromString(self::UUID_NAMESPACE);
        $product->setUuid(Uuid::v5($namespace, $this->getProductId());
        // e.g. if the productId is 'acme-1234',
        // the generated UUID is 'eacc432f-a7c1-5750-8f9f-9d69cb287987'
    }
}
```

UUIDv5 roughly performs `sha1($namespace.$name)` when generating values. This way, the generated UUIDs are not reversible (or guessable by external actors) and are deterministic (they always generate the same UUID for a given string).

If you want to make the public IDs shorter, use any of the methods provided by Symfony to [convert UUIDs](https://symfony.com/doc/current/components/uid.html#converting-uuids):

```php
$namespaceAsString = '8be5ecb9-1eba-4927-b4d9-73eaa98f8b65';
$namespace = Uuid::fromString($namespaceAsString);

$name = 'acme-1234';
$uuid = Uuid::v5($namespace, $name);
// (string) $uuid = 'eacc432f-a7c1-5750-8f9f-9d69cb287987'

$shortUuid = $uuid->toBase58();
// $shortUuid = 'VzeJE1ydqWXpJJMwnavj3t'
```

The UUID spec defines many other types of UUIDs which fit different scenarios. There's even a UUIDv3 which is the same as UUIDv5 but uses `md5` hashes instead of `sha1`. That's why UUIDv5 is preferred over UUIDv3.

Check out the [Symfony docs about the different types of UUIDs](https://symfony.com/doc/current/components/uid.html#generating-uuids) to know more about them.