Class UUID5Helper

java.lang.Object
com.helger.base.uuid.UUID5Helper

@Immutable public final class UUID5Helper extends Object
This class contains static methods that leverage UUID and MessageDigest to create version-5 UUIDs with full namespace support.

The UUID class provided by java.util is suitable as a datatype for UUIDs of any version, but lacks methods for creating version 5 (SHA-1 based) UUIDs. Its implementation of version 3 (MD5 based) UUIDs also lacks build-in namespace support.

This class was inspired by https://github.com/rootsdev/polygenea/blob/master/java/src/org/rootsdev/polygenea/UUID5.java from Luther Tychonievich

Since:
v12.1.3
Author:
Philip Helger
  • Method Summary

    Modifier and Type
    Method
    Description
    static @NonNull UUID
    fromBytes(byte @NonNull [] aNameBytes)
    Similar to UUID.nameUUIDFromBytes, but does version 5 (sha-1) not version 3 (md5)
    static @NonNull UUID
    fromBytes(@Nullable UUID aNamespace, byte @NonNull [] aNameBytes)
    Similar to UUID.nameUUIDFromBytes, but does version 5 (sha-1) not version 3 (md5) and uses a namespace
    static @NonNull UUID
    fromUTF8(@NonNull String sName)
    Similar to UUID.nameUUIDFromBytes, but does version 5 (sha-1) not version 3 (md5)
    static @NonNull UUID
    fromUTF8(@Nullable UUID aNamespace, @NonNull String sName)
    Similar to UUID.nameUUIDFromBytes, but does version 5 (sha-1) not version 3 (md5) and uses a namespace

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • fromBytes

      public static @NonNull UUID fromBytes(byte @NonNull [] aNameBytes)
      Similar to UUID.nameUUIDFromBytes, but does version 5 (sha-1) not version 3 (md5)
      Parameters:
      aNameBytes - The bytes to use as the "name" of this hash
      Returns:
      the UUID object and never null.
    • fromBytes

      public static @NonNull UUID fromBytes(@Nullable UUID aNamespace, byte @NonNull [] aNameBytes)
      Similar to UUID.nameUUIDFromBytes, but does version 5 (sha-1) not version 3 (md5) and uses a namespace
      Parameters:
      aNamespace - The namespace to use for this UUID. If null, uses 00000000-0000-0000-0000-000000000000
      aNameBytes - The bytes to use as the "name" of this hash. May not be null.
      Returns:
      the UUID object and never null.
    • fromUTF8

      public static @NonNull UUID fromUTF8(@NonNull String sName)
      Similar to UUID.nameUUIDFromBytes, but does version 5 (sha-1) not version 3 (md5)
      Parameters:
      sName - The string to be encoded in utf-8 to get the bytes to hash. May not be null.
      Returns:
      the UUID object
    • fromUTF8

      public static @NonNull UUID fromUTF8(@Nullable UUID aNamespace, @NonNull String sName)
      Similar to UUID.nameUUIDFromBytes, but does version 5 (sha-1) not version 3 (md5) and uses a namespace
      Parameters:
      aNamespace - The namespace to use for this UUID. If null, uses 00000000-0000-0000-0000-000000000000
      sName - The string to be encoded in utf-8 to get the bytes to hash. May not be null.
      Returns:
      the UUID object