SCRU128: Sortable, Clock and Random number-based Unique identifier

August 29, 2023 ยท View on GitHub

GitHub tag License Swift Compatibility Platform Compatibility

SCRU128 ID is yet another attempt to supersede UUID for the users who need decentralized, globally unique time-ordered identifiers. SCRU128 is inspired by ULID and KSUID and has the following features:

  • 128-bit unsigned integer type
  • Sortable by generation time (as integer and as text)
  • 25-digit case-insensitive textual representation (Base36)
  • 48-bit millisecond Unix timestamp that ensures useful life until year 10889
  • Up to 281 trillion time-ordered but unpredictable unique IDs per millisecond
  • 80-bit three-layer randomness for global uniqueness
import Scru128

// generate a new identifier object
let x = scru128()
print(x)  // e.g., "036z951mhjikzik2gsl81gr7l"
print(x.byteArray)  // as a 128-bit unsigned integer in big-endian byte array

// generate a textual representation directly
print(scru128String())  // e.g., "036z951mhzx67t63mq9xe6q0j"

See SCRU128 Specification for details.

Add swift-scru128 as a package dependency

To add this library to your Xcode project as a dependency, select File > Add Packages and enter the package URL: https://github.com/scru128/swift-scru128

To use this library in a SwiftPM project, add the following line to the dependencies in your Package.swift file:

.package(url: "https://github.com/scru128/swift-scru128", from: "<version>"),

And, include Scru128 as a dependency for your target:

.target(
  name: "<target>",
  dependencies: [.product(name: "Scru128", package: "swift-scru128")]
)

License

Licensed under the Apache License, Version 2.0.

See also