Socket
Socket
Sign inDemoInstall

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 - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

12

dist/index.d.ts

@@ -8,14 +8,14 @@ /**

*/
export { scru160, scru160V };
export { scru160, scru160f };
/**
* Generates a new SCRU-160 ID encoded in the hexadecimal format.
* Generates a new SCRU-160 ID encoded in the base32hex format.
*
* @returns 40-character hexadecimal string (`/^[0-9a-f]{40}$/`).
* @returns 32-character base32hexupper string (`/^[0-9A-V]{32}$/`).
*/
declare const scru160: () => string;
/**
* Generates a new SCRU-160 ID encoded in the base32hex format.
* Generates a new SCRU-160 ID encoded in the hexadecimal format.
*
* @returns 32-character base32hexupper string (`/^[0-9A-V]{32}$/`).
* @returns 40-character hexadecimal string (`/^[0-9a-f]{40}$/`).
*/
declare const scru160V: () => string;
declare const scru160f: () => string;

@@ -10,12 +10,12 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.scru160V = exports.scru160 = void 0;
exports.scru160f = exports.scru160 = void 0;
const crypto_1 = require("crypto");
/**
* Generates a new SCRU-160 ID encoded in the hexadecimal format.
* Generates a new SCRU-160 ID encoded in the base32hex format.
*
* @returns 40-character hexadecimal string (`/^[0-9a-f]{40}$/`).
* @returns 32-character base32hexupper string (`/^[0-9A-V]{32}$/`).
*/
const scru160 = () => {
return generateQuad()
.map((x) => ("000000000" + x.toString(16)).slice(-10))
.map((x) => ("0000000" + x.toString(32).toUpperCase()).slice(-8))
.join("");

@@ -25,12 +25,12 @@ };

/**
* Generates a new SCRU-160 ID encoded in the base32hex format.
* Generates a new SCRU-160 ID encoded in the hexadecimal format.
*
* @returns 32-character base32hexupper string (`/^[0-9A-V]{32}$/`).
* @returns 40-character hexadecimal string (`/^[0-9a-f]{40}$/`).
*/
const scru160V = () => {
const scru160f = () => {
return generateQuad()
.map((x) => ("0000000" + x.toString(32).toUpperCase()).slice(-8))
.map((x) => ("000000000" + x.toString(16)).slice(-10))
.join("");
};
exports.scru160V = scru160V;
exports.scru160f = scru160f;
/**

@@ -37,0 +37,0 @@ * Generates a byte sequence that represents a new SCRU-160 ID.

{
"name": "scru160",
"version": "0.1.0",
"version": "0.2.0",
"description": "SCRU-160: Sortable, Clock and Random number-based Unique identifier",

@@ -32,2 +32,3 @@ "main": "./dist/index.js",

"keywords": [
"identifier",
"uuid",

@@ -34,0 +35,0 @@ "guid",

# 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
```javascript
import { scru160, scru160V } from "scru160";
import { scru160, scru160f } from "scru160";
console.log(scru160()); // e.g. "017bdf6431bb33750751eb63beb3c3f8c5969d86"
console.log(scru160()); // e.g. "017bdf6431bb337662412e6a5758890735c33c2b"
console.log(scru160()); // e.g. "05TTUP1HNCPNH30VEK64KDQT9BSNU4C4"
console.log(scru160()); // e.g. "05TTUP1HNCPNIB63R8IN5V2L3VFGNFET"
console.log(scru160V()); // e.g. "05TTUP1HNCPNH30VEK64KDQT9BSNU4C4"
console.log(scru160V()); // e.g. "05TTUP1HNCPNIB63R8IN5V2L3VFGNFET"
console.log(scru160f()); // e.g. "017bdf6431bb33750751eb63beb3c3f8c5969d86"
console.log(scru160f()); // e.g. "017bdf6431bb337662412e6a5758890735c33c2b"
```
[uuid]: https://en.wikipedia.org/wiki/Universally_unique_identifier
[ulid]: https://github.com/ulid/spec
[ksuid]: https://github.com/segmentio/ksuid
## 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 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]`.
[rfc 4648]: https://datatracker.ietf.org/doc/html/rfc4648
```javascript
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.
```javascript
scru160f(); // e.g. "017be95b2d6042db903a19a0974182ec7445188a"
```
The base32hex representations and hex representations cannot be mixed when
SCRU-160 IDs need to be lexicographically sortable.
## See Also
- [API Documentation](https://scru160.github.io/javascript/docs/)
- [Run tests on your browser](https://scru160.github.io/javascript/test/)
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