SCRU-160: Sortable, Clock and Random number-based Unique identifier
SCRU-160 ID is yet another attempt to supersede UUID in the use cases that
need decentralized, globally unique time-ordered identifiers. SCRU-160 is
inspired by ULID and KSUID and has the following features:
- 160-bit length
- Sortable by generation time (in binary and in text)
- At least 32,767 unique, time-ordered IDs per millisecond
- Nearly 111-bit randomness for collision resistance
- 32-character base32hex encoding and 40-character hex encoding
- Case insensitive
import { scru160, scru160f } from "scru160";
console.log(scru160());
console.log(scru160());
console.log(scru160f());
console.log(scru160f());
Binary Layout and Byte Order
A SCRU-160 ID is an 160-bit object that consists of the following four fields:
Bit # | Field | Data Type |
---|
Msb 0 - 47 | timestamp | 48-bit unsigned integer, big-endian |
Msb 48 - 63 | counter | 16-bit unsigned integer, big-endian |
Msb 64 - 79 | random16 | 16-bit unsigned integer, big-endian |
Msb 80 - 159 | random80 | 80-bit unsigned integer, big-endian |
timestamp
- Unix time in millisecondscounter
- Reset to a 15-bit random number when timestamp
changes;
incremented by one for each new ID generated within the same timestamp
(a
different implementation may reset it to a smaller random number or zero to
accommodate more IDs per millisecond)random16
- Random number (documented separately from random80
for future
expansion)random80
- Random number
Cryptographically secure random number generators are employed if possible.
Encodings
The base32hex encoding as defined in RFC 4648 is used by default to produce
a 32-character textual representation consisting of [0-9A-V]
.
scru160();
Alternatively, hexadecimal encoding can be used to produce a 40-character hex
string that looks like the commonly seen SHA-1 hashes.
scru160f();
The base32hex representations and hex representations cannot be mixed when
SCRU-160 IDs need to be lexicographically sortable.
See Also