12
12
import cryptography .hazmat .backends as _backends
13
13
import cryptography .hazmat .primitives .ciphers as _ciphers
14
14
15
+ from .errors import SymmetricCryptographyError
16
+
15
17
16
18
class Fernet (object ):
17
19
"""The default symmetric cryptography method."""
@@ -30,26 +32,65 @@ def __init__(self, key=None):
30
32
31
33
:param key: byte data representing the encyption/decryption key
32
34
"""
33
- self ._symmetric = _fernet .Fernet (key or self .__class__ .generate_key ())
35
+ if key :
36
+ fernet_key_error = SymmetricCryptographyError (
37
+ "Fernet key must be 32 url-safe base64-encoded bytes."
38
+ )
39
+ try :
40
+ raw_key = _base64 .b64decode (key )
41
+ except Exception as e :
42
+ raise fernet_key_error from e
43
+ else :
44
+ if len (raw_key ) != 32 :
45
+ raise fernet_key_error
46
+ else :
47
+ key = self .__class__ .generate_key ()
34
48
35
- def encrypt (self , plaintext ):
49
+ self ._symmetric = _fernet .Fernet (key )
50
+
51
+ def encrypt (self , plaintext , * args , ** kwargs ):
36
52
"""Encrypt the given plaintext.
37
53
38
54
:param plaintext: byte data representing the plaintext
39
55
:return: byte data representing the ciphertext
40
56
"""
57
+ if args or kwargs :
58
+ _deprecation_msg = (
59
+ "The '.encrypt' method does not take into account any arguements, "
60
+ "other than the 'ciphertext' param. "
61
+ "Remove any other arguements. "
62
+ "In the next version, this method will not allow them."
63
+ )
64
+ _warnings .warn (_deprecation_msg , DeprecationWarning )
65
+
41
66
ciphertext = self ._symmetric .encrypt (plaintext )
42
67
return ciphertext
43
68
44
- def decrypt (self , ciphertext ):
69
+ def decrypt (self , ciphertext , * args , ** kwargs ):
45
70
"""Decrypt the given ciphertext.
46
71
47
72
:param ciphertext: byte data representing the ciphertext
48
73
:return: byte data representing the plaintext
49
74
"""
75
+ if args or kwargs :
76
+ _deprecation_msg = (
77
+ "The '.decrypt' method does not take into account any arguements, "
78
+ "other than the 'ciphertext' param. "
79
+ "Remove any other arguements. "
80
+ "In the next version, this method will not allow them."
81
+ )
82
+ _warnings .warn (_deprecation_msg , DeprecationWarning )
83
+
50
84
plaintext = self ._symmetric .decrypt (ciphertext )
51
85
return plaintext
52
86
87
+ def build_cipher (self , * args , ** kwargs ):
88
+ _deprecation_msg = (
89
+ "The 'Fernet' class does not need a build_cipher method."
90
+ "Remove any calls to this method. "
91
+ "In the next version, this method will be removed."
92
+ ).format (name = cls .__name__ , type = type (cls ).__name__ )
93
+ _warnings .warn (_deprecation_msg , DeprecationWarning )
53
94
54
95
55
96
class AESCipher (object ):
@@ -71,7 +112,9 @@ def _deprecation_notice(cls):
71
112
_deprecation_msg = (
72
113
'{name} {type} is deprecated. '
73
114
'It will be removed in the next version. '
74
- 'Use saml2.cryptography.symmetric instead.'
115
+ 'Use saml2.cryptography.symmetric.Default '
116
+ 'or saml2.cryptography.symmetric.Fernet '
117
+ 'instead.'
75
118
).format (name = cls .__name__ , type = type (cls ).__name__ )
76
119
_warnings .warn (_deprecation_msg , DeprecationWarning )
77
120
0 commit comments