summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manganiello <mike@fmanganiello.com.ar>2016-01-20 21:00:10 -0300
committerMichael Manganiello <mike@fmanganiello.com.ar>2016-01-20 21:00:10 -0300
commit5bac303e8ac695e60e52646574d8a449dfb831b0 (patch)
tree483df715eb813a3d9b5901967fe3dd723606a6d8
parent29d202d869e96e50637f9bb1a96703e169c560f7 (diff)
downloadrsa-5bac303e8ac695e60e52646574d8a449dfb831b0.tar.gz
Primes regeneration in RSA keys generation
-rw-r--r--rsa/key.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/rsa/key.py b/rsa/key.py
index b6de7b3..339c656 100644
--- a/rsa/key.py
+++ b/rsa/key.py
@@ -530,22 +530,31 @@ 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)
+
def newkeys(nbits, accurate=True, poolsize=1):
'''Generates public and private keys, and returns them as (pub, priv).