Skip to main content

A True Random Number Generator using raw camera sensor data.

Project description

photorand

A True Random Number Generator (TRNG) using raw camera sensor data to extract physical entropy.

Project Structure

  • src/photorand/ - Main package source code.
    • low_level/ - Modular primitives (ingest, sample, hash, csprng).
    • high_level/ - Clean OO abstractions (PhotoRandSeed, PhotoRandEngine).
    • cli/ - Command-line interface logic.
  • docs/ - Technical and scientific documentation detailing the entropy extraction and entropy expansion mechanisms.
  • tests/ - Comprehensive test suite organized by architectural layer.

Installation

This project uses pyproject.toml for managing dependencies.

  1. Create and activate a virtual environment (recommended):

    python -m venv .venv
    source .venv/bin/activate  # On Windows, use `.venv\Scripts\activate`
    
  2. Install the project in editable mode:

    pip install -e .
    # or this one for also installing the dev tools
    pip install -e ".[dev]"
    

1. High-Level Classes (Recommended)

The high-level classes provide a stateful and convenient interface for both TRNG and CSPRNG operations.

PhotoRandSeed (TRNG)

Encapsulates the process of extracting entropy from a RAW image. It is perfect for generating one-off secure seeds, keys, or dice rolls directly from physical noise.

from photorand.high_level.seed import PhotoRandSeed

# 1. Extract entropy from a RAW image
seed = PhotoRandSeed("path/to/image.raw")

# 2. Access as bytes, hex, or large integer
print(seed.to_hex_string())
print(seed.to_int())

# 3. Roll a D100 using physical entropy (Rejection Sampling)
luck = seed.to_int_range(1, 100)

PhotoRandEngine (CSPRNG)

An infinite stream generator powered by ChaCha20, seeded by a PhotoRandSeed. It handles salting (Time + PID) automatically to ensure that even consecutive runs with the same image produce unique streams.

from photorand.high_level.engine import PhotoRandEngine

# 1. Initialize from image or existing Seed object
engine = PhotoRandEngine("path/to/image.raw")

# 2. Pull arbitrary amounts of secure data
key = engine.next_bytes(32)
pin = engine.next_string(length=6, charset='numeric')
dice = engine.next_int_range(1, 20)

# 3. Batch generation
multiple_passwords = engine.generate_batch(engine.next_string, count=5, length=16)

CLI (Command Line Interface)

The package includes a powerful CLI to use these classes directly from your terminal.

# Extract 64-byte photorand seed (hex)
python -m photorand extract path/to/raw_image.ARW

# Roll a D20
python -m photorand extract path/to/raw_image.ARW --format range --min 1 --max 20

# Generate 5 random 16-char alphanumeric passwords
python -m photorand generate path/to/raw_image.ARW --type string -n 5 -l 16 --charset alpha

For more details, run: python -m photorand --help


2. Low-Level Modular Functions

For maximum control or research, you can use the modular primitives directly.

from photorand.low_level.ingest import ingest_raw_image
from photorand.low_level.sample import sample_entropy_grid
from photorand.low_level.hash import hash_entropy_pool

# 1. Ingest raw sensor data
raw_data = ingest_raw_image("path/to/image.raw")

# 2. Extract raw LSB bits
entropy_pool = sample_entropy_grid(raw_data)

# 3. Condition entropy into uniform bytes
seed_bytes = hash_entropy_pool(entropy_pool)

Alternatively, use the functional pipeline. It accepts custom functions for each part of the algorithm (ingest_fn, sample_fn and hash_fn) although we already provide the values as default parameters:

from photorand.low_level.generate import generate_true_random_number
seed = generate_true_random_number("path/to/image.raw")

Development

Running the Tests

python -m pytest -v --log-cli-level=INFO
# or just this one to skip logs
pytest

Formatting

ruff check --fix src tests examples

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

photorand-1.0.2.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

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

photorand-1.0.2-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file photorand-1.0.2.tar.gz.

File metadata

  • Download URL: photorand-1.0.2.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for photorand-1.0.2.tar.gz
Algorithm Hash digest
SHA256 99c3c096358eba2d0c59bf35dfe43e781aa46127132fa079b12e17042bb7a200
MD5 5231cab3f413c4da8fade10a917c8ef8
BLAKE2b-256 95be84f1740e571ca8fde086803193269e66f78ae06da172ab39dbce342964c2

See more details on using hashes here.

File details

Details for the file photorand-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: photorand-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for photorand-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 175501ea0f9ec634ac867f84eeef26d14375e8ae3b4d42ba507dad0a0f274508
MD5 7c84f1c13461979e48890f37e672eeb5
BLAKE2b-256 9cb3890a9f81262b59e692de7e187ccf54455ff84401ce5da390bae9d6ef1d1f

See more details on using hashes here.

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