summaryrefslogtreecommitdiff
path: root/rsa/common.py
diff options
context:
space:
mode:
authorSybren A. Stüvel <sybren@stuvel.eu>2016-03-29 18:20:17 +0200
committerSybren A. Stüvel <sybren@stuvel.eu>2016-03-29 18:20:17 +0200
commita5487826a1f28952a560c50f14e284c4292d94ca (patch)
tree3734791d7c65ded746a4187f1d1d4d3339087e97 /rsa/common.py
parentfec61ece4c1593e79bc52c84cf69a715cd6a9dcd (diff)
downloadrsa-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.py11
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