Skip to main content

A Python implementation the Randflake ID: a distributed, uniform, unpredictable, unique random ID generator.

Project description

GitHub GitHub tag (latest SemVer) PYPI npm Go Reference

Randflake ID

Overview

Randflake ID is a distributed, uniform, unpredictable, and unique random ID generator designed to provide robust identifier generation across multiple programming languages and environments.

Key Features

  • 🌐 Multi-Language Support: Available in Go, Python, and TypeScript/JavaScript
  • 🔒 Cryptographically Secure: Generates unpredictable and unique identifiers
  • 🚀 High-Performance ID Generation: Optimized for lock-free operation to minimize latency and maximize throughput
  • 📈 Scalable: Engineered to handle high-throughput systems with a maximum ID generation rate of 17,179,869,184 ID/s
  • 🔀 Distributed-Friendly: Suitable for distributed systems and microservices
  • 📊 Uniform Distribution: Ensures even spread of generated IDs

Installation

Go

go get -u gosuda.org/randflake

Python

pip install randflake

TypeScript/JavaScript

npm install randflake

Usage Examples

Go

package main

import (
    "fmt"
    "time"

    "gosuda.org/randflake"
)

func main() {
    now := time.Now().Unix()

    nodeid := int64(42)
    lease_start := int64(now)
    lease_end := int64(now + 600)
    secret := []byte("super-secret-key")

    g, err := randflake.NewGenerator(nodeid, lease_start, lease_end, secret)
    if err != nil {
        panic(err)
    }

    id, err := g.Generate()
    if err != nil {
        panic(err)
    }
    fmt.Println(id)
}

Python

import time
from randflake import Generator

now = int(time.time())

nodeid = 42
lease_start = now
lease_end = now + 600
secret = b'super-secret-key'

g = Generator(nodeid, lease_start, lease_end, secret)

uid = g.generate()
print(uid)

TypeScript/JavaScript

import { Generator } from 'randflake';

const now = Math.floor(Date.now() / 1000);

const nodeid = 42;
const lease_start = now;
const lease_end = now + 600;
const secret = new TextEncoder().encode('super-secret-key');

const generator = new Generator(nodeid, lease_start, lease_end, secret);
const uid = generator.generate();
console.log(uid);

Performance

Randflake ID is designed for high-performance scenarios, with minimal overhead in ID generation.

String Representation

Randflake ID is encoded as a base32hex string.

base32hexchars = "0123456789abcdefghijklmnopqrstuv"

original = 4594531474933654033
encoded = "3vgoe12ccb8gh"

def decode(s):
    return int(s, 32)

def encode(n):
    if n < 0:
        n += 1 << 64
    
    if n == 0:
        return "0"
    
    result = ""
    while n > 0:
        result = base32hexchars[n&0x1f] + result
        n = n // 32
    return result

assert original == decode(encode(original))
assert encode(original) == "3vgoe12ccb8gh"

Cross-Language Test Vectors

The repository root contains test_vectors.json, generated by the Go test suite and consumed by the Go, Python, and TypeScript tests. Each vector records the secret, lease, timestamp, node ID, sequence, raw bit-packed ID, encrypted ID, and base32hex encoded ID.

The raw_id and encrypted_id values are JSON strings, not JSON numbers, so 64-bit values round-trip safely in JavaScript/TypeScript and other IEEE 754-number environments.

To regenerate the canonical vectors after an intentional algorithm change:

go test . -run TestRandflakeTestVectors -update-test-vectors

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

More Information

For detailed documentation and additional resources, visit: https://gosuda.org/randflake

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

randflake-1.8.0.tar.gz (58.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

randflake-1.8.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file randflake-1.8.0.tar.gz.

File metadata

  • Download URL: randflake-1.8.0.tar.gz
  • Upload date:
  • Size: 58.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for randflake-1.8.0.tar.gz
Algorithm Hash digest
SHA256 7b0bea33a53ad77e8e68f74594be4e4fbe72681a62905f4730df2a8ba95bce6e
MD5 00d28d22799e239222db204e317b13ff
BLAKE2b-256 580aa1d2b24b21d9fae70b81d38e25e3211bd34590cbb1ad6128597b5cdc1b1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for randflake-1.8.0.tar.gz:

Publisher: python-publish.yml on gosuda/randflake

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file randflake-1.8.0-py3-none-any.whl.

File metadata

  • Download URL: randflake-1.8.0-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for randflake-1.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95a83fb0d791796786fd7cd24e2e393e30bcaf7e9177729cd9258135a0a20d16
MD5 f0a38ff94c998bdfaebb5b0302d8f32a
BLAKE2b-256 5fa2cdf0890d0bba7b182a2a7421d6d1515bb8817ccf9a67ff7716a1fd39ebb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for randflake-1.8.0-py3-none-any.whl:

Publisher: python-publish.yml on gosuda/randflake

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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