Skip to main content

otp-text

One-Time Pad (OTP) encryption for Unicode text — pure text in, pure text out.

Everything the caller needs to persist is a printable string (space-separated hex tokens). No files, no binary blobs, no external dependencies.

⚠️ Read this first

This is a correct but educational / utility implementation of the One-Time Pad. It follows Shannon's rules faithfully:

  1. The key is truly random (secrets, backed by os.urandom).
  2. The key is at least as long as the message (enforced).
  3. The key is used only once (your responsibility).
  4. The key stays secret (your responsibility).

Do not use this to protect real secrets unless you fully understand the One-Time Pad threat model — key distribution, key reuse, and side channels are all outside the scope of this library. For production cryptography, use cryptography with a modern AEAD cipher (e.g. AES-GCM or ChaCha20-Poly1305).

Install

pip install otp-text

No dependencies. Pure Python 3.9+.

Quick start

import otp_text

plaintext = "Hello, 世界! 🌍"
key = otp_text.key_for(plaintext)   # one key per message, sized exactly

ciphertext = otp_text.encrypt(plaintext, key)
recovered  = otp_text.decrypt(ciphertext, key)

assert recovered == plaintext

API

generate_key(length: int) -> str

Generate a fresh random key of length symbols. Returns a space-separated hex-token string. Each symbol is drawn uniformly from [0, 0x110000).

key_for(plaintext: str) -> str

Convenience: generate_key(len(plaintext)).

encrypt(plaintext: str, key: str) -> str

Encrypt with the additive Vernam scheme:

C_i = (P_i + K_i) mod 0x110000

where P_i is the Unicode code point of the i-th character.

decrypt(ciphertext: str, key: str) -> str

Inverse of encrypt:

P_i = (C_i - K_i) mod 0x110000

describe_key(key: str) -> dict

Return metadata about a key string without revealing the symbols:

>>> otp_text.describe_key(otp_text.generate_key(5))
{'length': 5, 'hex_chars': 30, 'valid': True, 'error': None}

symbols_to_text(symbols: list[int]) -> str

text_to_symbols(text: str) -> list[int]

Low-level converters between integer symbol lists and hex-token strings.

Serialization format

A key or ciphertext is a string of hex tokens separated by whitespace. Each token is exactly 6 lowercase hex digits, zero-padded, so tokens line up visually and are easy to split:

00a1f3 10ffff 000001 …

0x10FFFF is the highest Unicode code point, so 6 hex digits is always enough.

License

MIT — see LICENSE.#\x00 \x00p\x00y\x00-\x00o\x00t\x00p\x00 \x00 \x00

Metadata

Release files for opt-text 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opt-text 0.1.0
File Size Uploaded Provenance Actions
opt_text-0.1.0.tar.gz 7.2 kB Without provenance Details for opt_text-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for opt-text 0.1.0
File Interpreter ABI Platform Actions
opt_text-0.1.0-py3-none-any.whl Python 3 none any Details for opt_text-0.1.0-py3-none-any.whl

Total release size: 14.1 kB

Release files / opt_text-0.1.0.tar.gz

Download URL opt_text-0.1.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0090f4522ea72d9e98c54da31ad5658c2dd307b6fdd256d509e183ca60cd2073
BLAKE2b-256 checksum
How to use checksums
8a787952d391ae0d720ca80d5f0a3be965ac0a6cccbc9c66224308bad65079da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / opt_text-0.1.0-py3-none-any.whl

Download URL opt_text-0.1.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4154fa5e8fa44ab74c58c41f926ee933e62dcde1d13ca7e79a79e41747c7133
BLAKE2b-256 checksum
How to use checksums
3f2ffc102ef9f41bd65c6a0fedac56f083d5022e35c610de70ce3ad0027c0a85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Security for opt-text 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page