Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

scru160

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

scru160

SCRU-160: Sortable, Clock and Random number-based Unique identifier

  • 0.2.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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)
  • Two case-insensitive encodings: 32-character base32hex and 40-character hex
  • More than 32,768 unique, time-ordered but unpredictable IDs per millisecond
  • Nearly 111-bit randomness for collision resistance
import { scru160, scru160f } from "scru160";

console.log(scru160()); // e.g. "05TTUP1HNCPNH30VEK64KDQT9BSNU4C4"
console.log(scru160()); // e.g. "05TTUP1HNCPNIB63R8IN5V2L3VFGNFET"

console.log(scru160f()); // e.g. "017bdf6431bb33750751eb63beb3c3f8c5969d86"
console.log(scru160f()); // e.g. "017bdf6431bb337662412e6a5758890735c33c2b"

Binary Layout and Byte Order

A SCRU-160 ID is an 160-bit object that consists of the following four fields:

Bit #FieldData Type
Msb 0 - 47timestamp48-bit unsigned integer, big-endian
Msb 48 - 63counter16-bit unsigned integer, big-endian
Msb 64 - 79random1616-bit unsigned integer, big-endian
Msb 80 - 159random8080-bit unsigned integer, big-endian
  • timestamp - Unix time in milliseconds
  • counter - 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(); // e.g. "05TUIM8J8SU9O6P30I56PP49PI0RGNPM"

Alternatively, hexadecimal encoding can be used to produce a 40-character hex string that looks like the commonly seen SHA-1 hashes.

scru160f(); // e.g. "017be95b2d6042db903a19a0974182ec7445188a"

The base32hex representations and hex representations cannot be mixed when SCRU-160 IDs need to be lexicographically sortable.

License

Copyright 2021 LiosK

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

See Also

Keywords

FAQs

Package last updated on 19 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc