diff --git a/rsa/cli.py b/rsa/cli.py index 3166150..c7a24f4 100644 --- a/rsa/cli.py +++ b/rsa/cli.py @@ -58,10 +58,10 @@ def keygen() -> None: try: keysize = int(cli_args[0]) - except ValueError: + except ValueError as ex: parser.print_help() print('Not a valid number: %s' % cli_args[0], file=sys.stderr) - raise SystemExit(1) + raise SystemExit(1) from ex print('Generating %i-bit key' % keysize, file=sys.stderr) (pub_key, priv_key) = rsa.newkeys(keysize) @@ -280,8 +280,8 @@ def perform_operation(self, indata: bytes, pub_key: rsa.key.AbstractKey, try: rsa.verify(indata, signature, pub_key) - except rsa.VerificationError: - raise SystemExit('Verification failed.') + except rsa.VerificationError as ex: + raise SystemExit('Verification failed.') from ex print('Verification OK', file=sys.stderr) diff --git a/rsa/key.py b/rsa/key.py index b1e2030..8e38b87 100644 --- a/rsa/key.py +++ b/rsa/key.py @@ -123,10 +123,10 @@ def _assert_format_exists(file_format: str, methods: typing.Mapping[str, typing. try: return methods[file_format] - except KeyError: + except KeyError as ex: formats = ', '.join(sorted(methods.keys())) raise ValueError('Unsupported format: %r, try one of %s' % (file_format, - formats)) + formats)) from ex def save_pkcs1(self, format: str = 'PEM') -> bytes: """Saves the key in PKCS#1 DER or PEM format. @@ -675,7 +675,7 @@ def calculate_keys_custom_exponent(p: int, q: int, exponent: int) -> typing.Tupl raise rsa.common.NotRelativePrimeError( exponent, phi_n, ex.d, msg="e (%d) and phi_n (%d) are not relatively prime (divider=%i)" % - (exponent, phi_n, ex.d)) + (exponent, phi_n, ex.d)) from ex if (exponent * d) % phi_n != 1: raise ValueError("e (%d) and d (%d) are not mult. inv. modulo " diff --git a/rsa/pkcs1.py b/rsa/pkcs1.py index 57b0276..17b10a9 100644 --- a/rsa/pkcs1.py +++ b/rsa/pkcs1.py @@ -261,8 +261,8 @@ def decrypt(crypto: bytes, priv_key: key.PrivateKey) -> bytes: # Find the 00 separator between the padding and the message try: sep_idx = cleartext.index(b'\x00', 2) - except ValueError: - raise DecryptionError('Decryption failed') + except ValueError as ex: + raise DecryptionError('Decryption failed') from ex return cleartext[sep_idx + 1:] diff --git a/rsa/pkcs1_v2.py b/rsa/pkcs1_v2.py index f780aff..ed616f5 100644 --- a/rsa/pkcs1_v2.py +++ b/rsa/pkcs1_v2.py @@ -49,12 +49,12 @@ def mgf1(seed: bytes, length: int, hasher: str = 'SHA-1') -> bytes: try: hash_length = pkcs1.HASH_METHODS[hasher]().digest_size - except KeyError: + except KeyError as ex: raise ValueError( 'Invalid `hasher` specified. Please select one of: {hash_list}'.format( hash_list=', '.join(sorted(pkcs1.HASH_METHODS.keys())) ) - ) + ) from ex # If l > 2^32(hLen), output "mask too long" and stop. if length > (2**32 * hash_length):