A simple, powerful, and secure token generator module written in Python. It uses the built-in secrets module to generate cryptographically strong random tokens, making it suitable for security-sensitive applications like API keys, session identifiers, password reset links, and more.
- 🔐 Cryptographically Secure: Uses
secretsmodule, not the predictablerandommodule. - ⚙️ Highly Configurable: Easily control the token length and the character sets used (uppercase, lowercase, digits, symbols).
- 📦 No External Dependencies: Works out-of-the-box with the Python standard library.
- modular Clean & Reusable: The core logic is separated into
token_generator.pyfor easy integration into your own projects. -
- Robust Error Handling: Prevents generation of a token if no character sets are enabled.
The project is intentionally split into two files to demonstrate a clean, modular approach:
token_project/
├── token_generator.py # The core module with the token generation logic.
├── main.py # An example script demonstrating how to use the module.
└── README.md # This file.
No external packages are required. Just clone the repository or download the files.
git clone https://github.com/sayam/Token-Gen.git
cd Token-GenImport the generate_token function from the token_generator.py module into your own project.
Here are a few examples demonstrating how to use the function:
from token_generator import generate_token
# 1. Generate a standard 16-character token (default settings)
api_key = generate_token()
print(f"API Key: {api_key}")
# Example Output: API Key: FwR8vK3aL7pZ9jN1
# 2. Generate a long, 32-character token for higher security
session_id = generate_token(length=32)
print(f"Session ID: {session_id}")
# Example Output: Session ID: mYc2xT6vQj9zEw4bHn8rGk5fD3aU7pWz
# 3. Generate a complex 20-character password including symbols
complex_password = generate_token(length=20, use_symbols=True)
print(f"Complex Password: {complex_password}")
# Example Output: Complex Password: J&e@8S)q$L#wP!zV(k<g
# 4. Generate a 6-digit numeric PIN code
pin_code = generate_token(length=6, use_uppercase=False, use_lowercase=False)
print(f"PIN Code: {pin_code}")
# Example Output: PIN Code: 815309
# 5. Generate a lowercase-only token for a unique URL slug
url_slug = generate_token(length=8, use_uppercase=False, use_digits=False)
print(f"URL Slug: {url_slug}")
# Example Output: URL Slug: kwefpojuTo see all the examples in action, you can run the main.py script directly from your terminal:
python main.pygenerate_token(
length: int = 16,
use_uppercase: bool = True,
use_lowercase: bool = True,
use_digits: bool = True,
use_symbols: bool = False
) -> strParameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
length |
int |
16 |
The desired length of the generated token. |
use_uppercase |
bool |
True |
Include uppercase letters (A-Z) in the character pool. |
use_lowercase |
bool |
True |
Include lowercase letters (a-z) in the character pool. |
use_digits |
bool |
True |
Include digits (0-9) in the character pool. |
use_symbols |
bool |
False |
Include punctuation symbols (!@#$%^&*...). |
Returns:
str: A securely generated random token string.
Raises:
ValueError: If all character set flags (use_uppercase,use_lowercase,use_digits,use_symbols) are set toFalse.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request