20
20
21
21
import os
22
22
from hmac import compare_digest
23
- from rsa import (
24
- common ,
25
- core ,
26
- pkcs1 ,
27
- transform ,
28
- )
29
- from rsa ._compat import xor_bytes
30
23
24
+ from . import common , transform , core , key , pkcs1
25
+ from ._compat import xor_bytes
31
26
32
- def _constant_time_select (v , t , f ):
27
+
28
+ def _constant_time_select (v : int , t : int , f : int ) -> int :
33
29
"""Return t if v else f.
34
30
35
31
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:
95
91
return output [:length ]
96
92
97
93
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 :
99
97
try :
100
98
hasher = pkcs1 .HASH_METHODS [hash_method ](label )
101
99
except KeyError :
@@ -133,14 +131,22 @@ def _OAEP_encode(message, keylength, label, hash_method, mgf1_hash_method):
133
131
return em
134
132
135
133
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 :
137
141
"""Encrypts the given message using PKCS#1 v2 RSA-OEAP.
138
142
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),
143
147
'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.
144
150
"""
145
151
# NOTE: Some hash method other than listed in the docstring can be used
146
152
# 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
157
163
return c
158
164
159
165
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 :
161
173
"""Decrypts the givem crypto using PKCS#1 v2 RSA-OAEP.
162
174
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),
167
179
'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.
169
181
If it is None (default), *hash_method* is used.
170
182
171
183
:raise rsa.pkcs1.DecryptionError: when the decryption fails. No details are given as
0 commit comments