Skip to content

Commit 5a8aa7c

Browse files
authored
Merge pull request #121 from jschlyter/fernet_key
Fernet raw key
2 parents 6a3adab + d4908be commit 5a8aa7c

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exclude_lines = [
2222

2323
[tool.poetry]
2424
name = "cryptojwt"
25-
version = "1.8.0"
25+
version = "1.8.1"
2626
description = "Python implementation of JWT, JWE, JWS and JWK"
2727
authors = ["Roland Hedberg <[email protected]>"]
2828
license = "Apache-2.0"

src/cryptojwt/jwe/fernet.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,34 @@
1616
class FernetEncrypter(Encrypter):
1717
def __init__(
1818
self,
19-
password: str,
19+
password: Optional[str] = None,
2020
salt: Optional[bytes] = "",
21+
key: Optional[bytes] = None,
2122
hash_alg: Optional[str] = "SHA256",
2223
digest_size: Optional[int] = 0,
2324
iterations: Optional[int] = DEFAULT_ITERATIONS,
2425
):
2526
Encrypter.__init__(self)
26-
if not salt:
27-
salt = os.urandom(16)
28-
else:
29-
salt = as_bytes(salt)
3027

31-
_alg = getattr(hashes, hash_alg)
32-
# A bit special for SHAKE* and BLAKE* hashes
33-
if hash_alg.startswith("SHAKE") or hash_alg.startswith("BLAKE"):
34-
_algorithm = _alg(digest_size)
28+
if password is not None:
29+
_alg = getattr(hashes, hash_alg)
30+
# A bit special for SHAKE* and BLAKE* hashes
31+
if hash_alg.startswith("SHAKE") or hash_alg.startswith("BLAKE"):
32+
_algorithm = _alg(digest_size)
33+
else:
34+
_algorithm = _alg()
35+
salt = as_bytes(salt) if salt else os.urandom(16)
36+
kdf = PBKDF2HMAC(algorithm=_algorithm, length=32, salt=salt, iterations=iterations)
37+
self.key = base64.urlsafe_b64encode(kdf.derive(as_bytes(password)))
38+
elif key is not None:
39+
if not isinstance(key, bytes):
40+
raise TypeError("Raw key must be bytes")
41+
if len(key) != 32:
42+
raise ValueError("Raw key must be 32 bytes")
43+
self.key = base64.urlsafe_b64encode(key)
3544
else:
36-
_algorithm = _alg()
37-
kdf = PBKDF2HMAC(algorithm=_algorithm, length=32, salt=salt, iterations=iterations)
38-
self.key = base64.urlsafe_b64encode(kdf.derive(as_bytes(password)))
45+
self.key = Fernet.generate_key()
46+
3947
self.core = Fernet(self.key)
4048

4149
def encrypt(self, msg: Union[str, bytes], **kwargs) -> bytes:

tests/test_07_jwe.py

+40-4
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,46 @@ def test_invalid():
648648
decrypter.decrypt("a.b.c.d.e", keys=[encryption_key])
649649

650650

651-
def test_fernet():
651+
def test_fernet_password():
652+
encrypter = FernetEncrypter(password="DukeofHazardpass")
653+
_token = encrypter.encrypt(plain)
654+
655+
decrypter = encrypter
656+
resp = decrypter.decrypt(_token)
657+
assert resp == plain
658+
659+
660+
def test_fernet_symkey():
652661
encryption_key = SYMKey(use="enc", key="DukeofHazardpass", kid="some-key-id")
653662

654-
encrypter = FernetEncrypter(encryption_key.key)
663+
encrypter = FernetEncrypter(password=encryption_key.key)
664+
_token = encrypter.encrypt(plain)
665+
666+
decrypter = encrypter
667+
resp = decrypter.decrypt(_token)
668+
assert resp == plain
669+
670+
671+
def test_fernet_bad():
672+
with pytest.raises(TypeError):
673+
encrypter = FernetEncrypter(key="xyzzy")
674+
with pytest.raises(ValueError):
675+
encrypter = FernetEncrypter(key=os.urandom(16))
676+
677+
678+
def test_fernet_bytes():
679+
key = os.urandom(32)
680+
681+
encrypter = FernetEncrypter(key=key)
682+
_token = encrypter.encrypt(plain)
683+
684+
decrypter = encrypter
685+
resp = decrypter.decrypt(_token)
686+
assert resp == plain
687+
688+
689+
def test_fernet_default_key():
690+
encrypter = FernetEncrypter()
655691
_token = encrypter.encrypt(plain)
656692

657693
decrypter = encrypter
@@ -662,7 +698,7 @@ def test_fernet():
662698
def test_fernet_sha512():
663699
encryption_key = SYMKey(use="enc", key="DukeofHazardpass", kid="some-key-id")
664700

665-
encrypter = FernetEncrypter(encryption_key.key, hash_alg="SHA512")
701+
encrypter = FernetEncrypter(password=encryption_key.key, hash_alg="SHA512")
666702
_token = encrypter.encrypt(plain)
667703

668704
decrypter = encrypter
@@ -674,7 +710,7 @@ def test_fernet_blake2s():
674710
encryption_key = SYMKey(use="enc", key="DukeofHazardpass", kid="some-key-id")
675711

676712
encrypter = FernetEncrypter(
677-
encryption_key.key, hash_alg="BLAKE2s", digest_size=32, iterations=1000
713+
password=encryption_key.key, hash_alg="BLAKE2s", digest_size=32, iterations=1000
678714
)
679715
_token = encrypter.encrypt(plain)
680716

0 commit comments

Comments
 (0)