diff options
author | Michael Manganiello <mike@fmanganiello.com.ar> | 2016-01-20 21:00:10 -0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2016-01-21 11:12:33 +0100 |
commit | 39d636b03d2aaf190bfc096bf180d2d916896146 (patch) | |
tree | 4bee2a1e21b634f5b0369e755db1b89f791451e0 /rsa | |
parent | 001ee5d0a87ba48bb547ce2986aabbcd52579d8c (diff) | |
download | rsa-git-39d636b03d2aaf190bfc096bf180d2d916896146.tar.gz |
Primes regeneration in RSA keys generation
Diffstat (limited to 'rsa')
-rw-r--r-- | rsa/key.py | 14 |
1 files changed, 11 insertions, 3 deletions
@@ -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) |