Cuid2Ex (cuid2_ex v0.2.0)
Implementation of the CUID2 (Collision-resistant Unique IDentifier) algorithm.
CUID2 generates secure, collision-resistant ids optimized for horizontal scaling and performance. The generated ids are URL-safe, contain no special characters, and have a fixed length.
Example
iex> Cuid2Ex.create()
"k0xpkry4lx8tl3qh8vry0f6m"
iex> generator = Cuid2Ex.init(length: 32)
iex> generator.()
"k0xpkry4lx8tl3qh8vry0f6maabc1234"
Summary
Functions
Converts a binary buffer to a big integer.
Creates a new CUID2 string with the given options.
Creates a counter function that increments from the given starting count.
Creates entropy string of specified length.
Creates a fingerprint for the CUID generator using create_entropy/2
with @big_length
.
Validates if a given string is a valid Cuid2Ex.
Creates a hash of the input string using SHA3-512 and converts it to base36. Drops the first character because it will bias the histogram to the left.
Initializes a CUID generator function with the given options.
Computes SHA3-512 hash of input and returns it as a big integer.
Functions
buf_to_big_int(buf)
Converts a binary buffer to a big integer.
create(opts \\ [])
Creates a new CUID2 string with the given options.
See init/1
for available options.
create_counter(count)
Creates a counter function that increments from the given starting count.
Parameters
count
- Starting count
create_entropy(length \\ 4, random \\ &:rand.uniform/0, entropy \\ "")
Creates entropy string of specified length.
Parameters
length
- The desired length of the entropy string (default: 4)random
- Function that returns random float between 0 and 1 (default::rand.uniform/0
)entropy
- Initial entropy string (default: "")
create_fingerprint(random \\ :rand.uniform() / 0)
Creates a fingerprint for the CUID generator using create_entropy/2
with @big_length
.
Parameters
random
- Random number generator function (default::rand.uniform/0
)
cuid?(id, opts \\ [])
Validates if a given string is a valid Cuid2Ex.
Options
:min_length
- Minimum allowed length (default: 2):max_length
- Maximum allowed length (default: 32)
Examples
iex> Cuid2Ex.cuid?("k0xpkry4lx8tl3qh8vry0f6m")
true
iex> Cuid2Ex.cuid?("invalid!")
false
hash(input \\ "")
Creates a hash of the input string using SHA3-512 and converts it to base36. Drops the first character because it will bias the histogram to the left.
init(opts \\ [])
Initializes a CUID generator function with the given options.
Options
:length
- Length of generated ids (default: 24):random
- Custom random number generator function:counter
- Custom counter function:fingerprint
- Custom fingerprint string
Returns a function that generates CUID2 strings when called.
sha3_512(input)
Computes SHA3-512 hash of input and returns it as a big integer.