Skip to main content

A tiny, secure key generator.

Project description

On PyPI Downloads Total Downloads Python Version License

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


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.3.1.tar.gz (20.6 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.3.1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tinygenkey-0.3.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

Hashes for tinygenkey-0.3.1.tar.gz
Algorithm Hash digest
SHA256 8e2b23ec44eba4edbd03e300d87747bc963045c6a2052b7f52a4ea1f12edf3f8
MD5 f320e85b3e4a58076bef4c420dd4a2ba
BLAKE2b-256 46c4e3d9167a0915b64452a5440ac992910c7de669743b0544f79354eaa28c24

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinygenkey-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 20.4 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.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11563a7a16fcef85c0f9fe362e1d951a40d83c5679bde1c2c479c6f8e8371b0d
MD5 1febfbea815bd82ac73d5a6c7aae6678
BLAKE2b-256 b3d10b974c86588a5ab8f0eff54c6494120133c22532ffe7cf359aebffc88e2e

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