@@ -421,15 +421,20 @@ def extended_gcd(a, b):
421421 if (ly < 0 ): ly += oa #If neg wrap modulo orignal a
422422 return (a , lx , ly ) #Return only positive values
423423
424- def find_p_q (nbits , accurate = True ):
424+ def find_p_q (nbits , getprime_func = rsa . prime . getprime , accurate = True ):
425425 ''''Returns a tuple of two different primes of nbits bits each.
426426
427427 The resulting p * q has exacty 2 * nbits bits, and the returned p and q
428428 will not be equal.
429429
430- @param nbits: the number of bits in each of p and q.
431- @param accurate: whether to enable accurate mode or not.
432- @returns (p, q), where p > q
430+ :param nbits: the number of bits in each of p and q.
431+ :param getprime_func: the getprime function, defaults to
432+ :py:func:`rsa.prime.getprime`.
433+
434+ *Introduced in Python-RSA 3.1*
435+
436+ :param accurate: whether to enable accurate mode or not.
437+ :returns: (p, q), where p > q
433438
434439 >>> (p, q) = find_p_q(128)
435440 >>> from rsa import common
@@ -457,9 +462,9 @@ def find_p_q(nbits, accurate=True):
457462
458463 # Choose the two initial primes
459464 log .debug ('find_p_q(%i): Finding p' , nbits )
460- p = rsa . prime . getprime (pbits )
465+ p = getprime_func (pbits )
461466 log .debug ('find_p_q(%i): Finding q' , nbits )
462- q = rsa . prime . getprime (qbits )
467+ q = getprime_func (qbits )
463468
464469 def is_acceptable (p , q ):
465470 '''Returns True iff p and q are acceptable:
@@ -480,16 +485,12 @@ def is_acceptable(p, q):
480485
481486 # Keep choosing other primes until they match our requirements.
482487 change_p = False
483- tries = 0
484488 while not is_acceptable (p , q ):
485- tries += 1
486489 # Change p on one iteration and q on the other
487490 if change_p :
488- log .debug (' find another p' )
489- p = rsa .prime .getprime (pbits )
491+ p = getprime_func (pbits )
490492 else :
491- log .debug (' find another q' )
492- q = rsa .prime .getprime (qbits )
493+ q = getprime_func (qbits )
493494
494495 change_p = not change_p
495496
@@ -522,41 +523,62 @@ def calculate_keys(p, q, nbits):
522523
523524 return (e , d )
524525
525- def gen_keys (nbits , accurate = True ):
526+ def gen_keys (nbits , getprime_func , accurate = True ):
526527 """Generate RSA keys of nbits bits. Returns (p, q, e, d).
527528
528529 Note: this can take a long time, depending on the key size.
529530
530- @ param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
531+ : param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
531532 ``q`` will use ``nbits/2`` bits.
533+ :param getprime_func: either :py:func:`rsa.prime.getprime` or a function
534+ with similar signature.
532535 """
533536
534- (p , q ) = find_p_q (nbits // 2 , accurate )
537+ (p , q ) = find_p_q (nbits // 2 , getprime_func , accurate )
535538 (e , d ) = calculate_keys (p , q , nbits // 2 )
536539
537540 return (p , q , e , d )
538541
539- def newkeys (nbits , accurate = True ):
542+ def newkeys (nbits , accurate = True , poolsize = 1 ):
540543 """Generates public and private keys, and returns them as (pub, priv).
541544
542545 The public key is also known as the 'encryption key', and is a
543- :py:class:`PublicKey` object. The private key is also known as the
544- 'decryption key' and is a :py:class:`PrivateKey` object.
546+ :py:class:`rsa. PublicKey` object. The private key is also known as the
547+ 'decryption key' and is a :py:class:`rsa. PrivateKey` object.
545548
546549 :param nbits: the number of bits required to store ``n = p*q``.
547550 :param accurate: when True, ``n`` will have exactly the number of bits you
548551 asked for. However, this makes key generation much slower. When False,
549552 `n`` may have slightly less bits.
553+ :param poolsize: the number of processes to use to generate the prime
554+ numbers. If set to a number > 1, a parallel algorithm will be used.
555+ This requires Python 2.6 or newer.
550556
551557 :returns: a tuple (:py:class:`rsa.PublicKey`, :py:class:`rsa.PrivateKey`)
558+
559+ The ``poolsize`` parameter was added in *Python-RSA 3.1* and requires
560+ Python 2.6 or newer.
552561
553562 """
554563
555564 if nbits < 16 :
556565 raise ValueError ('Key too small' )
557566
558- (p , q , e , d ) = gen_keys (nbits )
567+ if poolsize < 1 :
568+ raise ValueError ('Pool size (%i) should be >= 1' % poolsize )
569+
570+ # Determine which getprime function to use
571+ if poolsize > 1 :
572+ from rsa import parallel
573+ import functools
574+
575+ getprime_func = functools .partial (parallel .getprime , poolsize = poolsize )
576+ else : getprime_func = rsa .prime .getprime
577+
578+ # Generate the key components
579+ (p , q , e , d ) = gen_keys (nbits , getprime_func )
559580
581+ # Create the key objects
560582 n = p * q
561583
562584 return (
0 commit comments