A tiny, secure key generator.
Project description
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 keyverify(key, **kwargs) -> KeyVerifyReport- Verify a keyformat(key, group_size=4, sep="-") -> str- Format with separators
Key - Single key with instance methods
create(**kwargs) -> Key- Create new instance with generated keyvalue: str- The key stringgen(**kwargs) -> str- Generate new key (updatesvalue)format(**kwargs) -> str- Format key (updatesvalue)verify(**kwargs) -> KeyVerifyReport- Verify this keyis_valid(**kwargs) -> bool- Quick validity check
Keys - Multiple keys with batch operations
create(count, **kwargs) -> Keys- Create with generated keysvalues: list[str]- List of key stringsgen(count, **kwargs) -> list[str]- Generate new keys (updatesvalues)format_all(group_size=4, sep="-") -> list[str]- Format all keysverify(**kwargs) -> list[KeyVerifyReport]- Verify all keysis_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 charactersalphabet: list[str] | None- Custom character setpreset: str = "alphanumeric"- Preset nameprefix: str = ""- String before keysuffix: str = ""- String after keyauto_format: bool = False- Auto-format on generationgroup_size: int = 4- Chars per group (when formatting)sep: str = "-"- Separator between groups
Verification:
alphabet_or_preset: list[str] | str | None- Expected charsetmin_length: int | None- Minimum core lengthmax_length: int | None- Maximum core lengthprefix: str | None- Expected prefixsuffix: 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tinygenkey-0.5.1.tar.gz.
File metadata
- Download URL: tinygenkey-0.5.1.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3943c520aec1adc1205d44a02ee501e33cb87c258bb85c28c3a030c1393a94e7
|
|
| MD5 |
cc3e18c6bde5fedc57bb46880854708d
|
|
| BLAKE2b-256 |
bd778fbff249dcbb59afc1f5032cdf2c79a936e8268805c4cdb55dc5ed1089d2
|
File details
Details for the file tinygenkey-0.5.1-py3-none-any.whl.
File metadata
- Download URL: tinygenkey-0.5.1-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
865f12e7218c389f5c7473ee3c63376a41703a45848cef211d4df86958d037ba
|
|
| MD5 |
ea3e6cf4af69fa24be04d836c8aabb7c
|
|
| BLAKE2b-256 |
957df01cb6abc21ef53d7702316e026862c97066edfd7bb7591cacafbf8f3519
|