summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSybren St?vel <sybren@stuvel.eu>2016-01-21 10:43:07 +0100
committerSybren St?vel <sybren@stuvel.eu>2016-01-21 10:43:07 +0100
commit5b1a34e2e109ac53dad42ff398a92ce1cb814dd3 (patch)
tree4bee2a1e21b634f5b0369e755db1b89f791451e0
parent4d86715577d9c5a21a7539aed3d468b64aa05584 (diff)
parent5bac303e8ac695e60e52646574d8a449dfb831b0 (diff)
downloadrsa-master.tar.gz
Merged in adamantike/python-rsa (pull request #20)HEADmaster
Primes regeneration in RSA keys generation
-rw-r--r--rsa/key.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/rsa/key.py b/rsa/key.py
index 57f2aa8..fc2ff84 100644
--- a/rsa/key.py
+++ b/rsa/key.py
@@ -530,19 +530,27 @@ def calculate_keys(p, q, nbits):
return (e, d)
+
def gen_keys(nbits, getprime_func, accurate=True):
'''Generate RSA keys of nbits bits. Returns (p, q, e, d).
Note: this can take a long time, depending on the key size.
-
+
:param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
``q`` will use ``nbits/2`` bits.
:param getprime_func: either :py:func:`rsa.prime.getprime` or a function
with similar signature.
'''
- (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
- (e, d) = calculate_keys(p, q, nbits // 2)
+ # Regenerate p and q values, until calculate_keys doesn't raise a
+ # ValueError.
+ while True:
+ (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
+ try:
+ (e, d) = calculate_keys(p, q, nbits // 2)
+ break
+ except ValueError:
+ pass
return (p, q, e, d)