"""Interface to RSA cipher and signature algorithm"""from__future__importannotationsimporttypingfromtypingimportTYPE_CHECKINGfrompyflocker.ciphers.backendsimportload_algorithmas_load_algoifTYPE_CHECKING:# pragma: no coverfromtypesimportModuleTypefrompyflocker.ciphers.backendsimportBackendsfrompyflocker.ciphers.baseimportBaseRSAPrivateKey,BaseRSAPublicKeydef_load_rsa(backend:Backends|None)->ModuleType:"""Load the cipher module from the backend."""return_load_algo("RSA",backend)
[docs]defgenerate(bits:int,e:int=65537,*,backend:Backends|None=None,)->BaseRSAPrivateKey:""" Generate a private key with given key modulus ``bits`` and public exponent ``e`` (default 65537). Recommended size of ``bits`` > 1024. Args: bits: The bit length of the RSA key. e: The public exponent value. Default is 65537. Keyword Arguments: backend: The backend to use. It must be a value from :any:`Backends`. Returns: The RSA private key. """key=_load_rsa(backend).generate(bits,e)iftyping.TYPE_CHECKING:assertisinstance(key,BaseRSAPrivateKey)returnkey
[docs]defload_public_key(data:bytes,*,backend:Backends|None=None,)->BaseRSAPublicKey:"""Loads the public key and returns a Key interface. Args: data: The public key (a bytes-like object) to deserialize. Keyword Arguments: backend: The backend to use. It must be a value from :any:`Backends`. Returns: The RSA public key. """key=_load_rsa(backend).load_public_key(data)iftyping.TYPE_CHECKING:assertisinstance(key,BaseRSAPublicKey)returnkey
[docs]defload_private_key(data:bytes,passphrase:bytes|None=None,*,backend:Backends|None=None,)->BaseRSAPrivateKey:"""Loads the private key and returns a Key interface. If the private key was not encrypted duting the serialization, `passphrase` must be `None`, otherwise it must be a `bytes` object. Args: data: The private key (a bytes-like object) to deserialize. passphrase: The passphrase that was used to encrypt the private key. ``None`` if the private key was not encrypted. Keyword Arguments: backend: The backend to use. It must be a value from :any:`Backends`. Returns: The RSA private key. """key=_load_rsa(backend).load_private_key(data,passphrase)iftyping.TYPE_CHECKING:assertisinstance(key,BaseRSAPrivateKey)returnkey