SCRU64 Swift Implementation

SCRU64: Sortable, Clock-based, Realm-specifically Unique identifier

GitHub tag License Swift Compatibility Platform Compatibility

SCRU64 ID offers compact, time-ordered unique identifiers generated by distributed nodes. SCRU64 has the following features:

  • ~62-bit non-negative integer storable as signed/unsigned 64-bit integer
  • Sortable by generation time (as integer and as text)
  • 12-digit case-insensitive textual representation (Base36)
  • ~38-bit Unix epoch-based timestamp that ensures useful life until year 4261
  • Variable-length node/machine ID and counter fields that share 24 bits
import Darwin  // import `setenv()`
import Scru64

// pass node ID through environment variable
setenv("SCRU64_NODE_SPEC", "42/8", 1)

// generate a new identifier object
let x = scru64Sync()
print(x)  // e.g., "0u2r85hm2pt3"
print(x.num)  // as a 64-bit unsigned integer

// generate a textual representation directly
print(scru64StringSync())  // e.g., "0u2r85hm2pt4"

See SCRU64 Specification for details.

SCRU64's uniqueness is realm-specific, i.e., dependent on the centralized assignment of node ID to each generator. If you need decentralized, globally unique time-ordered identifiers, consider SCRU128.

Add swift-scru64 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/scru64/swift-scru64

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/scru64/swift-scru64", from: "<version>"),

And, include Scru64 as a dependency for your target:

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


Licensed under the Apache License, Version 2.0.

See also


  • Swift Tools 5.6.0
View More Packages from this Author


  • None
Last updated: Fri Sep 29 2023 11:40:18 GMT-0900 (Hawaii-Aleutian Daylight Time)