UUID (UUID v1.6.5) View Source

UUID generator and utilities for Elixir. See RFC 4122.

Link to this section Summary

Types

Hex representation of UUID.

Information about given UUID (see info/1)

Namespace for UUID v3 and v5 (with some predefined UUIDs as atom aliases).

Raw binary representation of UUID.

Slug representation of UUID.

String representation of UUID.

t()

One of representations of UUID.

Type of UUID representation.

URN representation of UUID.

Variant of UUID: see RFC for the details.

UUID version.

Functions

Convert binary UUID data to a string.

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.

Convert a UUID v1 to a UUID v6 in the same format.

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.

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

Generate a new UUID v6, 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.

Convert a UUID v6 to a UUID v1 in the same format.

Extract the type (:default etc) and pure byte value from a UUID String.

Validate a RFC4122 UUID.

Identify the UUID variant according to section 4.1.1 of RFC 4122.

Link to this section Types

Specs

hex() :: <<_::256>>

Hex representation of UUID.

Specs

info() :: UUID.Info.t()

Information about given UUID (see info/1)

Specs

namespace() :: :dns | :url | :oid | :x500 | nil | str()

Namespace for UUID v3 and v5 (with some predefined UUIDs as atom aliases).

Specs

raw() :: <<_::128>>

Raw binary representation of UUID.

Specs

slug() :: String.t()

Slug representation of UUID.

Specs

str() :: <<_::288>>

String representation of UUID.

Specs

t() :: str() | raw() | hex() | urn() | slug()

One of representations of UUID.

Specs

type() :: :default | :raw | :hex | :urn | :slug

Type of UUID representation.

Specs

urn() :: <<_::360>>

URN representation of UUID.

Specs

variant() :: :reserved_future | :reserved_microsoft | :rfc4122 | :reserved_ncs

Variant of UUID: see RFC for the details.

Specs

version() :: 1 | 3 | 4 | 5 | 6

UUID version.

Link to this section Functions

Link to this function

binary_to_string!(uuid, format \\ :default)

View Source

Specs

binary_to_string!(raw(), type()) :: t()

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, :urn, or :raw.

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"

iex> UUID.binary_to_string!(<<39, 73, 196, 181, 29, 90, 74, 96, 157,
...>        47, 171, 144, 84, 164, 155, 52>>, :raw)
<<39, 73, 196, 181, 29, 90, 74, 96, 157, 47, 171, 144, 84, 164, 155, 52>>

Specs

info(str()) :: {:ok, info()} | {:error, String.t()}

See UUID.Info.new/1.

Specs

info!(str()) :: info()

See UUID.Info.new!/1.

Specs

string_to_binary!(str()) :: raw()

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>>

iex> UUID.string_to_binary!(<<39, 73, 196, 181, 29, 90, 74, 96, 157, 47,
...>        171, 144, 84, 164, 155, 52>>)
<<39, 73, 196, 181, 29, 90, 74, 96, 157, 47, 171, 144, 84, 164, 155, 52>>
Link to this function

uuid1(format \\ :default)

View Source

Specs

uuid1(type()) :: t()

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"

iex> UUID.uuid1(:raw)
<<205, 253, 175, 68, 238, 53, 17, 227, 132, 107, 20, 16, 159, 241, 163, 4>>

iex> UUID.uuid1(:slug)
"zf2vRO41EeOEaxQQn_GjBA"
Link to this function

uuid1(clock_seq, node, format \\ :default)

View Source

Specs

uuid1(clock_seq :: <<_::14>>, node :: <<_::48>>, type()) :: t()

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"

iex> UUID.uuid1(:raw)
<<205, 253, 175, 68, 238, 53, 17, 227, 132, 107, 20, 16, 159, 241, 163, 4>>

iex> UUID.uuid1(:slug)
"zf2vRO41EeOEaxQQn_GjBA"

Convert a UUID v1 to a UUID v6 in the same format.

Examples

iex> UUID.uuid1_to_uuid6("dafc431a-0d21-11eb-adc1-0242ac120002")
"1eb0d21d-afc4-631a-adc1-0242ac120002"

iex> UUID.uuid1_to_uuid6("2vxDGg0hEeutwQJCrBIAAg")
"HrDSHa_EYxqtwQJCrBIAAg"

iex> UUID.uuid1_to_uuid6(<<218, 252, 67, 26, 13, 33, 17, 235, 173, 193, 2, 66, 172, 18, 0, 2>>)
<<30, 176, 210, 29, 175, 196, 99, 26, 173, 193, 2, 66, 172, 18, 0, 2>>
Link to this function

uuid3(namespace_or_uuid, name, format \\ :default)

View Source

Specs

uuid3(namespace(), name :: binary(), type()) :: t()

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")
"03bf0706-b7e9-33b8-aee5-c6142a816478"

iex> UUID.uuid3(:dns, "my.domain.com", :default)
"03bf0706-b7e9-33b8-aee5-c6142a816478"

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

iex> UUID.uuid3(:dns, "my.domain.com", :urn)
"urn:uuid:03bf0706-b7e9-33b8-aee5-c6142a816478"

