loading...
Cover image for Which UUID version?

Which UUID version?

aakhtar3 profile image aakhtar3 ใƒป2 min read

Which UUID version?

Universally Unique IDentifiers versions are defined by the RFC4122.

They are 5 different versions that have different implementations to generate Unique Identifiers.

v1 Time

Uses the CPU clock cycle to calculate a unique ID

v2 MAC

Uses the CPU clock cycle and MAC address to generate a unique ID.

v3 MD5

Uses a namespace value to salt the MD5 Hash (128 bits).

v4 Random

Uses Pseudo-random numbers to generate a unique ID.

v5 SHA-1

Uses a namespace value to salt the SHA-1 Hash (160 bits).

API

Generally, a UUID library will expose all 5 versions and a few helper functions.

  • Version: Provides UUID RFC version
  • Parse: Convert UUID string to array of bytes
  • Stringify: Convert array of bytes to UUID string
  • Validate: Check if UUID is valid
  • Nil: All bits are set to 0
    • 00000000-0000-0000-0000-000000000000

Ranking

Uniqueness

v1 ๐Ÿ‘Š v2 ๐Ÿ‘Š v4

v4 Random

v1 and v2 are similar, but the cpu cycle is predictable.

Hashing

v3 ๐Ÿ‘Š v5

v5 SHA-1

v5 SHA-1 has a stronger encryption than v3 MD5.

Protected

v2 ๐Ÿ‘Š v1 ๐Ÿ‘Š v4 ๐Ÿ‘Š v3 ๐Ÿ‘Š v5

v5 SHA-1

v1 and v2 are similar, but the MAC address can make your system less secure.

Overall

v1 ๐Ÿ‘Š v2 ๐Ÿ‘Š v3 ๐Ÿ‘Š v5 ๐Ÿ‘Š v4

v4 Random

v4 is generally the best option in most use cases.

Discussion

pic
Editor guide