A tiny, secure key generator.
Project description
TinyGenKey
A tiny, secure, and easy-to-use key generator in Python. Generate cryptographically strong keys for your needs.
Features
- Cryptographically secure keys using
os.urandom - Customizable alphabet, length, prefix, and suffix
- Lightweight and dependency-free
- Type-hinted for modern Python
Installation
pip install tinygenkey
Usage
import tinygenkey as tgk
# Generate a default 42-character key
key = tgk.gen_key()
print(key)
# Generate a key with a prefix and suffix
key2 = tgk.gen_key(prefix="user_", suffix="_end", length=16)
print(key2)
# Use a custom alphabet
custom_alphabet = list("ABC123")
key3 = tgk.gen_key(alphabet=custom_alphabet, length=8)
print(key3)
keys_seq()
This function returns a list of keys, determined with argument count and customized with **kwargs.
api_key = tgk.keys_seq(5) # Generates 5 keys
new_tokens = tgk.keys_seq(5, prefix="new_", suffix=f"_{tgk.keys_gen(length=6)}", preset="hex")
# Align with keys_align()
print(tgk.keys_align(new_tokens))
keys_align()
Separates a list of tokens with a passed separator argument (default is newline \n). Examples could be seen on keys_seq()
keys_verify()
A powerful tool to validate keys against character sets, length requirements, and more:
result = tgk.keys_verify(
"api_abc123_key",
preset="hex",
min_length=8,
max_length=20,
prefix="api_",
suffix="_key"
)
if result["valid"]:
print("[v] Valid key!")
else:
print("[x] Issues found:")
for reason in result["reasons"]:
print(f" - {reason}")
if result["hints"]:
print("\nHints:")
for hint in result["hints"]:
print(f" {hint}")
Advanced Usage
Recursive Key Generation
Create structured, hierarchical keys by cleanly nesting keys_gen() (or outdated gen_key()) calls:
api_key = tgk.keys_gen(
prefix=f"api_{tgk.gen_key(length=8)}_",
length=32,
suffix=f"_{tgk.gen_key(length=4)}"
)
# Result = api_<8 random chars>_<32 random>_<4 random>
# Organized and polished identifiers!
This composable design allows you to build complex key structures while maintaining cryptographic security at every level.
Why This Is Better Than secrets
With Python's built-in secrets module:
# pretend we imported secrets and have an alphanumeric
# list of characters
# awkward..
secrets_key = (
f"api_"
f"{''.join(secrets.choice(chars) for _ in range(8))}_"
f"{''.join(secrets.choice(chars) for _ in range(32))}"
f"_{''.join(secrets.choice(chars) for _ in range(8))}"
)
With tinygenkey
import tinygenkey as tgk
# Much better
tgk_key = tgk.keys_gen(
prefix=f"api_{tgk.gen_key(length=8)}_",
length=32,
suffix=f"_{tgk.gen_key(length=4)}"
)
Cleaner, more readable, easier to maintain.
!!!! WARNING Syntax Migration !!!!
In version 0.2.0, the syntax for gen_key() was replaced by a more powerful and consistent keys_gen(). You may still use gen_key() as usual, but it is deprecated.
On version stable 0.5.0, gen_key() will be deleted, but optional deprecated-supported versions will still be maintained as version >0.5-dep and is NOT preserved in minor updates.
By version 1.0.0, gen_key() will be removed entirely and deprecated-supported versions will stop being maintained.
Save Your Program
If your program cannot be modified and is very important for others, you may ask for a special version that supports gen_key().
GitHub
This project is open-sourced at GitHub: tinygenkey on GitHub
You may further verify the security of the code yourself.
License
This project is protected by the MIT License.
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.3.1.1.tar.gz.
File metadata
- Download URL: tinygenkey-0.3.1.1.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bc6783b8a0f3c4c132166304d62ed01a5d8fb2cd95671e4f17604e0f22e004a
|
|
| MD5 |
066344aa7c77796b15da9b9765aa4036
|
|
| BLAKE2b-256 |
1f9e017cc79f6b17cd6713630e4041edce3a36ddcd07e96c98ab6232cae0bd35
|
File details
Details for the file tinygenkey-0.3.1.1-py3-none-any.whl.
File metadata
- Download URL: tinygenkey-0.3.1.1-py3-none-any.whl
- Upload date:
- Size: 20.5 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 |
a6dc10fd47df3afbb72d3caf555dc5ec6dcb8736ffcfb9020b902a36f761aef5
|
|
| MD5 |
5b74bd8c78608faf662e5e1e404e0a91
|
|
| BLAKE2b-256 |
d393d00d0d935fc8fff959fde7fdfe381b29e7ca5b9f2add1fec2c0a0527f8d4
|