Two different identifiers
Classic CUID (often called CUID v1) is a 25-character string that starts with the letter c. It was built so horizontal workers could mint ids without a central sequence, and so the value was safe to drop into an HTML id or a URL. The parts are readable: a timestamp, a counter, a host fingerprint, and random characters.
CUID2 is a later design from the same author. It deliberately hides the counter, the host, and the time inside a hash, because a readable counter lets someone who has seen a few ids guess the next one. A CUID2 is not time-sortable. The official library (@paralleldrive/cuid2) defaults to 24 characters, starts with a random letter from a to z, and hashes entropy, a session counter, a host fingerprint, and time.
How a classic CUID is cut
The original layout is one letter, then base36 text. base36 uses digits and lowercase letters. A millisecond timestamp is currently 8 characters. It stays 8 until the millisecond count reaches 36^8, which is year 2059. The counter block is 4 characters, the fingerprint block is 4, and the random block is 8, in two groups of 4. 1 + 8 + 4 + 4 + 8 = 25.
The sample below is a layout, not a promise that the button will print this string. lqu5m2o0 is the base36 form of 1704067200000, which is 2024-01-01T00:00:00.000Z. The counter 004h is a small base36 integer padded to 4. The fingerprint on a server is derived from the process id and the hostname. In this browser the fingerprint is the fixed label devf, because a page has no process id. The last 8 characters are random base36.
c prefix, so the id can be an HTML identifier
lqu5m2o0 timestamp, base36 of 1704067200000
004h counter block, padded to 4
devf fingerprint block, 4 characters
1k2m9p0q random block, 8 characters of base36
clqu5m2o0004hdevf1k2m9p0qWhat CUID2 changes, and what this page generates
You cannot decode an official CUID2 back into time, counter, or host. Those inputs are mixed by a cryptographic hash and then written in base36. The first character is chosen from a to z so the id is a valid HTML identifier even when the hash would have started with a digit. Length is the security parameter. For a 24-character id, if the first character is one of 26 letters and the other 23 are uniform base36, the space is 26 x 36^23, about 124 bits. Shorter ids are a smaller space. The library author treats 24 as the default for a large application and treats very short lengths as a choice you have to justify.
The CUID2 control on this page is a demonstration of that shape, not a build of @paralleldrive/cuid2. It always starts with c, mixes time, a counter, and Math.random with a small non-cryptographic hash, then pads with more Math.random until it reaches the length you set (4 to 32, default 24). Do not quote the 124-bit figure for values from this page. For production, use the official package, which uses a cryptographic hash and a random initial letter.
When to use which
- Use official CUID2 when the id will appear in a URL or an API and you do not want observers to learn the time, the host, or the counter.
- Use classic CUID only if an existing system already stores that 25-character form. New work should not prefer it: the counter and the timestamp are visible.
- Prefer a ULID when the id must sort by creation time in a database index. Neither CUID form is a sort key.
- Prefer Snowflake when you need a 64-bit integer and you can assign a unique worker id.
Pitfalls
- A CUID v1 from this page shares the fingerprint devf with every other visitor. That is fine for a demo. It is not a substitute for a server generator that fingerprints the host.
- The counter on this page wraps in a small base36 range. It blocks duplicates inside one browser tab. It does not coordinate with other tabs or other machines.
- CUID2 length is not free entropy on this page. The slider changes how many characters you see. The official collision math applies to the official generator.
- Do not use either form as a password or a session secret. They are identifiers. A leaked id is meant to be handleable.
- Sorting a column of CUID2 values does not mean sorting by time. If you need that order, store a timestamp or use a ULID.
CUID next to the other ids on this site
| CUID v1 | CUID2 (official) | ULID | |
|---|---|---|---|
| Length | 25 characters | 24 by default, configurable | 26 characters |
| Starts with a letter | Always c | A random letter a-z | Often a digit |
| Time readable | Yes, base36 milliseconds | No, it is inside the hash | Yes, first 10 characters |
| Sorts by time | Roughly, until the timestamp gains a digit | No | Yes |
| Hides the host and counter | No | Yes | There is no host id |
Questions
Why does a classic CUID start with c?
So the value is a legal HTML and CSS identifier. Those identifiers cannot start with a digit. The c is a fixed prefix, not a checksum.
Can I read the time out of a CUID v1?
Yes. Drop the leading c and read the next 8 base36 characters as a millisecond timestamp, while timestamps are still 8 characters. The sample lqu5m2o0 decodes to 2024-01-01T00:00:00.000Z. After 2059 the timestamp grows to 9 characters and a fixed slice of 8 will be wrong.
Is the CUID2 from this page the official algorithm?
No. Official CUID2 uses a cryptographic hash and a random first letter. This page shows the idea: a letter, then a hashed body, with a length you can change. Ship the official library in an application.
How big is the official CUID2 space at length 24?
About 124 bits if the first character is one of 26 letters and each of the other 23 characters is a uniform base36 symbol: log2(26) + 23 x log2(36). That is the same order of magnitude as a UUID. A shorter length is a smaller space.
Should I use CUID2 or a ULID as a primary key?
Use CUID2 when unpredictability matters more than order. Use a ULID when the index should follow creation time. They solve different problems, and this site can generate both.