Skip to main content

Cryptographically secure random generator for strings, numbers, passwords, API keys, and bytes

Project description

CryptoStrings

A production-ready, cryptographically secure random generator for strings, numbers, and bytes with performance optimization for high-throughput applications.

Features

  • Security-First Design: Generate secure random bytes with configurable security levels (medium, high, paranoid)
  • Multiple Output Formats: Generate strings, numbers, API keys, passwords, and raw bytes
  • Performance Optimized: Uses an entropy pool for efficient generation of multiple values
  • Cross-Platform Compatibility: Works reliably across Windows, macOS, and Linux
  • Comprehensive Character Sets: Supports hex, base64, URL-safe, alphanumeric, and custom charsets
  • Production Ready: Input validation, error handling, and fallbacks for enterprise applications

Installation

Install from PyPI:

pip install CryptoStrings

Example usage

Refer to example.py. Simply run with CryptoStrings installed and watch the magic!

Basic Usage

from CryptoStrings.core import generate_bytes, generate_string, generate_number

# Generate 32 secure random bytes
random_bytes = generate_bytes(32)

# Generate a 16-character secure hex string
random_hex = generate_string(16, charset='hex')

# Generate a secure 128-bit random number
random_number = generate_number(128)

Security Levels

All functions accept a security_level parameter:

  • medium: Fast generation using an entropy pool (good for most applications)
  • high: More entropy sources combined for stronger security (default)
  • paranoid: Multiple rounds of hashing and maximum entropy gathering (slower but most secure)
# Generate high-security token
token = generate_string(32, charset='base64', security_level='high')

# Generate paranoid-level key for critical applications
critical_key = generate_bytes(64, security_level='paranoid')

Core Functions

Generating Bytes

from CryptoStrings.core import generate_bytes

# Generate 32 bytes (256 bits) of secure random data
key_bytes = generate_bytes(32)

# Generate with paranoid security for cryptographic keys
crypto_key = generate_bytes(32, security_level='paranoid')

Generating Strings

from CryptoStrings.core import generate_string

# Hexadecimal string (for IDs, hashes)
hex_id = generate_string(32, charset='hex')

# Base64 string (efficient encoding, good for tokens)
token = generate_string(64, charset='base64')

# URL-safe string (perfect for URLs, no special chars)
url_token = generate_string(32, charset='url_safe')

# Alphanumeric only (letters and numbers)
alnum = generate_string(16, charset='alphanumeric')

# ASCII letters only (A-Z, a-z)
letters = generate_string(12, charset='ascii')

# Digits only (0-9)
digits = generate_string(10, charset='digits')

# Printable ASCII (includes special characters)
complex = generate_string(20, charset='printable')

# Custom character set
custom = generate_string(16, charset='custom', custom_chars='ABCDEFGHJKLMNPQRSTUVWXYZ23456789')

Generating Numbers

from CryptoStrings.core import generate_number

# Generate 32-bit number (range: 0 to 2^32-1)
num32 = generate_number(32)

# Generate 64-bit number
num64 = generate_number(64)

# Generate 128-bit number (very large integer)
num128 = generate_number(128)

Advanced Functions

Password Generation

Generate strong passwords with configurable requirements:

from CryptoStrings.core import generate_password

# Generate password with default settings (16 chars, 1 digit, 1 special)
password = generate_password()

# Generate longer password with more requirements
strong_pass = generate_password(length=20, min_digits=3, min_special=3)

API Key Generation

Generate API keys with optional prefixes:

from CryptoStrings.core import generate_api_key

# Simple API key
api_key = generate_api_key(length=32)

# API key with prefix (format: "prefix.random_string")
user_api_key = generate_api_key(prefix="user_123", length=32)

Common Use Cases

Authentication Tokens

from CryptoStrings.core import generate_string

# Session token (URL-safe for cookies/headers)
session_token = generate_string(64, charset='url_safe')

# JWT secret
jwt_secret = generate_string(32, charset='base64')

Cryptographic Keys

from CryptoStrings.core import generate_bytes
import base64

# Generate AES-256 key
aes_key = generate_bytes(32, security_level='paranoid')

# Format for storage/transmission
b64_key = base64.b64encode(aes_key).decode('ascii')

Unique Identifiers

from CryptoStrings.core import generate_string

# Database record ID
record_id = generate_string(16, charset='hex')

# Short unique code (useful for URL shorteners, confirmation codes)
short_code = generate_string(8, charset='custom', 
                           custom_chars='ABCDEFGHJKLMNPQRSTUVWXYZ23456789')

Performance Considerations

CryptoStrings uses an internal entropy pool for improved performance when generating multiple values, especially useful in high-throughput web applications.

For maximum performance with acceptable security:

from CryptoStrings.core import generate_string

# Medium security is fastest while still being cryptographically secure
tokens = [generate_string(32, charset='url_safe', security_level='medium') 
          for _ in range(1000)]

Cross-Platform Notes

CryptoStrings automatically adapts to the platform it's running on:

  • On Unix-like systems (Linux, macOS), it can access /dev/urandom
  • On Windows, it uses the Windows Cryptographic API when available
  • Fallbacks ensure it works even when certain entropy sources are unavailable

Thread Safety

All functions are thread-safe and can be called concurrently from multiple threads.

Comparison with Other Libraries

|-----------------------------|-----------------|---------|-------|--------|
| Feature                     |  CryptoStrings  | Secrets |  UUID | Random |
|-----------------------------|-----------------|---------|-------|--------|
| Cryptographically secure    | ✅             | ✅      | ❌   | ❌     |
| Multiple security levels    | ✅             | ❌      | ❌   | ❌     |
| Performance optimization    | ✅             | ❌      | ❌   | ✅     |
| Password generation         | ✅             | 🤔      | ❌   | ❌     |
| API key generation          | ✅             | 🤔      | ❌   | ❌     |
| Custom character sets       | ✅             | ❌      | ❌   | 🤔     |

License

GPL-3.0 License


Author: Sammy Folkhome
Email: support@vincio.cc

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

cryptostrings-1.1.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cryptostrings-1.1.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file cryptostrings-1.1.1.tar.gz.

File metadata

  • Download URL: cryptostrings-1.1.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0

File hashes

Hashes for cryptostrings-1.1.1.tar.gz
Algorithm Hash digest
SHA256 6c53169a795458038236ba8d88ccd78e8084e90cc86cb83daac6ef9da1e72d6d
MD5 3b6fe34db3d19c6e76a6cf5f49112863
BLAKE2b-256 7750ade64cc79dfd69a33fe3b945b8437991a8cc637aef801a7040c3bdd8e146

See more details on using hashes here.

File details

Details for the file cryptostrings-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: cryptostrings-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0

File hashes

Hashes for cryptostrings-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e30fa8894d881c742b66dad0be9f0f7ac381aada371af2d74756db913b653ef
MD5 2b877dd0e114c272b129455af51e97ad
BLAKE2b-256 bdd65b1730a99fa8df12f8eac5b4139c14a786a23084e1d871a224d9cda07941

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