CUID Versions

Resources  |  CUID Versions

CUID (Collision-resistant Unique Identifier) is a unique identifier system designed to be more robust and user-friendly compared to traditional UUIDs. There are different versions of CUID, each with its own use cases and features. The primary versions of CUID are CUID (original version) and CUID2.

CUID Versions

Original CUID

The original CUID focuses on being collision-resistant, human-readable, and suitable for distributed systems. Its format includes several components to ensure uniqueness and readability.

Format:

  • Prefix: c
  • Timestamp
  • Counter
  • Fingerprint
  • Randomness

Characteristics:

  • Prefixed with c to indicate it is a CUID.
  • The timestamp ensures the identifier is unique over time.
  • A counter to manage multiple CUIDs generated in quick succession.
  • Machine-specific fingerprint to avoid collisions across different machines.
  • Random characters to further reduce the risk of collisions.

Example:

c1df8fbf-7601-4f75-b7e8-43132e21acb9

CUID2

CUID2 is an evolution of the original CUID. It aims to address some limitations and adds new features to improve collision resistance, readability, and utility.

Format:

  • No fixed prefix, making it shorter and more flexible.
  • Improved base encoding for readability and compactness.
  • Enhanced collision resistance mechanisms.

Characteristics:

  • Improved encoding: Uses a more compact and readable base encoding.
  • Length flexibility: The length can be adjusted based on requirements, making it more versatile.
  • Enhanced collision resistance: Improved algorithms to further reduce the likelihood of collisions.

Example:

d25xwqgl4lg4ezqx25g3nxe15f

Generating CUID and CUID2 in Different Languages

JavaScript

Original CUID: To generate an original CUID in JavaScript, use the cuid library.

npm install cuid
const cuid = require('cuid');

const id = cuid();
console.log(id);  // Example output: c1df8fbf-7601-4f75-b7e8-43132e21acb9

CUID2: To generate a CUID2, use the cuid2 library.

npm install @paralleldrive/cuid2
const { createId } = require('@paralleldrive/cuid2');

const id = createId();
console.log(id);  // Example output: d25xwqgl4lg4ezqx25g3nxe15f

Python

For Python, you might need to implement the generation logic manually or find a suitable package. Here�s a basic implementation for CUID:

import time
import os
import random
import hashlib

def cuid():
    timestamp = int(time.time() * 1000)
    counter = random.randint(0, 16777215)
    machine_fingerprint = hashlib.md5(os.uname().nodename.encode()).hexdigest()[:4]
    random_part = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz0123456789', k=6))
    
    return f'c{timestamp:x}{counter:x}{machine_fingerprint}{random_part}'

print(cuid())  # Example output: c16f5b2d6d0000cc02c16eaa6

For CUID2, a similar approach can be taken but with improvements in encoding and collision resistance mechanisms.

Summary

CUID and CUID2 are both designed to offer unique identifiers with high collision resistance, human readability, and performance suitable for distributed systems. CUID2 improves upon the original CUID by offering more compact and flexible encoding, making it a robust choice for modern applications. Depending on your specific requirements, you can choose the appropriate version and implement it using available libraries or custom code.