ExCuid2 (ex_cuid2 v0.9.0)

CUID2 (Collision-Resistant Unique Identifiers) generator in Elixir.

This implementation follows the CUID2 standard and generates safe, horizontally scalable IDs, ideal for use as primary keys in databases.

Features:

  • Prefix with a random letter.
  • Timestamp in milliseconds.
  • Atomic counter to prevent collisions in the same millisecond.
  • Cryptographically secure entropy.
  • Process fingerprint to ensure uniqueness across nodes.

Summary

Functions

Generates a CUID2 with the default length (24 characters).

Generates a CUID2 with a specific length (between 24 and 32).

Validates whether a string matches the CUID2 format.

Starts the counter agent.

Functions

generate()

Generates a CUID2 with the default length (24 characters).

Examples

iex> ExCuid2.generate() |> String.length()
24

generate(length)

Generates a CUID2 with a specific length (between 24 and 32).

Examples

iex> ExCuid2.generate(32) |> String.length()
32

is_valid?(cuid)

Validates whether a string matches the CUID2 format.

Examples

iex> ExCuid2.is_valid?("t4p35j2w2fiyqrec00pjxd7b")
true

iex> ExCuid2.is_valid?("123-abc")
false

start_link(opts)

Starts the counter agent.

It should be added to the supervision tree in application.ex.