From 13e61b7befee279063243f75fbeba61137d0b4d7 Mon Sep 17 00:00:00 2001 From: Tanishq Dubey Date: Tue, 5 Nov 2024 19:40:29 -0500 Subject: [PATCH] remove duplicate secret key handling --- app.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 2c829e4..143e50f 100644 --- a/app.py +++ b/app.py @@ -32,13 +32,21 @@ from steganography import embed_message, extract_message # Add this function to handle secret key persistence def get_or_create_secret_key(): + """Get existing secret key or create a new one""" secret_key_file = Path("secret.key") - if secret_key_file.exists(): - return secret_key_file.read_bytes() - else: - secret_key = os.urandom(24) - secret_key_file.write_bytes(secret_key) - return secret_key + try: + if secret_key_file.exists(): + logger.info("Loading existing secret key") + return secret_key_file.read_bytes() + else: + logger.info("Generating new secret key") + secret_key = os.urandom(32) # Use 32 bytes for better security + secret_key_file.write_bytes(secret_key) + return secret_key + except Exception as e: + logger.error(f"Error handling secret key: {e}") + # Fallback to a memory-only key if file operations fail + return os.urandom(32) DEFAULT_CONFIG = { "server": {"host": "0.0.0.0", "port": 5000}, @@ -221,9 +229,6 @@ limiter = Limiter( storage_uri="memory://", ) -# Generate a strong secret key at startup -app.secret_key = secrets.token_hex(32) - @app.before_request def before_request(): g.csp_nonce = secrets.token_hex(16)