Skip to content

Commit

Permalink
Merge pull request #122 from jschlyter/fernet_order
Browse files Browse the repository at this point in the history
Prefer raw key to FernetEncrypter
  • Loading branch information
rohe authored Apr 27, 2022
2 parents 5a8aa7c + 1723845 commit ec79b98
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exclude_lines = [

[tool.poetry]
name = "cryptojwt"
version = "1.8.1"
version = "1.8.2"
description = "Python implementation of JWT, JWE, JWS and JWK"
authors = ["Roland Hedberg <[email protected]>"]
license = "Apache-2.0"
Expand Down
14 changes: 7 additions & 7 deletions src/cryptojwt/jwe/fernet.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ def __init__(
):
Encrypter.__init__(self)

if password is not None:
if key is not None:
if not isinstance(key, bytes):
raise TypeError("Raw key must be bytes")
if len(key) != 32:
raise ValueError("Raw key must be 32 bytes")
self.key = base64.urlsafe_b64encode(key)
elif password is not None:
_alg = getattr(hashes, hash_alg)
# A bit special for SHAKE* and BLAKE* hashes
if hash_alg.startswith("SHAKE") or hash_alg.startswith("BLAKE"):
Expand All @@ -35,12 +41,6 @@ def __init__(
salt = as_bytes(salt) if salt else os.urandom(16)
kdf = PBKDF2HMAC(algorithm=_algorithm, length=32, salt=salt, iterations=iterations)
self.key = base64.urlsafe_b64encode(kdf.derive(as_bytes(password)))
elif key is not None:
if not isinstance(key, bytes):
raise TypeError("Raw key must be bytes")
if len(key) != 32:
raise ValueError("Raw key must be 32 bytes")
self.key = base64.urlsafe_b64encode(key)
else:
self.key = Fernet.generate_key()

Expand Down

0 comments on commit ec79b98

Please sign in to comment.