Class UuidV3Generator

java.lang.Object
cloud.opencode.base.id.uuid.UuidV3Generator

public final class UuidV3Generator extends Object
UUID v3 Generator - Name-Based UUID using MD5 UUID v3生成器 - 基于名称的MD5 UUID

Generates deterministic UUIDs based on RFC 4122 version 3. The same namespace + name pair always produces the same UUID. Uses MD5 hashing of the namespace bytes concatenated with the name bytes.

基于RFC 4122版本3生成确定性UUID。相同的命名空间+名称组合始终产生相同的UUID。 使用命名空间字节与名称字节拼接后的MD5哈希值。

UUID v3 Structure | UUID v3结构:

Input  = bytes(namespace_uuid) || bytes(name, UTF-8)
Hash   = MD5(Input)            [16 bytes]
hash[6] = (hash[6] & 0x0F) | 0x30  → version = 3
hash[8] = (hash[8] & 0x3F) | 0x80  → variant = 10xx
UUID   = UUID(msb=hash[0..7], lsb=hash[8..15])

Standard Namespaces | 标准命名空间 (RFC 4122):

Usage Examples | 使用示例:

UuidV3Generator gen = UuidV3Generator.create(UuidV3Generator.NAMESPACE_DNS);

// Deterministic: same input → same UUID
UUID uuid1 = gen.generate("example.com");
UUID uuid2 = gen.generate("example.com");
assert uuid1.equals(uuid2);

// Different names → different UUIDs
UUID a = gen.generate("foo.com");
UUID b = gen.generate("bar.com");
assert !a.equals(b);

Security | 安全性:

  • Thread-safe: Yes (stateless after construction) - 线程安全: 是(构造后无状态)
  • Deterministic: Yes - 确定性: 是
  • Note: MD5 is not cryptographically secure; use UUID v5 (SHA-1) for security-sensitive cases. 注意: MD5并非密码学安全,安全敏感场景请使用UUID v5(SHA-1)。
Since:
JDK 25, opencode-base-id V1.0.4
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final UUID
    DNS namespace UUID as defined in RFC 4122 | RFC 4122定义的DNS命名空间UUID
    static final UUID
    OID namespace UUID as defined in RFC 4122 | RFC 4122定义的OID命名空间UUID
    static final UUID
    URL namespace UUID as defined in RFC 4122 | RFC 4122定义的URL命名空间UUID
    static final UUID
    X.500 namespace UUID as defined in RFC 4122 | RFC 4122定义的X.500命名空间UUID
  • Method Summary

    Modifier and Type
    Method
    Description
    create(UUID namespace)
    Creates a UUID v3 generator for the given namespace 为给定命名空间创建UUID v3生成器
    Generates a deterministic UUID v3 for the given name 为给定名称生成确定性UUID v3
    Generates a simple UUID v3 string (no hyphens) for the given name 为给定名称生成简化UUID v3字符串(无连字符)
    Generates a UUID v3 string for the given name 为给定名称生成UUID v3字符串
    Returns the namespace used by this generator 返回此生成器使用的命名空间
    Returns the generator type name 返回生成器类型名称

    Methods inherited from class Object

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

    • NAMESPACE_DNS

      public static final UUID NAMESPACE_DNS
      DNS namespace UUID as defined in RFC 4122 | RFC 4122定义的DNS命名空间UUID
    • NAMESPACE_URL

      public static final UUID NAMESPACE_URL
      URL namespace UUID as defined in RFC 4122 | RFC 4122定义的URL命名空间UUID
    • NAMESPACE_OID

      public static final UUID NAMESPACE_OID
      OID namespace UUID as defined in RFC 4122 | RFC 4122定义的OID命名空间UUID
    • NAMESPACE_X500

      public static final UUID NAMESPACE_X500
      X.500 namespace UUID as defined in RFC 4122 | RFC 4122定义的X.500命名空间UUID
  • Method Details

    • create

      public static UuidV3Generator create(UUID namespace)
      Creates a UUID v3 generator for the given namespace 为给定命名空间创建UUID v3生成器
      Parameters:
      namespace - the namespace UUID | 命名空间UUID
      Returns:
      generator | 生成器
      Throws:
      IllegalArgumentException - if namespace is null | 命名空间为null时抛出
    • generate

      public UUID generate(String name)
      Generates a deterministic UUID v3 for the given name 为给定名称生成确定性UUID v3

      Examples | 示例:

      create(NAMESPACE_DNS).generate("python.org") → 6fa459ea-ee8a-3ca4-894e-db77e160355e
      create(NAMESPACE_DNS).generate("example.com") → always the same UUID
      

      Performance | 性能:

      Time: O(n) where n = name length, Space: O(n)

      Parameters:
      name - the name to hash | 要哈希的名称
      Returns:
      deterministic UUID v3 | 确定性UUID v3
      Throws:
      IllegalArgumentException - if name is null | 名称为null时抛出
    • generateStr

      public String generateStr(String name)
      Generates a UUID v3 string for the given name 为给定名称生成UUID v3字符串
      Parameters:
      name - the name | 名称
      Returns:
      UUID string | UUID字符串
    • generateSimple

      public String generateSimple(String name)
      Generates a simple UUID v3 string (no hyphens) for the given name 为给定名称生成简化UUID v3字符串(无连字符)
      Parameters:
      name - the name | 名称
      Returns:
      32-character UUID string | 32字符UUID字符串
    • getNamespace

      public UUID getNamespace()
      Returns the namespace used by this generator 返回此生成器使用的命名空间
      Returns:
      namespace UUID | 命名空间UUID
    • getType

      public String getType()
      Returns the generator type name 返回生成器类型名称
      Returns:
      type name | 类型名称