Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved short secret key message #21

Merged
merged 1 commit into from
Sep 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mirage/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from django.conf import settings
from django.utils.encoding import force_bytes, force_str

SHORT_SECRET_KEY: str = """
django-mirage-field key length (MIRAGE_SECRET_KEY or SECRET_KEY) must be longer than 32 characters!
"""


class BaseCipher:
def __init__(self, key, iv) -> None:
Expand Down Expand Up @@ -57,7 +61,7 @@ class Crypto:
def __init__(self, key=None, mode=None, iv=None):
if key is None:
key = getattr(settings, "MIRAGE_SECRET_KEY", None) or getattr(settings, "SECRET_KEY")
assert len(key) >= 32, "mirage key length must more than 32!"
assert len(key) >= 32, SHORT_SECRET_KEY
key = base64.urlsafe_b64encode(force_bytes(key))[:32]
if mode is None:
mode = getattr(settings, "MIRAGE_CIPHER_MODE", "ECB")
Expand Down