iex> UUID.uuid3(:dns, "my.domain.com", :raw)
<<3, 191, 7, 6, 183, 233, 51, 184, 174, 229, 198, 20, 42, 129, 100, 120>>

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

iex> UUID.uuid3(:dns, "my.domain.com", :slug)
"A78HBrfpM7iu5cYUKoFkeA"

Specs

uuid4() :: str()

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

Examples

iex> UUID.uuid4()
"fb49a0ec-d60c-4d20-9264-3b4cfe272106"

iex> UUID.uuid4(:default)
"fb49a0ec-d60c-4d20-9264-3b4cfe272106"

iex> UUID.uuid4(:hex)
"fb49a0ecd60c4d2092643b4cfe272106"

iex> UUID.uuid4(:urn)
"urn:uuid:fb49a0ec-d60c-4d20-9264-3b4cfe272106"

iex> UUID.uuid4(:raw)
<<251, 73, 160, 236, 214, 12, 77, 32, 146, 100, 59, 76, 254, 39, 33, 6>>

iex> UUID.uuid4(:slug)
"-0mg7NYMTSCSZDtM_ichBg"

Specs

uuid4(type() | :strong | :weak) :: t()
Link to this function

uuid5(namespace_or_uuid, name, format \\ :default)

View Source

Specs

uuid5(namespace(), name :: binary(), type()) :: t()

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")
"016c25fd-70e0-56fe-9d1a-56e80fa20b82"

iex> UUID.uuid5(:dns, "my.domain.com", :default)
"016c25fd-70e0-56fe-9d1a-56e80fa20b82"

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

iex> UUID.uuid5(:dns, "my.domain.com", :urn)
"urn:uuid:016c25fd-70e0-56fe-9d1a-56e80fa20b82"

iex> UUID.uuid5(:dns, "my.domain.com", :raw)
<<1, 108, 37, 253, 112, 224, 86, 254, 157, 26, 86, 232, 15, 162, 11, 130>>

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

iex> UUID.uuid5("fb49a0ec-d60c-4d20-9264-3b4cfe272106", "my.domain.com", :slug)
"giyrGd9YXrSYtclsFcdtMg"
Link to this function

uuid6(node_type \\ :random_bytes, format \\ :default)

View Source

Specs

uuid6(:random_bytes | :mac_address, type()) :: t()

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

Accepts a node_type argument that can be either :mac_address or :random_bytes. Defaults to :random_bytes.

See the RFC draft, section 3.3 for more information on the node parts.

Examples

iex> UUID.uuid6()
"1eb0d28f-da4c-6eb2-adc1-0242ac120002"

iex> UUID.uuid6(:random_bytes, :default)
"1eb0d297-eb1e-62a6-a37f-a55eda5dd6e4"

iex> UUID.uuid6(:random_bytes, :hex)
"1eb0d298502563fcadcd25e5d0a44c1a"

iex> UUID.uuid6(:random_bytes, :urn)
"urn:uuid:1eb0d298-ca10-6914-ab0e-7d7e1e6e1808"

iex> UUID.uuid6(:random_bytes, :raw)
<<30, 176, 210, 153, 52, 23, 102, 230, 164, 146, 99, 66, 4, 72, 220, 114>>

iex> UUID.uuid6(:random_bytes, :slug)
"HrDSmab8ZnqR4SKw4LN-UA"
Link to this function

uuid6(arg1, arg2, format)

View Source

Generate a new UUID v6, 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.

Convert a UUID v6 to a UUID v1 in the same format.

Examples

iex> UUID.uuid6_to_uuid1("1eb0d21d-afc4-631a-adc1-0242ac120002")
"dafc431a-0d21-11eb-adc1-0242ac120002"

iex> UUID.uuid6_to_uuid1("HrDSHa_EYxqtwQJCrBIAAg")
"2vxDGg0hEeutwQJCrBIAAg"

iex> UUID.uuid6_to_uuid1(<<30, 176, 210, 29, 175, 196, 99, 26, 173, 193, 2, 66, 172, 18, 0, 2>>)
<<218, 252, 67, 26, 13, 33, 17, 235, 173, 193, 2, 66, 172, 18, 0, 2>>
Link to this function

uuid_string_to_hex_pair(uuid)

View Source

Specs

uuid_string_to_hex_pair(t()) :: {type(), raw()}

Extract the type (:default etc) and pure byte value from a UUID String.

Link to this function

valid?(uuid, version \\ "[0-6]")

View Source

Specs

valid?(binary(), 0..6 | String.t()) :: boolean()

Validate a RFC4122 UUID.

Specs

variant(binary()) :: variant()

Identify the UUID variant according to section 4.1.1 of RFC 4122.

Examples

iex> UUID.variant(<<1, 1, 1>>)
:reserved_future

iex> UUID.variant(<<1, 1, 0>>)
:reserved_microsoft

iex> UUID.variant(<<1, 0, 0>>)
:rfc4122

iex> UUID.variant(<<0, 1, 1>>)
:reserved_ncs

iex> UUID.variant(<<1>>)
** (ArgumentError) Invalid argument; Not valid variant bits