Skip to main content

ZID is a unique identifier with nice properties

Project description

ZID

PyPI - Python Version Liberapay Patrons GitHub Sponsors

zid is a unique identifier with nice properties:

  • It behaves like a 64-bit signed integer, so it can be safely used with external software, e.g., in a database. ZIDs will never overflow into negative values.

  • ZIDs are numerically sortable since the timestamp is stored in the most significant bits. Additional randomness is stored only in the least significant bits.

  • The specification is very simple, reducing the potential for bugs and making ZIDs highly efficient to generate and parse. Scroll down for the installation-free copy-and-paste code - it's that short!

  • CSPRNG-initialized sequence numbers enhance the privacy of the generated identifiers while remaining collision-resistant. You can generate up to 65,536 ZIDs within the same millisecond timestamp on a single machine.

Installation

The recommended installation method is through the PyPI package manager. The project is implemented in Rust, offering excellent performance characteristics. Several pre-built binary wheels are available for Linux, macOS, and Windows, with support for both x64 and ARM architectures.

pip install zid

Installation (copy & paste)

Alternatively, you can copy and paste the following code for an installation-free ZID generator. This code excludes performance optimizations and utility methods for the sake of simplicity and portability:

from os import urandom
from time import time_ns

_last_time: int = -1
_last_sequence: int = -1

def zid() -> int:
    global _last_time, _last_sequence

    # UNIX timestamp in milliseconds
    time: int = time_ns() // 1_000_000
    if time > 0x7FFF_FFFF_FFFF:
        raise OverflowError('Time value is too large')

    # CSPRNG-initialized sequence numbers
    sequence: int
    if _last_time == time:
        _last_sequence = sequence = (_last_sequence + 1) & 0xFFFF
    else:
        _last_sequence = sequence = int.from_bytes(urandom(2))
        _last_time = time

    return (time << 16) | sequence

Basic usage

from zid import zid
zid()  # -> 112723768038396241
zid()  # -> 112723768130153517
zid()  # -> 112723768205368402

from zid import zids
zids(3)
# -> [113103096068704205, 113103096068704206, 113103096068704207]

from zid import parse_zid_timestamp
parse_zid_timestamp(112723768038396241)
# -> 1720028198828 (UNIX timestamp in milliseconds)

Format specification

ZID is 64 bits long in binary. Only 63 bits are used to fit in a signed integer. The first 47 bits are a UNIX timestamp in milliseconds. The remaining 16 bits are CSPRNG-initialized sequence numbers.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|0|                     timestamp (31 bits)                     |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      timestamp (16 bits)      |   random+sequence (16 bits)   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Limitations

  • Timestamps support years from 1970 to approx. 6429. To verify this, you can follow the formula 1970 + (2^47 − 1) / 1000 / (3600 * 24) / 365.25

  • If several ZIDs are generated with the same millisecond timestamp, knowing one of them will allow you to discover the others due to linearly increasing sequence numbers. Otherwise, guessing ZID values is difficult (but not impossible) due to the millisecond precision of the timestamp and the additional 16 bits of entropy. Do not rely on ZIDs alone for your security!

  • You can generate up to 65,536 ZIDs within the same millisecond timestamp on a single machine. With two separate machines, you will generate on average 16,384 ZIDs each before a collision occurs within the same millisecond timestamp. With three separate machines, the average number is 10,240 ZIDs each.

  • ZIDs are not strictly increasing within the same millisecond timestamp due to the possible sequence number overflow.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zid-1.3.0.tar.gz (23.0 kB view details)

Uploaded Source

Built Distributions

zid-1.3.0-cp39-abi3-win_amd64.whl (106.2 kB view details)

Uploaded CPython 3.9+ Windows x86-64

zid-1.3.0-cp39-abi3-musllinux_1_2_x86_64.whl (401.4 kB view details)

Uploaded CPython 3.9+ musllinux: musl 1.2+ x86-64

zid-1.3.0-cp39-abi3-musllinux_1_2_aarch64.whl (418.7 kB view details)

