uuid_tools v0.1.0 UUIDTools.UUID View Source
UUIDTools.UUID is a module that handles the generation of UUIDs for Elixir. It follows the RFC 4122.
Link to this section Summary
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.
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.
Link to this section Functions
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> UUIDTools.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> UUIDTools.UUID.binary_to_string!(<<142, 161, 81, 61, 248, 161, 77, 234, 155,
...> 234, 107, 143, 75, 91, 110, 115>>, :hex)
"8ea1513df8a14dea9bea6b8f4b5b6e73"
iex> UUIDTools.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> UUIDTools.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>>
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> UUIDTools.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> UUIDTools.UUID.string_to_binary!("8ea1513df8a14dea9bea6b8f4b5b6e73")
<<142, 161, 81, 61, 248, 161, 77, 234, 155, 234, 107, 143, 75, 91, 110, 115>>
iex> UUIDTools.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> UUIDTools.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>>
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> UUIDTools.UUID.uuid1()
"cdfdaf44-ee35-11e3-846b-14109ff1a304"
iex> UUIDTools.UUID.uuid1(:default)
"cdfdaf44-ee35-11e3-846b-14109ff1a304"
iex> UUIDTools.UUID.uuid1(:hex)
"cdfdaf44ee3511e3846b14109ff1a304"
iex> UUIDTools.UUID.uuid1(:urn)
"urn:uuid:cdfdaf44-ee35-11e3-846b-14109ff1a304"
iex> UUIDTools.UUID.uuid1(:raw)
<<205, 253, 175, 68, 238, 53, 17, 227, 132, 107, 20, 16, 159, 241, 163, 4>>
iex> UUIDTools.UUID.uuid1(:slug)
"zf2vRO41EeOEaxQQn_GjBA"
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> UUIDTools.uuid1()
"e93880b4-c4b7-11e9-8925-f2189835db58"
iex> UUIDTools.uuid1(:default)
"f580ace8-c4b7-11e9-a704-f2189835db58"
iex> UUIDTools.uuid1(:hex)
"05bcb75ac4b811e99c8af2189835db58"
iex> UUIDTools.uuid1(:urn)
"urn:uuid:10ac6930-c4b8-11e9-93d7-f2189835db58"
iex> UUIDTools.uuid1(:raw)
<<31, 88, 207, 250, 196, 184, 17, 233, 187, 65, 242, 24, 152, 53, 219, 88>>
iex> UUIDTools.uuid1(:slug)
"V7E_wsS4EemLq_IYmDXbWA"
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.
Examples
iex> UUIDTools.uuid3(:md5, "google.com", :raw)
<<154, 116, 200, 62, 44, 9, 53, 19, 167, 75, 145, 214, 121, 190, 130, 184>>
iex> UUIDTools.uuid3("8808f33a-3e11-3708-919e-15fba88908db", "google.com")
"9556d661-520d-3843-8745-4e0601b06ca0"
iex> UUIDTools.uuid3(:md5, "google.com", :slug)
"mnTIPiwJNROnS5HWeb6CuA"
Generate a new UUID v4. This version uses pseudo-random bytes generated by
the crypto
module.
Examples
iex> UUIDTools.uuid4()
"e453a6bf-1acc-41a0-a768-8e486ff0bc74"
iex> UUIDTools.uuid4(:default)
"06e04ae3-5db2-4b20-bb31-fc1ffdc4ac87"
iex> UUIDTools.uuid4(:hex)
98c73069ce8549ac82ef4a131d7f05b4
iex> UUIDTools.uuid4(:urn)
"urn:uuid:18f2ea3b-6508-4f42-b65d-a0eaa6892bf4"
iex> UUIDTools.uuid4(:raw)
<<193, 103, 191, 223, 115, 251, 75, 179, 165, 138, 217, 109, 228, 155, 201, 163>>
iex> UUIDTools.uuid4(:slug)
"p78u0Qi7RpqX6D3A8FD5BQ"
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.
Examples
iex> UUIDTools.uuid5(:sha1, "google.com")
"64ee70a4-8cc1-5d25-abf2-dea6c79a09c8"
iex> UUIDTools.uuid5("d26d4db3-2a94-5185-a091-5b7b61148c87", "google.com")
"4699746d-3d64-5122-a1f3-187f40fb63ac"
iex> UUIDTools.uuid5("d26d4db3-2a94-5185-a091-5b7b61148c87", "google.com", :slug)
"Rpl0bT1kUSKh8xh_QPtjrA"