Search code examples
uuid

Does name based UUID satisfy those conditions?


For some reason, I have to make UUIDs that are same when same strings are input like below.

UUID.nameUUIDFromBytes(str.getBytes());

What I want to know is this new UUID satisfy below conditions.

  • As far as input strs are same, I can get the same generated UUIDs.
  • As far as input strs are different, the generated UUIDs must be different.
  • Is there any chance that randomly created UUIDs that are created by Time-based UUID can be duplicate with this new UUID.

I checked those in the UUID docs but I want a more confirmation as deployment without those conditions can make our system disaster.


Solution

  • "As far as str is same, I can get the same UUID"

    Yes, as defined in the RFC 4122 - 4.3. Algorithm for Creating a Name-Based UUID:

    The requirements for these types of UUIDs are as follows:

    • The UUIDs generated at different times from the same name in the same namespace MUST be equal.

    "As far as input strs are different, the UUIDs must be different."

    Yes (and no), as defined in the RFC 4122 - 4.3. Algorithm for Creating a Name-Based UUID:

    [...]

    The UUIDs generated from two different names in the same namespace should be different (with very high probability).

    Practically, different byte arrays results in different UUIDs. But there have been MD5 collisions, therefore the mentioned "with very high probability".

    "Is there any chance that randomly created UUIDs that are created by Time-based UUID can be duplicate with this new UUID."

    No, the name-based UUID have the version number set to 3 (or 5), the time based UUID have the version set to 1.