Uploaded CPython 3.9+ musllinux: musl 1.2+ ARM64

zid-1.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (220.7 kB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ x86-64

zid-1.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (210.7 kB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ ARM64

zid-1.3.0-cp39-abi3-macosx_11_0_arm64.whl (202.9 kB view details)

Uploaded CPython 3.9+ macOS 11.0+ ARM64

zid-1.3.0-cp39-abi3-macosx_10_12_x86_64.whl (207.5 kB view details)

Uploaded CPython 3.9+ macOS 10.12+ x86-64

File details

Details for the file zid-1.3.0.tar.gz.

File metadata

  • Download URL: zid-1.3.0.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for zid-1.3.0.tar.gz
Algorithm Hash digest
SHA256 a0656fec82da66f9c0f07d09c19470ddef887c878ba8c3d5f7d210b7766de786
MD5 97037ee7e94fe78475fd88a541384e6f
BLAKE2b-256 369f59b447652aa62bab4076430cfabb8d35ed14cf0f4fe4ab4cdf9c870d7d78

See more details on using hashes here.

File details

Details for the file zid-1.3.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: zid-1.3.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 106.2 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for zid-1.3.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 85d82e16ba90f44fe8b55434cf2cca286b4c1847d4235fdc72e067b2bbaa5cda
MD5 5c621391018b4560c28a7e860be25181
BLAKE2b-256 a9750d07236016b8d6767ca70cd42697a2a6877b45413817c1b6d836157cd58f

See more details on using hashes here.

File details

Details for the file zid-1.3.0-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for zid-1.3.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 69d6baa740ff71bb19caa4a24031f5889a8a24734a300e11be3b113c5ab71959
MD5 d7be948b2bb7ac16143e14528e479e90
BLAKE2b-256 9440fa976b60cff6b387d3c528c43c16da441bf77fd94c6965a90403cb2ae3af

See more details on using hashes here.

File details

Details for the file zid-1.3.0-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for zid-1.3.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 9e9369b2cb54a0ba09106eb4f6ebf337f3cc61fcabdfbec13688ae6ff042676d
MD5 22d3df76e7475a06c624c4e10ffa7116
BLAKE2b-256 c724ed61c4ab7e7f42cb35082a3407c8fc0014c8021686411f620868b6b6281b

See more details on using hashes here.

File details

Details for the file zid-1.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zid-1.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 20dcf553c7eaf430e70203339b7477631ae7d5baabda530dc3587ea78a7cc608
MD5 de2c4bb5743a4311cb3d260e9eb2f7c5
BLAKE2b-256 f909638cb46064c7de1feb9b701ed480956ba73961dea218983624e38cc3b143

See more details on using hashes here.

File details

Details for the file zid-1.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for zid-1.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b1a9b93b4ac9635591e5e6e61ce98f10685d903b20eaf756697e71187e500b2a
MD5 1d2857c45a652bc9b517cd657c07c991
BLAKE2b-256 0db4ded92205a3a4185be6f0e5aff7a01a88431a53b9566f71e9e22ff9377d35

See more details on using hashes here.

File details

Details for the file zid-1.3.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: zid-1.3.0-cp39-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 202.9 kB
  • Tags: CPython 3.9+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for zid-1.3.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 effeacd9d543accd0b785a5fc107207e0e74afc28e97dece871dea85533f2e30
MD5 c8b9913429af2f3f5e804d0a37b55b7a
BLAKE2b-256 4f2866e37d0c2797b39c0d6eb2bd2ed3fd6b3fe1c6ac99a9daf8adb6a0f5c2fc

See more details on using hashes here.

File details

Details for the file zid-1.3.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for zid-1.3.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9d43a8394a8cdfbcd39490294373cf6aaa3a655a894e09c90970f2027b7f66ab
MD5 8c5ad32d5dfb5a1cc0e1ef7c9eee64ab
BLAKE2b-256 78a2f830bb9d9d29f1e1dd532fc93efc01d43f3d0a2912e20a244bbf73885f9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page