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.1.tar.gz (14.1 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.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tinygenkey-0.5.0.1.tar.gz
  • Upload date:
  • Size: 14.1 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.1.tar.gz
Algorithm Hash digest
SHA256 5dfe70d385d1ea55480f99f7ccec6e90eeb29711b32b28eb5c630ece0e3231bc
MD5 2d12297743b9a5cd1f75278de095bb70
BLAKE2b-256 abb274da67defb6a90eb6ac83bf1301b8767bfeffa14ad39ba911cc8118cbac3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinygenkey-0.5.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e8b486422ae2992818ee32e4ed11c484fb06d6f1c490fb98d5d70ebfe627d301
MD5 09856d6381874fc0da2cca481a61dacc
BLAKE2b-256 58f4dd9aaf88ce30b2ae4b17bef6400e4805a8361c00d9a9f81932a085fcb81b

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