@@ -17,17 +17,13 @@ class FernetEncrypter(Encrypter):
1717 def __init__ (
1818 self ,
1919 password : Optional [str ] = None ,
20- key : Optional [bytes ] = None ,
2120 salt : Optional [bytes ] = "" ,
21+ key : Optional [bytes ] = None ,
2222 hash_alg : Optional [str ] = "SHA256" ,
2323 digest_size : Optional [int ] = 0 ,
2424 iterations : Optional [int ] = DEFAULT_ITERATIONS ,
2525 ):
2626 Encrypter .__init__ (self )
27- if not salt :
28- salt = os .urandom (16 )
29- else :
30- salt = as_bytes (salt )
3127
3228 if password is not None :
3329 _alg = getattr (hashes , hash_alg )
@@ -36,12 +32,15 @@ def __init__(
3632 _algorithm = _alg (digest_size )
3733 else :
3834 _algorithm = _alg ()
35+ salt = as_bytes (salt ) if salt else os .urandom (16 )
3936 kdf = PBKDF2HMAC (algorithm = _algorithm , length = 32 , salt = salt , iterations = iterations )
4037 self .key = base64 .urlsafe_b64encode (kdf .derive (as_bytes (password )))
4138 elif key is not None :
39+ if not isinstance (key , bytes ):
40+ raise TypeError ("Raw key must be bytes" )
4241 if len (key ) != 32 :
4342 raise ValueError ("Raw key must be 32 bytes" )
44- self .key = base64 .urlsafe_b64encode (as_bytes ( key ) )
43+ self .key = base64 .urlsafe_b64encode (key )
4544 else :
4645 self .key = Fernet .generate_key ()
4746
0 commit comments