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

Link to this function

buf_to_big_int(buf)

Converts a binary buffer to a big integer.

Link to this function

create(opts \\ [])

Creates a new CUID2 string with the given options.

See init/1 for available options.

Link to this function

create_counter(count)

Creates a counter function that increments from the given starting count.

Parameters

  • count - Starting count
Link to this function

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: "")
Link to this function

create_fingerprint(random \\ :rand.uniform() / 0)

@spec create_fingerprint(any()) :: binary()

Creates a fingerprint for the CUID generator using create_entropy/2 with @big_length.

Parameters

Link to this function

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
Link to this function

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.

Link to this function

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.

Link to this function

sha3_512(input)

Computes SHA3-512 hash of input and returns it as a big integer.