diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2016-03-29 18:20:17 +0200 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2016-03-29 18:20:17 +0200 |
commit | a5487826a1f28952a560c50f14e284c4292d94ca (patch) | |
tree | 3734791d7c65ded746a4187f1d1d4d3339087e97 /rsa/common.py | |
parent | fec61ece4c1593e79bc52c84cf69a715cd6a9dcd (diff) | |
download | rsa-git-a5487826a1f28952a560c50f14e284c4292d94ca.tar.gz |
Introduced NotRelativePrimeError exception.
This makes catching exceptions slightly stronger, as it is now possible
to check for this specific exception. Furthermore, information about the
not-prime numbers is included in the exception object.
Diffstat (limited to 'rsa/common.py')
-rw-r--r-- | rsa/common.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/rsa/common.py b/rsa/common.py index b573252..34142cc 100644 --- a/rsa/common.py +++ b/rsa/common.py @@ -17,6 +17,15 @@ """Common functionality shared by several modules.""" +class NotRelativePrimeError(ValueError): + def __init__(self, a, b, d, msg=None): + super(NotRelativePrimeError, self).__init__( + msg or "%d and %d are not relatively prime, divider=%i" % (a, b, d)) + self.a = a + self.b = b + self.d = d + + def bit_size(num): """ Number of bits needed to represent a integer excluding any prefix @@ -142,7 +151,7 @@ def inverse(x, n): (divider, inv, _) = extended_gcd(x, n) if divider != 1: - raise ValueError("x (%d) and n (%d) are not relatively prime" % (x, n)) + raise NotRelativePrimeError(x, n, divider) return inv |