Skip to content

Next generation guids. Secure, collision-resistant ids optimized for horizontal scaling and performance.

License

Notifications You must be signed in to change notification settings

jwojnowski/scuid

Repository files navigation

Scuid 🦑 — Cuid2 for Scala

License Maven Central Known Vulnerabilities

Functional Scala implementation of Cuid2.

Cuid2 examples

sa9vplf9bpyqj6hui8rsvi27
nfehj5xe9g7aruem87e1dcb6
p50d8uxvzs0irgrl9sidxqos

Benefits over other IDs

  • secure
  • collision-resistant
  • optimized for horizontal scaling and performance
  • friendlier to humans (easier to distinguish visually, double-click-selectable)

A full comparison to other IDs can be found in the readme of the original implementation.

Getting started

SBT

libraryDependencies += "me.wojnowski" %% "scuid" % "<version>"
libraryDependencies += "me.wojnowski" %% "scuid-circe" % "<version>"

Usage (Cats Effect)

import cats.effect.IO
import me.wojnowski.scuid.Cuid2
import me.wojnowski.scuid.Cuid2Gen

...

for {
  cuid2Generator: Cuid2Gen[IO] <- Cuid2Gen.default[IO]
  id: Cuid2                    <- cuid2Generator.generate // q055n575se3xpxu2xxmmd3ex
} yield ()

Usage (sync)

import cats.Id
import me.wojnowski.scuid.Cuid2
import me.wojnowski.scuid.Cuid2Gen

...

val generator: Cuid2Gen[Id] = Cuid2Gen.unsafeDefault
val id: Cuid2               = generator.generate // r0o5ncoizclu1b9iraz620cn

Length

There are three options for length of the generated ID:

  1. Cuid2 - 24 characters (default)
  2. Cuid2Long - 32 characters
  3. Cuid2Custom[n] - custom length (parametrised using literal types, e.g. Cuid2Custom[10] or Cuid2Custom[27])

Note on SHA3 support

This library uses SHA3, which is available from JDK 9 (b119) onwards. Alternative implementation can be provided via Cuid2Gen.custom.

Validation

val idDefault: Either[ValidationError, Cuid2]                = Cuid2.validate("r0o5ncoizclu1b9iraz620cn")
val idLong: Either[ValidationError, Cuid2Long]               = Cuid2Long.validate("tzwxyg5tav24zm8ycsrtpfi0njhegmes")
val idCustomLength: Either[ValidationError, Cuid2Custom[10]] = Cuid2Custom.validate[10]("axcf7v6n1w")

ValidationError is an ADT, which offers prettyMessage method for easy conversion to String.

Circe integration

Use the following import for Cuid2 codecs:

import me.wojnowski.scuid.circe._

TODO

  • Circe integration
  • Tapir integration
  • ZIO integration

About

Next generation guids. Secure, collision-resistant ids optimized for horizontal scaling and performance.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Languages