nuid (nuid v1.0.0)
View SourceUnique identifier generation.
This module produces two families of identifiers as t/0 binaries:
- UUIDs. Versions 1, 3, 4, and 5 per
RFC 4122, and versions 6, 7,
and 8 per RFC 9562, plus the
nil and max UUIDs. Rendered as the canonical 36-character hyphenated
form, e.g.
<<"018b3d7a-9f9a-7577-adb2-08761e3d87f7">>. - nuids.
nuid1/0andnuid2/0, two Nomasystems identifiers that are lexicographically sortable by creation time and carry 128 bits of cryptographically strong randomness. They are encoded with a URL-safe, sortable base64 variant (seenuid_base64).
Time-based and random identifiers (uuid1/0, uuid4/0, uuid6/0,
uuid7/0, nuid1/0, nuid2/0) are non-deterministic. The name-based
identifiers (uuid3/2, uuid5/2) are deterministic: the same
namespace/0 and name always produce the same UUID.
The *_info/1 functions recover the creation time (and, where encoded,
the originating node) from an identifier.
Examples
1> nuid:uuid4().
<<"37a9e737-f680-44a9-b83d-a517ec758b75">>
2> nuid:uuid5(dns, <<"nomasystems.com">>).
<<"cefe05b2-95ca-5b0a-ad06-9b3f2b38e532">>
3> nuid:uuid7().
<<"018b3d7a-9f9a-7577-adb2-08761e3d87f7">>
4> nuid:nuid2().
<<"OHtpP-----Fkn3F6JaT5Kxnm_NAiDzFgGMzc">>
Summary
Types
Namespace for name-based UUIDs. The atoms select the predefined RFC 4122 namespace UUIDs; a binary is used verbatim as a custom namespace.
A generated identifier as a printable binary.
Decoded creation time, embedded counter, and originating node of a
time-based UUID. Defined by the uuidInfo record in nuid.hrl.
Functions
Return the RFC 9562 max UUID (all one bits).
Return the RFC 4122 nil UUID (all zero bits).
Generate a nuid1 identifier.
Recover the creation time from a nuid1 identifier.
Generate a nuid2 identifier.
Recover the originating node and creation time from a nuid2 identifier.
Generate a time-based RFC 4122 version 1 UUID.
Recover creation time, counter, and node from a version 1 UUID.
Generate a name-based RFC 4122 version 3 UUID (MD5).
Generate a random RFC 4122 version 4 UUID (122 random bits).
Generate a name-based RFC 4122 version 5 UUID (SHA-1).
Generate a time-ordered RFC 9562 version 6 UUID.
Recover creation time, counter, and node from a version 6 UUID.
Generate a time-ordered RFC 9562 version 7 UUID.
Recover the creation time from a version 7 UUID.
Generate a vendor-specific RFC 9562 version 8 UUID from a 128-bit binary.
Generate a vendor-specific RFC 9562 version 8 UUID from its three custom fields (48, 12, and 62 bits).
Types
-type date() :: {non_neg_integer(), month(), day()}.
-type day() :: 1..31.
-type hour() :: 0..23.
-type mins() :: 0..59.
-type month() :: 1..12.
-type namespace() :: dns | url | oid | x500 | nil | binary().
Namespace for name-based UUIDs. The atoms select the predefined RFC 4122 namespace UUIDs; a binary is used verbatim as a custom namespace.
-type secs() :: 0..59.
-type t() :: binary().
A generated identifier as a printable binary.
Decoded creation time, embedded counter, and originating node of a
time-based UUID. Defined by the uuidInfo record in nuid.hrl.
Functions
-spec max_uuid() -> t().
Return the RFC 9562 max UUID (all one bits).
-spec nil_uuid() -> t().
Return the RFC 4122 nil UUID (all zero bits).
-spec nuid1() -> t().
Generate a nuid1 identifier.
A hex microsecond timestamp, a separator, and 16 cryptographically strong random bytes in sortable base64. Lexicographically sortable and greater than any previously generated version 6 UUID.
Recover the creation time from a nuid1 identifier.
-spec nuid2() -> t().
Generate a nuid2 identifier.
A POSIX-second timestamp, a sortable counter, 3 bytes of node origin, and 16 cryptographically strong random bytes, all in sortable base64. Lexicographically sortable, URL-safe, and no longer than a UUID.
Recover the originating node and creation time from a nuid2 identifier.
The node is resolved against the currently connected nodes; it is
undefined if the originating node is not reachable.
-spec uuid1() -> t().
Generate a time-based RFC 4122 version 1 UUID.
Recover creation time, counter, and node from a version 1 UUID.
Generate a name-based RFC 4122 version 3 UUID (MD5).
Deterministic: the same namespace/0 and name always yield the same
UUID.
-spec uuid4() -> t().
Generate a random RFC 4122 version 4 UUID (122 random bits).
Generate a name-based RFC 4122 version 5 UUID (SHA-1).
Deterministic: the same namespace/0 and name always yield the same
UUID.
-spec uuid6() -> t().
Generate a time-ordered RFC 9562 version 6 UUID.
The most significant bits hold the timestamp, so version 6 UUIDs sort lexicographically by creation time.
Recover creation time, counter, and node from a version 6 UUID.
-spec uuid7() -> t().
Generate a time-ordered RFC 9562 version 7 UUID.
A 48-bit Unix millisecond timestamp followed by 74 random bits. Sorts lexicographically by creation time.
Recover the creation time from a version 7 UUID.
Generate a vendor-specific RFC 9562 version 8 UUID from a 128-bit binary.
The version and variant bits in the input are overwritten; all other bits are preserved.
-spec uuid8(non_neg_integer(), non_neg_integer(), non_neg_integer()) -> t().
Generate a vendor-specific RFC 9562 version 8 UUID from its three custom fields (48, 12, and 62 bits).