UUID

UUID generator and utilities for Elixir. See RFC 4122.

Summary

Functions

Convert binary UUID data to a string

Inspect a UUID and return information about its 128-bit binary content, type, version and variant

Convert a UUID string to its binary data equivalent

Generate a new UUID v1. This version uses a combination of one or more of: unix epoch, random bytes, pid hash, and hardware address

Generate a new UUID v1, with an existing clock sequence and node address. This version uses a combination of one or more of: unix epoch, random bytes, pid hash, and hardware address

Generate a new UUID v3. This version uses an MD5 hash of fixed value (chosen based on a namespace atom - see Appendix C of RFC 4122 and a name value. Can also be given an existing UUID String instead of a namespace atom

Generate a new UUID v4. This version uses pseudo-random bytes generated by the crypto module

Generate a new UUID v5. This version uses an SHA1 hash of fixed value (chosen based on a namespace atom - see Appendix C of RFC 4122 and a name value. Can also be given an existing UUID String instead of a namespace atom

Functions

binary_to_string!(uuid, format \\ :default)

Convert binary UUID data to a string.

Will raise an ArgumentError if the given binary is not valid UUID data, or the format argument is not one of: :default, :hex, or :urn.

Examples

iex> UUID.binary_to_string!(<<135, 13, 248, 232, 49, 7, 68, 135,
...>        131, 22, 129, 224, 137, 184, 194, 207>>)
"870df8e8-3107-4487-8316-81e089b8c2cf"

iex> UUID.binary_to_string!(<<142, 161, 81, 61, 248, 161, 77, 234, 155,
...>        234, 107, 143, 75, 91, 110, 115>>, :hex)
"8ea1513df8a14dea9bea6b8f4b5b6e73"

iex> UUID.binary_to_string!(<<239, 27, 26, 40, 238, 52, 17, 227, 136,
...>        19, 20, 16, 159, 241, 163, 4>>, :urn)
"urn:uuid:ef1b1a28-ee34-11e3-8813-14109ff1a304"
info!(uuid_string)

Inspect a UUID and return information about its 128-bit binary content, type, version and variant.

Timestamp portion is not checked to see if it’s in the future, and therefore not yet assignable. See “Validation mechanism” in section 3 of RFC 4122.

Will raise an ArgumentError if the given string is not a UUID representation in a format like:

  • "870df8e8-3107-4487-8316-81e089b8c2cf"
  • "8ea1513df8a14dea9bea6b8f4b5b6e73"
  • "urn:uuid:ef1b1a28-ee34-11e3-8813-14109ff1a304"

Examples

iex> UUID.info!("870df8e8-3107-4487-8316-81e089b8c2cf")
[uuid: "870df8e8-3107-4487-8316-81e089b8c2cf",
 binary: <<135, 13, 248, 232, 49, 7, 68, 135, 131, 22, 129, 224, 137, 184, 194, 207>>,
 type: :default,
 version: 4,
 variant: :rfc4122]

iex> UUID.info!("8ea1513df8a14dea9bea6b8f4b5b6e73")
[uuid: "8ea1513df8a14dea9bea6b8f4b5b6e73",
 binary: <<142, 161, 81, 61, 248, 161, 77, 234, 155,
            234, 107, 143, 75, 91, 110, 115>>,
 type: :hex,
 version: 4,
 variant: :rfc4122]

iex> UUID.info!("urn:uuid:ef1b1a28-ee34-11e3-8813-14109ff1a304")
[uuid: "urn:uuid:ef1b1a28-ee34-11e3-8813-14109ff1a304",
 binary: <<239, 27, 26, 40, 238, 52, 17, 227, 136, 19, 20, 16, 159, 241, 163, 4>>,
 type: :urn,
 version: 1,
 variant: :rfc4122]
string_to_binary!(arg1)

Convert a UUID string to its binary data equivalent.

Will raise an ArgumentError if the given string is not a UUID representation in a format like:

  • "870df8e8-3107-4487-8316-81e089b8c2cf"
  • "8ea1513df8a14dea9bea6b8f4b5b6e73"
  • "urn:uuid:ef1b1a28-ee34-11e3-8813-14109ff1a304"

Examples

iex> UUID.string_to_binary!("870df8e8-3107-4487-8316-81e089b8c2cf")
<<135, 13, 248, 232, 49, 7, 68, 135, 131, 22, 129, 224, 137, 184, 194, 207>>

iex> UUID.string_to_binary!("8ea1513df8a14dea9bea6b8f4b5b6e73")
<<142, 161, 81, 61, 248, 161, 77, 234, 155, 234, 107, 143, 75, 91, 110, 115>>

iex> UUID.string_to_binary!("urn:uuid:ef1b1a28-ee34-11e3-8813-14109ff1a304")
<<239, 27, 26, 40, 238, 52, 17, 227, 136, 19, 20, 16, 159, 241, 163, 4>>
uuid1(format \\ :default)

Generate a new UUID v1. This version uses a combination of one or more of: unix epoch, random bytes, pid hash, and hardware address.

Examples

iex> UUID.uuid1()
"cdfdaf44-ee35-11e3-846b-14109ff1a304"

iex> UUID.uuid1(:default)
"cdfdaf44-ee35-11e3-846b-14109ff1a304"

iex> UUID.uuid1(:hex)
"cdfdaf44ee3511e3846b14109ff1a304"

iex> UUID.uuid1(:urn)
"urn:uuid:cdfdaf44-ee35-11e3-846b-14109ff1a304"
uuid1(clock_seq, node, format \\ :default)

Generate a new UUID v1, with an existing clock sequence and node address. This version uses a combination of one or more of: unix epoch, random bytes, pid hash, and hardware address.

Examples

iex> UUID.uuid1()
"cdfdaf44-ee35-11e3-846b-14109ff1a304"

iex> UUID.uuid1(:default)
"cdfdaf44-ee35-11e3-846b-14109ff1a304"

iex> UUID.uuid1(:hex)
"cdfdaf44ee3511e3846b14109ff1a304"

iex> UUID.uuid1(:urn)
"urn:uuid:cdfdaf44-ee35-11e3-846b-14109ff1a304"
uuid3(namespace_or_uuid, name, format \\ :default)

Generate a new UUID v3. This version uses an MD5 hash of fixed value (chosen based on a namespace atom - see Appendix C of RFC 4122 and a name value. Can also be given an existing UUID String instead of a namespace atom.

Accepted arguments are: :dns|:url|:oid|:x500|:nil OR uuid, String

Examples

iex> UUID.uuid3(:dns, "my.domain.com")
"eecf4c2b-f6e5-3ae3-bef7-1ea09f91d3e7"

iex> UUID.uuid3(:dns, "my.domain.com", :default)
"eecf4c2b-f6e5-3ae3-bef7-1ea09f91d3e7"

iex> UUID.uuid3(:dns, "my.domain.com", :hex)
"eecf4c2bf6e53ae3bef71ea09f91d3e7"

iex> UUID.uuid3(:dns, "my.domain.com", :urn)
"urn:uuid:eecf4c2b-f6e5-3ae3-bef7-1ea09f91d3e7"

iex> UUID.uuid3("cdfdaf44-ee35-11e3-846b-14109ff1a304", "my.domain.com")
"8808f33a-3e11-3708-919e-15fba88908db"
uuid4()

Generate a new UUID v4. This version uses pseudo-random bytes generated by the crypto module.

Accepts optional :strong (default) or :weak parameter.

Examples

# Equivalent to: UUID.uuid4(:strong, :default)
iex> UUID.uuid4()
"fb49a0ec-d60c-4d20-9264-3b4cfe272106"

# Equivalent to: UUID.uuid4(:strong, :default)
iex> UUID.uuid4(:strong)
"fb49a0ec-d60c-4d20-9264-3b4cfe272106"

# Equivalent to: UUID.uuid4(:weak, :default)
iex> UUID.uuid4(:weak)
"cd63a9c4-0b4e-477a-8229-3f3aa971a37b"

# Equivalent to: UUID.uuid4(:strong, :default)
iex> UUID.uuid4(:default)
"fb49a0ec-d60c-4d20-9264-3b4cfe272106"

# Equivalent to: UUID.uuid4(:strong, :hex)
iex> UUID.uuid4(:hex)
"fb49a0ecd60c4d2092643b4cfe272106"

# Equivalent to: UUID.uuid4(:strong, :urn)
iex> UUID.uuid4(:urn)
"urn:uuid:fb49a0ec-d60c-4d20-9264-3b4cfe272106"

iex> UUID.uuid4(:weak, :urn)
"urn:uuid:cd63a9c4-0b4e-477a-8229-3f3aa971a37b"
uuid4(format)
uuid4(arg1, format)
uuid5(namespace_or_uuid, name, format \\ :default)

Generate a new UUID v5. This version uses an SHA1 hash of fixed value (chosen based on a namespace atom - see Appendix C of RFC 4122 and a name value. Can also be given an existing UUID String instead of a namespace atom.

Accepted arguments are: :dns|:url|:oid|:x500|:nil OR uuid, String

Examples

iex> UUID.uuid5(:dns, "my.domain.com")
"ae119419-7776-563d-b6e8-8a177abccc7a"

iex> UUID.uuid5(:dns, "my.domain.com", :default)
"ae119419-7776-563d-b6e8-8a177abccc7a"

iex> UUID.uuid5(:dns, "my.domain.com", :hex)
"ae1194197776563db6e88a177abccc7a"

iex> UUID.uuid5(:dns, "my.domain.com", :urn)
"urn:uuid:ae119419-7776-563d-b6e8-8a177abccc7a"

iex> UUID.uuid5("fb49a0ec-d60c-4d20-9264-3b4cfe272106", "my.domain.com")
"822cab19-df58-5eb4-98b5-c96c15c76d32"