Skip to main content

A tiny, secure key generator.

Project description

On PyPI Downloads Python Version License

TinyGenKey

Cryptographically secure key generation in Python. Tiny, simple, and dependency-free.

Installation

pip install tinygenkey

Quick Start

import tinygenkey as tgk

# Generate a key (default: 42 alphanumeric chars)
key = tgk.BaseKey.gen()

# With options
api_key = tgk.BaseKey.gen(
    length=32,
    prefix="api_",
    preset="hex"
)

Beginner Usage

Functional API

# Simple generation
key = tgk.BaseKey.gen(length=16)

# With prefix/suffix
token = tgk.BaseKey.gen(
    length=24,
    prefix="tok_",
    suffix="_v1"
)

# Custom alphabet
pin = tgk.BaseKey.gen(
    length=6,
    alphabet=list("0123456789")
)

# Using presets
hex_key = tgk.BaseKey.gen(length=32, preset="hex")
safe_key = tgk.BaseKey.gen(length=20, preset="safe")  # URL-safe

Object-Oriented API

# Single key
key = tgk.Key.create(length=32, preset="hex")
print(key.value)  # Access the key
print(key)        # Same as key.value

# Multiple keys
keys = tgk.Keys.create(count=5, length=16, prefix="user_")
print(keys.values)  # List of keys
print(keys)         # Newline-separated output

Advanced Usage

Formatting Keys

# Format for readability
key = tgk.BaseKey.gen(length=16)
formatted = tgk.BaseKey.format(key, group_size=4, sep="-")
# Output: "a3f7-9d2e-4b1c-8f6a"

# Auto-format on generation
key = tgk.BaseKey.gen(
    length=16,
    auto_format=True,
    group_size=4,
    sep="-"
)

# Format with Key class
key = tgk.Key.create(length=20)
key.format(group_size=5, sep=".")
print(key.value)  # "ab3f9.d2e4b.1c8f6.a9e2d"

Verification

# Verify a single key
report = tgk.BaseKey.verify(
    "api_a3f79d2e4b1c_key",
    preset="hex",
    min_length=10,
    max_length=20,
    prefix="api_",
    suffix="_key"
)

if report["valid"]:
    print("Valid!")
else:
    print(f"Invalid: {report['reasons']}")
    print(f"Hints: {report['hints']}")

# Using Key class
key = tgk.Key("api_abc123_key")
if key.is_valid(preset="hex", prefix="api_", suffix="_key"):
    print("Valid!")

# Verify multiple keys
keys = tgk.Keys(["key1", "key2", "key3"])
reports = keys.verify(preset="alphanumeric", min_length=4)
validity = keys.is_valid(preset="hex")  # [True, False, True]

Nested/Hierarchical Keys

# Complex structured keys
session_id = tgk.BaseKey.gen(
    prefix=f"sess_{tgk.BaseKey.gen(length=8, preset='hex')}_",
    length=32,
    suffix=f"_{tgk.BaseKey.gen(length=4, preset='numbers')}"
)
# Result: sess_a3f79d2e_<32 chars>_4182

Available Presets

# Common presets
"alphanumeric"  # a-z, A-Z, 0-9 (default)
"hex"           # 0-9, a-f
"base64"        # Base64 character set
"safe"          # URL-safe: a-z, A-Z, 0-9, -, _
"lowercase"     # a-z
"uppercase"     # A-Z
"numbers"       # 0-9
"password"      # Letters, numbers, symbols

# Specialty presets
"binary"        # 0, 1
"octal"         # 0-7
"dna"           # A, T, G, C
"emoji"         # Common emoji set
"morse"         # Dots, dashes, spaces

# Access preset character sets
tgk.PRESETS["hex"]  # ['0','1',...,'f']
tgk.HEX_LIST        # Same as above

API Reference

Classes

BaseKey - Static methods for key generation

  • gen(**kwargs) -> str - Generate a key
  • verify(key, **kwargs) -> KeyVerifyReport - Verify a key
  • format(key, group_size=4, sep="-") -> str - Format with separators

Key - Single key with instance methods

  • create(**kwargs) -> Key - Create new instance with generated key
  • value: str - The key string
  • gen(**kwargs) -> str - Generate new key (updates value)
  • format(**kwargs) -> str - Format key (updates value)
  • verify(**kwargs) -> KeyVerifyReport - Verify this key
  • is_valid(**kwargs) -> bool - Quick validity check

Keys - Multiple keys with batch operations

  • create(count, **kwargs) -> Keys - Create with generated keys
  • values: list[str] - List of key strings
  • gen(count, **kwargs) -> list[str] - Generate new keys (updates values)
  • format_all(group_size=4, sep="-") -> list[str] - Format all keys
  • verify(**kwargs) -> list[KeyVerifyReport] - Verify all keys
  • is_valid(**kwargs) -> list[bool] - Validity for each key

Functions

keys_gen(**kwargs) -> str - Alias for BaseKey.gen()

gen_key(**kwargs) -> str - Legacy function (deprecated, use BaseKey.gen())

Parameters

Generation:

  • length: int = 42 - Number of random characters
  • alphabet: list[str] | None - Custom character set
  • preset: str = "alphanumeric" - Preset name
  • prefix: str = "" - String before key
  • suffix: str = "" - String after key
  • auto_format: bool = False - Auto-format on generation
  • group_size: int = 4 - Chars per group (when formatting)
  • sep: str = "-" - Separator between groups

Verification:

  • alphabet_or_preset: list[str] | str | None - Expected charset
  • min_length: int | None - Minimum core length
  • max_length: int | None - Maximum core length
  • prefix: str | None - Expected prefix
  • suffix: str | None - Expected suffix

Security

Uses os.urandom() for cryptographically secure randomness. Suitable for API keys, tokens, session IDs, and secrets.

License

MIT License - See GitHub

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

tinygenkey-0.5.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

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

tinygenkey-0.5.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file tinygenkey-0.5.0.tar.gz.

File metadata

  • Download URL: tinygenkey-0.5.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for tinygenkey-0.5.0.tar.gz
Algorithm Hash digest
SHA256 2f37a717d4651773cbce315c7f3ae460a92e82bc56db7714f89bad0df8fc6264
MD5 9034295af8dd877e96c7205c66877e09
BLAKE2b-256 f12f9439516c8ae6557c0511d7b341f0c7db8a787ce882f44ec10a9872583deb

See more details on using hashes here.

File details

Details for the file tinygenkey-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: tinygenkey-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for tinygenkey-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 770562f2cabed79b449886e2763fda5b3d23b67a0f5a22a8298843872fffa06f
MD5 02b4040ebedd0f1fef52804b9713c8f4
BLAKE2b-256 a1cbb2ad8f329907f1d5bf81ff9777e24295f93d305bc7d0b146858ed8878796

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