sethserver / Security

Secure Flask Secret Key Generator [2025]

By Seth Black Updated August 19, 2025

...

copyregen

Quickstart: Click "regen" to generate a new secret key, then click "copy" to copy it to your clipboard. The generated key is safe for use in .env files and command-line arguments.

Usage Examples

Flask Application (.env file)

# .env file
SECRET_KEY=your-generated-key-here
DATABASE_URL=postgresql://user:pass@localhost/dbname
DEBUG=False

Flask Application (Python)

import os
from flask import Flask
from dotenv import load_dotenv

load_dotenv()

app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')

# Or directly in config
app.config['SECRET_KEY'] = 'your-generated-key-here'

Command Line Export

$ export SECRET_KEY="your-generated-key-here"
$ python app.py

Frequently Asked Questions

What makes this different from a regular password generator?

This generator specifically avoids characters that can cause problems in environment variables, shell commands, and configuration files. Regular password generators often include characters like quotes, dollar signs, and backslashes that can break .env file parsing or cause command execution issues.

What characters are included in the generated keys?

The generator uses uppercase letters (A-Z), lowercase letters (a-z), numbers (0-9), underscores (_), and hyphens (-). These characters are universally safe across all shells, .env parsers, and configuration file formats.

How long should my Flask secret key be?

Flask recommends using secret keys that are at least 24 characters long. However, for better security, we recommend using 64 characters (the default) or more. Longer keys provide more entropy and are harder to brute-force.

Is it safe to use the same secret key in development and production?

No! Always use different secret keys for different environments. If your development key is compromised, it shouldn't affect your production environment. Generate separate keys for development, staging, and production.

How should I store my secret keys?

Best practices for storing secret keys:

  • Use environment variables loaded from .env files (never commit .env to version control)
  • Use secret management services like AWS Secrets Manager, HashiCorp Vault, or Azure Key Vault
  • For containerized apps, use Kubernetes Secrets or Docker Secrets
  • For CI/CD, use your platform's secret storage (GitHub Secrets, GitLab CI Variables, etc.)
What happens if I change my Flask secret key?

Changing your Flask secret key will invalidate all existing sessions and signed cookies. Users will be logged out and need to authenticate again. Any data signed with the old key (like remember-me tokens) will become invalid. Plan key rotation during maintenance windows and notify users if necessary.

Can I use this for other frameworks besides Flask?

Yes! These keys work for any framework or application that needs secure random strings. Common uses include Django's SECRET_KEY, Express.js session secrets, Rails secret_key_base, JWT signing secrets, and API keys.

Why not just use random characters from the keyboard?

Human-generated "random" strings are predictable and have low entropy. True randomness requires a cryptographically secure random number generator. This tool uses PCG32 seeded with crypto.getRandomValues() to ensure unpredictability.

Is this generator secure? Is my key sent over the internet?

Yes, it's secure. The entire key generation process happens in your browser using JavaScript. No data is sent to our servers or over the network. You can verify this by checking your browser's network tab or viewing the page source.

What are the seed and stream parameters?

These are parameters for the PCG32 random number generator. The seed initializes the generator's state, and the stream selects a specific sequence. For most users, the auto-generated values are perfect. Advanced users might use specific values for reproducible key generation in testing environments (not recommended for production).

Should I rotate my secret keys regularly?

Yes, periodic key rotation is a good security practice. Consider rotating keys:

  • Every 3-6 months as a routine practice
  • Immediately after a security incident
  • When team members with access leave the organization
  • After major application updates or infrastructure changes
Can I use special characters if my environment supports them?

While some environments handle special characters correctly, it's safer to stick with alphanumeric characters plus underscore and hyphen. This ensures your application remains portable and won't break when deployed to different environments or platforms.

How can I verify my .env file is being loaded correctly?

Add a debug print statement in your application to verify the key is loaded:

import os
secret_key = os.environ.get('SECRET_KEY')
print(f"Secret key loaded: {bool(secret_key)}")
print(f"Secret key length: {len(secret_key) if secret_key else 0}")

Never print the actual secret key value, even in development!

What's the difference between SECRET_KEY and other API keys?

SECRET_KEY is used internally by your application for cryptographic operations like signing cookies and generating tokens. API keys authenticate your application to external services. Both should be kept secret, but SECRET_KEY is typically more critical as it affects all user sessions and security tokens in your application.

Security Best Practices

For Developers

  • Never hardcode secret keys in your source code
  • Add .env to your .gitignore file immediately
  • Use different keys for different environments
  • Document where keys are stored (but not the keys themselves)
  • Implement key rotation procedures
  • Monitor for exposed keys in version control
  • Use secret scanning tools in your CI/CD pipeline
  • Encrypt keys at rest when possible
  • Limit access to production keys to essential personnel only
  • Audit key access and usage regularly

For DevOps Teams

  • Use centralized secret management solutions
  • Implement proper access controls and audit logging
  • Automate key rotation where possible
  • Use separate keys for each application instance
  • Never log or display secret keys in output
  • Ensure keys are transmitted only over encrypted channels
  • Implement break-glass procedures for emergency key access
  • Regular security audits of key management practices

Also check out the Strong Random Password Generator and Random Passphrase Generator for other security needs.

-Sethers

Know someone who'd appreciate this? Share it with them!