11import base64
22import os
3- from typing import Optional , Union
43
54from cryptography .fernet import Fernet
65from cryptography .hazmat .primitives import hashes
1514class FernetEncrypter (Encrypter ):
1615 def __init__ (
1716 self ,
18- password : Optional [ str ] = None ,
19- salt : Optional [ bytes ] = "" ,
20- key : Optional [ bytes ] = None ,
21- hash_alg : Optional [ str ] = "SHA256" ,
22- digest_size : Optional [ int ] = 0 ,
23- iterations : Optional [ int ] = DEFAULT_ITERATIONS ,
17+ password : str | None = None ,
18+ salt : bytes | None = "" ,
19+ key : bytes | None = None ,
20+ hash_alg : str | None = "SHA256" ,
21+ digest_size : int | None = 0 ,
22+ iterations : int | None = DEFAULT_ITERATIONS ,
2423 ):
2524 Encrypter .__init__ (self )
2625
@@ -45,14 +44,14 @@ def __init__(
4544
4645 self .core = Fernet (self .key )
4746
48- def encrypt (self , msg : Union [ str , bytes ] , ** kwargs ) -> bytes :
47+ def encrypt (self , msg : str | bytes , ** kwargs ) -> bytes :
4948 text = as_bytes (msg )
5049 # Padding to block size of AES
5150 if len (text ) % 16 :
5251 text += b" " * (16 - len (text ) % 16 )
5352 return self .core .encrypt (as_bytes (text ))
5453
55- def decrypt (self , msg : Union [ str , bytes ] , ** kwargs ) -> bytes :
54+ def decrypt (self , msg : str | bytes , ** kwargs ) -> bytes :
5655 dec_text = self .core .decrypt (as_bytes (msg ))
5756 dec_text = dec_text .rstrip (b" " )
5857 return dec_text
0 commit comments