Skip to main content

Crate cuid

Crate cuid 

Source
Expand description

§CUID generation in rust

This crate contains implementations of both the v1 and v2 CUID algorithms. By default, both v1 and v2 CUIDs are available. If you are optimizing for binary size, you can exclude one or the other by setting default_features = false and selecting the one you need (see Features, below).

In addition, both CUID algorithms are provided as their own independent crates, which this one merely wraps. They are creatively named cuid1 and cuid2.

§Usage

use cuid;

// Get a v1 CUID
println!("{}", cuid::cuid1());
println!("{}", cuid::v1::cuid());

// Get a v2 CUID
println!("{}", cuid::cuid2());
println!("{}", cuid::v2::cuid());

Note that this crate also provides a very simple, single purpose commandline interface:

$> cuid
ckfritrvg0000kdtwc766fful

You can generate v2 CUIDs via the commandline like so:

$> cuid --v2
i12sf8k69lbvktlr7qb4p6xv

v2 CUIDs also support some customization, allowing the specification of:

  • length
  • a counter function
  • a system fingerprint function

See the v2 CuidConstructor docs for more details.

§Should I Use v1 or v2?

According to the original implementation, v1 CUIDs are deprecated because they are “insecure,” due to the fact that they are k-sortable, which is to say, as one goes forward in time, v1 CUIDs go up.

K-sortability is actually a common property of IDs, and is sometimes desired. For example, k-sortable IDs are great for PKs for high-volume timeseries data, since they significantly improve index locality of adjacent rows. As an example, v7 UUIDs are k-sortable, and you can find plenty of material online discussing performance improvements using them for primary keys in postgres or other databases.

That said, k-sortable IDs are significantly more “guessable” than non-k-sortable IDs, which can potentially be a security issue for certain applications.

For CUID, the v1 algorithm is simpler and faster than the v2 algorithm. Generating a v1 CUID is around 7-8x faster than a v2 CUID (~127 ns vs ~962 ns on my machine).

As such, if your use-case is not sensitive to guessability, I would recommend going with v1.

For more information, see this issue

§Prior Art

See the original v1 implementation and original v2 implementation for more details on CUIDs in general.

§Features

  • v1 (enabled by default): provides access to v1 CUIDs
  • v2 (enabled by default): provides access to v2 CUIDs

Re-exports§

pub use cuid1 as v1;
pub use cuid2 as v2;

Structs§

Cuid2Constructor
Provides customization of CUID generation.

Functions§

cuid1
Generate a v1 CUID
cuid2
Creates a new CUID.
cuid1_slug
Generate a CUID v1 slug
cuid2_slug
Creates a new CUID slug, which is just a CUID with a length of 10 characters.
is_cuid1
Return whether a string looks like it could be a legitimate CUID
is_cuid2
Return whether a string is a legitimate CUID2
is_cuid1_slug
Return whether a string looks like it could be a legitimate v1 CUID slug
is_cuid2_slug
Return whether a string looks like it could be a legitimate CUID slug.