Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception causes all over the codebase #157

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions rsa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions rsa/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 "
Expand Down
4 changes: 2 additions & 2 deletions rsa/pkcs1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]

Expand Down
4 changes: 2 additions & 2 deletions rsa/pkcs1_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down