2020
2121import os
2222from hmac import compare_digest
23- from rsa import (
24- common ,
25- core ,
26- pkcs1 ,
27- transform ,
28- )
29- from rsa ._compat import xor_bytes
3023
24+ from . import common , transform , core , key , pkcs1
25+ from ._compat import xor_bytes
3126
32- def _constant_time_select (v , t , f ):
27+
28+ def _constant_time_select (v : int , t : int , f : int ) -> int :
3329 """Return t if v else f.
3430
3531 v must be 0 or 1. (False and True are allowed)
@@ -95,7 +91,9 @@ def mgf1(seed: bytes, length: int, hasher: str = "SHA-1") -> bytes:
9591 return output [:length ]
9692
9793
98- def _OAEP_encode (message , keylength , label , hash_method , mgf1_hash_method ):
94+ def _OAEP_encode (
95+ message : bytes , keylength : int , label , hash_method : str , mgf1_hash_method : str
96+ ) -> bytes :
9997 try :
10098 hasher = pkcs1 .HASH_METHODS [hash_method ](label )
10199 except KeyError :
@@ -133,14 +131,22 @@ def _OAEP_encode(message, keylength, label, hash_method, mgf1_hash_method):
133131 return em
134132
135133
136- def encrypt_OAEP (message , pub_key , label = b"" , hash_method = "SHA-1" , mgf1_hash_method = None ):
134+ def encrypt_OAEP (
135+ message : bytes ,
136+ pub_key : key .PublicKey ,
137+ label : bytes = b"" ,
138+ hash_method : str = "SHA-1" ,
139+ mgf1_hash_method : str = None ,
140+ ) -> bytes :
137141 """Encrypts the given message using PKCS#1 v2 RSA-OEAP.
138142
139- :param bytes message: the message to encrypt.
140- :param rsa.PublicKey pub_key: the public key to encrypt with.
141- :param bytes label: optional RSA-OAEP label.
142- :param str hash_method: hash function to be used. 'SHA-1' (default),
143+ :param message: the message to encrypt.
144+ :param pub_key: the public key to encrypt with.
145+ :param label: optional RSA-OAEP label.
146+ :param hash_method: hash function to be used. 'SHA-1' (default),
143147 'SHA-256', 'SHA-384', and 'SHA-512' can be used.
148+ :param mgf1_hash_method: hash function to be used by MGF1 function.
149+ If it is None (default), *hash_method* is used.
144150 """
145151 # NOTE: Some hash method other than listed in the docstring can be used
146152 # for hash_method. But the RFC 8017 recommends only them.
@@ -157,15 +163,21 @@ def encrypt_OAEP(message, pub_key, label=b"", hash_method="SHA-1", mgf1_hash_met
157163 return c
158164
159165
160- def decrypt_OAEP (crypto , priv_key , label = b"" , hash_method = "SHA-1" , mgf1_hash_method = None ):
166+ def decrypt_OAEP (
167+ crypto : bytes ,
168+ priv_key : key .PrivateKey ,
169+ label : bytes = b"" ,
170+ hash_method : str = "SHA-1" ,
171+ mgf1_hash_method : str = None ,
172+ ) -> bytes :
161173 """Decrypts the givem crypto using PKCS#1 v2 RSA-OAEP.
162174
163- :param bytes crypto: the crypto text as returned by :py:func:`rsa.encrypt`
164- :param rsa.PrivateKey priv_key: the private key to decrypt with.
165- :param bytes label: optional RSA-OAEP label.
166- :param str hash_method: hash function to be used. 'SHA-1' (default),
175+ :param crypto: the crypto text as returned by :py:func:`rsa.encrypt`
176+ :param priv_key: the private key to decrypt with.
177+ :param label: optional RSA-OAEP label.
178+ :param hash_method: hash function to be used. 'SHA-1' (default),
167179 'SHA-256', 'SHA-384', and 'SHA-512' can be used.
168- :param str mgf1_hash_method: hash function to be used by MGF1 function.
180+ :param mgf1_hash_method: hash function to be used by MGF1 function.
169181 If it is None (default), *hash_method* is used.
170182
171183 :raise rsa.pkcs1.DecryptionError: when the decryption fails. No details are given as
0 commit comments