1

How to namespace a UUID such that I could later check if it was generated using the namespace?

In particular, I was considering using UUID v5. However, from what I can tell, there is no way to check if a particular UUID was generated using a particular namespace, without also knowing the name.

I've considered simply generating UUID v4 and replacing the first 4 characters with constant values, e.g. 0001 or 0002 and later checking if UUID uses that prefix. I realize that this will reduce the cardinality of the UUID. However, that would be true for any namespace solution. My bigger concern is that this will make it invalid v4 UUID.

1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented May 22, 2023 at 0:17

1 Answer 1

1

However, from what I can tell, there is no way to check if a particular UUID was generated using a particular namespace, without also knowing the name.

Correct. The namespace in a v5 UUID is hashed together with the key, and there is no way to reverse it.

I've considered simply generating UUID v4 and replacing the first 4 characters with constant values

Sure. This is a pretty common approach, though usually I see folks use more bytes for their prefix than 2. Most folks I see use a several-byte constant-but-random prefix at the start, followed by their internal namespace, followed by a random id. But it doesn't really matter. As long as your UUIDs don't intermix with other systems' UUIDs, you don't need to worry about collisions in the prefix section. And you definitely don't have to worry about collisions elsewhere as long as enough of the bytes are still random.

There are only 6-8 bits in a v4 UUID that are defined and they're not at the beginning. It's the 13th and varying parts of the 17th digit in the hex encoding. So I probably would avoid messing with those two digits, but I've never encountered a system that actually validated the version information. If you just generated a random 128 bits, it's going to work fine in every system I've seen. But I'm a stickler for formats, so I personally always set the bits correctly, mostly because it makes me happy, not because consuming systems actually care. Your scheme is fine, as long as you're not intermixing with other systems where other people may have picked the same namespace identifier.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Greatly appreciate detail response

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.