UUID Versions

Resources  |  UUID Versions

UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify objects or entities on the internet. UUIDs have different versions, each with specific methods of generation and intended use cases. Here's a breakdown of the various UUID versions:

UUID Version 1 (UUIDv1)

  • Based on: Time and node (usually the MAC address of the generating device).
  • Structure: Combines the current timestamp, a clock sequence, and the node identifier (MAC address).
  • Use case: Situations where uniqueness over time is crucial.
  • Example: 6ba7b810-9dad-11d1-80b4-00c04fd430c8

UUID Version 2 (UUIDv2)

  • Based on: POSIX UID/GID and timestamp.
  • Structure: Similar to UUIDv1 but includes a POSIX UID/GID in place of some of the node and timestamp bits.
  • Use case: Intended for DCE (Distributed Computing Environment) security and identification purposes.
  • Example: Less commonly used and not widely supported.

UUID Version 3 (UUIDv3)

  • Based on: Names and MD5 hashing.
  • Structure: Uses a namespace UUID and a name, hashed together using MD5.
  • Use case: When you need to generate a UUID from a "name" within a certain namespace, ensuring the same name always yields the same UUID.
  • Example: 3b2e70c0-68a3-11eb-9439-0242ac130002

UUID Version 4 (UUIDv4)

  • Based on: Random numbers.
  • Structure: Mostly random data, with certain bits set for version and variant.
  • Use case: General-purpose UUID generation where random uniqueness is sufficient.
  • Example: f47ac10b-58cc-4372-a567-0e02b2c3d479

UUID Version 5 (UUIDv5)

  • Based on: Names and SHA-1 hashing.
  • Structure: Similar to UUIDv3 but uses SHA-1 instead of MD5.
  • Use case: Like UUIDv3, but when a more secure hashing algorithm is preferred.
  • Example: 3b2e70c0-68a3-11eb-9439-0242ac130002

UUID Version 6 (UUIDv6)

  • Based on: Modified version of UUIDv1 with reordered fields for better sortability.
  • Structure: Timestamp first, followed by the clock sequence and node identifier.
  • Use case: When a UUID needs to be sortable by creation time.
  • Example: Not yet widely implemented or standardized.

UUID Version 7 (UUIDv7)

  • Based on: Time-based and lexicographically sortable.
  • Structure: Current timestamp followed by random data.
  • Use case: Situations requiring time-based ordering and uniqueness.
  • Example: Not yet standardized but proposed for future use.

UUID Version 8 (UUIDv8)

  • Based on: Customizable for specific needs.
  • Structure: Allows embedding arbitrary data within the UUID format.
  • Use case: Custom implementations where specific requirements dictate the structure of the UUID.
  • Example: Not yet standardized, open for custom implementations.

Example Code Snippets for Generating Different UUID Versions

Python

import uuid

# UUIDv1
uuid_v1 = uuid.uuid1()
print("UUIDv1:", uuid_v1)

# UUIDv3
namespace = uuid.NAMESPACE_DNS
name = "example.com"
uuid_v3 = uuid.uuid3(namespace, name)
print("UUIDv3:", uuid_v3)

# UUIDv4
uuid_v4 = uuid.uuid4()
print("UUIDv4:", uuid_v4)

# UUIDv5
uuid_v5 = uuid.uuid5(namespace, name)
print("UUIDv5:", uuid_v5)

JavaScript (Node.js)

const { v1: uuidv1, v3: uuidv3, v4: uuidv4, v5: uuidv5 } = require('uuid');

// UUIDv1
console.log("UUIDv1:", uuidv1());

// UUIDv3
const NAMESPACE_DNS = uuidv3.DNS;
console.log("UUIDv3:", uuidv3('example.com', NAMESPACE_DNS));

// UUIDv4
console.log("UUIDv4:", uuidv4());

// UUIDv5
console.log("UUIDv5:", uuidv5('example.com', NAMESPACE_DNS));

Java

import java.util.UUID;

public class UUIDExample {
    public static void main(String[] args) {
        // UUIDv1
        UUID uuid_v1 = UUID.randomUUID();
        System.out.println("UUIDv1: " + uuid_v1);

        // UUIDv3
        UUID namespace = UUID.fromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
        UUID uuid_v3 = UUID.nameUUIDFromBytes("example.com".getBytes());
        System.out.println("UUIDv3: " + uuid_v3);

        // UUIDv4
        UUID uuid_v4 = UUID.randomUUID();
        System.out.println("UUIDv4: " + uuid_v4);

        // UUIDv5
        UUID uuid_v5 = UUID.nameUUIDFromBytes("example.com".getBytes());
        System.out.println("UUIDv5: " + uuid_v5);
    }
}

Conclusion

UUIDs provide a standardized way to generate unique identifiers across different systems and use cases. Each version of UUID has its specific methods of generation and intended use cases, allowing developers to choose the most appropriate version for their needs. Understanding the differences between these versions helps in selecting the right one to ensure uniqueness, security, and performance in your applications.