@@ -17,17 +17,13 @@ class FernetEncrypter(Encrypter):
17
17
def __init__ (
18
18
self ,
19
19
password : Optional [str ] = None ,
20
- key : Optional [bytes ] = None ,
21
20
salt : Optional [bytes ] = "" ,
21
+ key : Optional [bytes ] = None ,
22
22
hash_alg : Optional [str ] = "SHA256" ,
23
23
digest_size : Optional [int ] = 0 ,
24
24
iterations : Optional [int ] = DEFAULT_ITERATIONS ,
25
25
):
26
26
Encrypter .__init__ (self )
27
- if not salt :
28
- salt = os .urandom (16 )
29
- else :
30
- salt = as_bytes (salt )
31
27
32
28
if password is not None :
33
29
_alg = getattr (hashes , hash_alg )
@@ -36,12 +32,15 @@ def __init__(
36
32
_algorithm = _alg (digest_size )
37
33
else :
38
34
_algorithm = _alg ()
35
+ salt = as_bytes (salt ) if salt else os .urandom (16 )
39
36
kdf = PBKDF2HMAC (algorithm = _algorithm , length = 32 , salt = salt , iterations = iterations )
40
37
self .key = base64 .urlsafe_b64encode (kdf .derive (as_bytes (password )))
41
38
elif key is not None :
39
+ if not isinstance (key , bytes ):
40
+ raise TypeError ("Raw key must be bytes" )
42
41
if len (key ) != 32 :
43
42
raise ValueError ("Raw key must be 32 bytes" )
44
- self .key = base64 .urlsafe_b64encode (as_bytes ( key ) )
43
+ self .key = base64 .urlsafe_b64encode (key )
45
44
else :
46
45
self .key = Fernet .generate_key ()
47
46
0 commit comments