ó
‚¾^Yc           @   s®  d  Z  d d l Z d d l m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z e j
 e ƒ Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ e j j e d „ Z d „  Z d „  Z e e d „ Z e d e d „ Z d d	 d g Z e d k rªd d l Z ye x^ e d ƒ D]P Z e j ƒ  \ Z Z  e rVPn  e rle d d k sxe d k r4d e GHq4q4WWn e! k
 r¡d GHqªXd GHn  d S(   sj  RSA key generation code.

Create new keys with the newkeys() function. It will give you a PublicKey and a
PrivateKey object.

Loading and saving keys requires the pyasn1 module. This module is imported as
late as possible, such that other functionality will remain working in absence
of pyasn1.

.. note::

    Storing public and private keys via the `pickle` module is possible.
    However, it is insecure to load a key from an untrusted source.
    The pickle module is not secure against erroneous or maliciously
    constructed data. Never unpickle data received from an untrusted
    or unauthenticated source.

iÿÿÿÿN(   t   bi  t   AbstractKeyc           B   s\   e  Z d  Z d
 Z d „  Z e d d „ ƒ Z e d „  ƒ Z d d „ Z	 d „  Z
 d	 „  Z RS(   s0   Abstract superclass for private and public keys.t   nt   ec         C   s   | |  _  | |  _ d  S(   N(   R   R   (   t   selfR   R   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   __init__6   s    	t   PEMc         C   s6   i |  j  d 6|  j d 6} |  j | | ƒ } | | ƒ S(   s  Loads a key in PKCS#1 DER or PEM format.

        :param keyfile: contents of a DER- or PEM-encoded file that contains
            the public key.
        :param format: the format of the file to load; 'PEM' or 'DER'

        :return: a PublicKey object
        R   t   DER(   t   _load_pkcs1_pemt   _load_pkcs1_dert   _assert_format_exists(   t   clst   keyfilet   formatt   methodst   method(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt
   load_pkcs1:   s
    
c         C   sU   y | |  SWnB t  k
 rP d j t | j ƒ  ƒ ƒ } t d |  | f ƒ ‚ n Xd S(   sB   Checks whether the given file format exists in 'methods'.
        s   , s%   Unsupported format: %r, try one of %sN(   t   KeyErrort   joint   sortedt   keyst
   ValueError(   t   file_formatR   t   formats(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR
   M   s    	c         C   s3   i |  j  d 6|  j d 6} |  j | | ƒ } | ƒ  S(   s«   Saves the public key in PKCS#1 DER or PEM format.

        :param format: the format to save; 'PEM' or 'DER'
        :returns: the DER- or PEM-encoded public key.
        R   R   (   t   _save_pkcs1_pemt   _save_pkcs1_derR
   (   R   R   R   R   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt
   save_pkcs1Y   s
    
c         C   s!   | t  | |  j |  j ƒ |  j S(   s¹  Performs blinding on the message using random number 'r'.

        :param message: the message, as integer, to blind.
        :type message: int
        :param r: the random number to blind with.
        :type r: int
        :return: the blinded message.
        :rtype: int

        The blinding is such that message = unblind(decrypt(blind(encrypt(message))).

        See https://en.wikipedia.org/wiki/Blinding_%28cryptography%29
        (   t   powR   R   (   R   t   messaget   r(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   blindh   s    c         C   s!   t  j j | |  j ƒ | |  j S(   s‚  Performs blinding on the message using random number 'r'.

        :param blinded: the blinded message, as integer, to unblind.
        :param r: the random number to unblind with.
        :return: the original message.

        The blinding is such that message = unblind(decrypt(blind(encrypt(message))).

        See https://en.wikipedia.org/wiki/Blinding_%28cryptography%29
        (   t   rsat   commont   inverseR   (   R   t   blindedR   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   unblindy   s    (   R   R   (   t   __name__t
   __module__t   __doc__t	   __slots__R   t   classmethodR   t   staticmethodR
   R   R   R#   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR   1   s   		t	   PublicKeyc           B   s˜   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 e
 d	 „  ƒ Z d
 „  Z e
 d „  ƒ Z d „  Z e
 d „  ƒ Z e
 d „  ƒ Z RS(   s†  Represents a public RSA key.

    This key is also known as the 'encryption key'. It contains the 'n' and 'e'
    values.

    Supports attributes as well as dictionary-like access. Attribute accesss is
    faster, though.

    >>> PublicKey(5, 3)
    PublicKey(5, 3)

    >>> key = PublicKey(5, 3)
    >>> key.n
    5
    >>> key['n']
    5
    >>> key.e
    3
    >>> key['e']
    3

    R   R   c         C   s   t  |  | ƒ S(   N(   t   getattr(   R   t   key(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   __getitem__¢   s    c         C   s   d |  j  |  j f S(   Ns   PublicKey(%i, %i)(   R   R   (   R   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   __repr__¥   s    c         C   s   |  j  |  j f S(   s&   Returns the key as tuple for pickling.(   R   R   (   R   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   __getstate__¨   s    c         C   s   | \ |  _  |  _ d S(   s   Sets the key from tuple.N(   R   R   (   R   t   state(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   __setstate__¬   s    c         C   sE   | d  k r t St | t ƒ s# t S|  j | j k oD |  j | j k S(   N(   t   Nonet   Falset
   isinstanceR*   R   R   (   R   t   other(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   __eq__°   s
    c         C   s   |  | k S(   N(    (   R   R5   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   __ne__¹   s    c         C   se   d d l  m } d d l m } | j | d | ƒ  ƒ\ } } |  d t | d ƒ d t | d ƒ ƒ S(	   sÍ  Loads a key in PKCS#1 DER format.

        :param keyfile: contents of a DER-encoded file that contains the public
            key.
        :return: a PublicKey object

        First let's construct a DER encoded key:

        >>> import base64
        >>> b64der = 'MAwCBQCNGmYtAgMBAAE='
        >>> der = base64.standard_b64decode(b64der)

        This loads the file:

        >>> PublicKey._load_pkcs1_der(der)
        PublicKey(2367317549, 65537)

        iÿÿÿÿ(   t   decoder(   t	   AsnPubKeyt   asn1SpecR   t   modulusR   t   publicExponent(   t   pyasn1.codec.derR8   t   rsa.asn1R9   t   decodet   int(   R   R   R8   R9   t   privt   _(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR	   ¼   s    c         C   s\   d d l  m } d d l m } | ƒ  } | j d |  j ƒ | j d |  j ƒ | j | ƒ S(   sb   Saves the public key in PKCS#1 DER format.

        @returns: the DER-encoded public key.
        iÿÿÿÿ(   t   encoder(   R9   R;   R<   (   R=   RC   R>   R9   t   setComponentByNameR   R   t   encode(   R   RC   R9   t   asn_key(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR   ×   s    	c         C   s"   t  j j | d ƒ } |  j | ƒ S(   sO  Loads a PKCS#1 PEM-encoded public key file.

        The contents of the file before the "-----BEGIN RSA PUBLIC KEY-----" and
        after the "-----END RSA PUBLIC KEY-----" lines is ignored.

        :param keyfile: contents of a PEM-encoded file that contains the public
            key.
        :return: a PublicKey object
        s   RSA PUBLIC KEY(   R   t   pemt   load_pemR	   (   R   R   t   der(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR   ç   s    c         C   s   |  j  ƒ  } t j j | d ƒ S(   sƒ   Saves a PKCS#1 PEM-encoded public key file.

        :return: contents of a PEM-encoded file that contains the public key.
        s   RSA PUBLIC KEY(   R   R   RG   t   save_pem(   R   RI   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR   ö   s    c         C   s"   t  j j | d ƒ } |  j | ƒ S(   sÞ  Loads a PKCS#1.5 PEM-encoded public key file from OpenSSL.

        These files can be recognised in that they start with BEGIN PUBLIC KEY
        rather than BEGIN RSA PUBLIC KEY.

        The contents of the file before the "-----BEGIN PUBLIC KEY-----" and
        after the "-----END PUBLIC KEY-----" lines is ignored.

        :param keyfile: contents of a PEM-encoded file that contains the public
            key, from OpenSSL.
        :return: a PublicKey object
        s
   PUBLIC KEY(   R   RG   RH   t   load_pkcs1_openssl_der(   R   R   RI   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   load_pkcs1_openssl_pemÿ   s    c         C   s   d d l  m } d d l m } d d l m } | j | d | ƒ  ƒ\ } } | d d | j d ƒ k rz t d	 ƒ ‚ n  |  j	 | d
 d ƒ S(   sÖ   Loads a PKCS#1 DER-encoded public key file from OpenSSL.

        :param keyfile: contents of a DER-encoded file that contains the public
            key, from OpenSSL.
        :return: a PublicKey object

        iÿÿÿÿ(   t   OpenSSLPubKey(   R8   (   t   univR:   t   headert   oids   1.2.840.113549.1.1.1s7   This is not a DER-encoded OpenSSL-compatible public keyR,   i   (
   R>   RM   R=   R8   t   pyasn1.typeRN   R?   t   ObjectIdentifiert	   TypeErrorR	   (   R   R   RM   R8   RN   t   keyinfoRB   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyRK     s    
(   R   R   (   R$   R%   R&   R'   R-   R.   R/   R1   R6   R7   R(   R	   R   R   R   RL   RK   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR*   ˆ   s   										t
   PrivateKeyc           B   sž   e  Z d  Z d Z d d d d	 „ Z d
 „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z e d „  ƒ Z d „  Z e d „  ƒ Z d „  Z RS(   s;  Represents a private RSA key.

    This key is also known as the 'decryption key'. It contains the 'n', 'e',
    'd', 'p', 'q' and other values.

    Supports attributes as well as dictionary-like access. Attribute accesss is
    faster, though.

    >>> PrivateKey(3247, 65537, 833, 191, 17)
    PrivateKey(3247, 65537, 833, 191, 17)

    exp1, exp2 and coef can be given, but if None or omitted they will be calculated:

    >>> pk = PrivateKey(3727264081, 65537, 3349121513, 65063, 57287, exp2=4)
    >>> pk.exp1
    55063
    >>> pk.exp2  # this is of course not a correct value, but it is the one we passed.
    4
    >>> pk.coef
    50797

    If you give exp1, exp2 or coef, they will be used as-is:

    >>> pk = PrivateKey(1, 2, 3, 4, 5, 6, 7, 8)
    >>> pk.exp1
    6
    >>> pk.exp2
    7
    >>> pk.coef
    8

    R   R   t   dt   pt   qt   exp1t   exp2t   coefc	   	      C   sÀ   t  j |  | | ƒ | |  _ | |  _ | |  _ | d  k rT t | | d ƒ |  _ n	 | |  _ | d  k rƒ t | | d ƒ |  _ n	 | |  _ | d  k r³ t	 j
 j | | ƒ |  _ n	 | |  _ d  S(   Ni   (   R   R   RV   RW   RX   R2   R@   RY   RZ   R   R    R!   R[   (	   R   R   R   RV   RW   RX   RY   RZ   R[   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR   K  s    					c         C   s   t  |  | ƒ S(   N(   R+   (   R   R,   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR-   a  s    c         C   s   d |  S(   Ns-   PrivateKey(%(n)i, %(e)i, %(d)i, %(p)i, %(q)i)(    (   R   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR.   d  s    c         C   s4   |  j  |  j |  j |  j |  j |  j |  j |  j f S(   s&   Returns the key as tuple for pickling.(   R   R   RV   RW   RX   RY   RZ   R[   (   R   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR/   g  s    c      	   C   s:   | \ |  _  |  _ |  _ |  _ |  _ |  _ |  _ |  _ d S(   s   Sets the key from tuple.N(   R   R   RV   RW   RX   RY   RZ   R[   (   R   R0   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR1   k  s    c         C   s±   | d  k r t St | t ƒ s# t S|  j | j k o° |  j | j k o° |  j | j k o° |  j | j k o° |  j | j k o° |  j	 | j	 k o° |  j
 | j
 k o° |  j | j k S(   N(   R2   R3   R4   RU   R   R   RV   RW   RX   RY   RZ   R[   (   R   R5   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR6   o  s    c         C   s   |  | k S(   N(    (   R   R5   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR7     s    c         C   sY   t  j j |  j d ƒ } |  j | | ƒ } t  j j | |  j |  j ƒ } |  j | | ƒ S(   sØ   Decrypts the message using blinding to prevent side-channel attacks.

        :param encrypted: the encrypted message
        :type encrypted: int

        :returns: the decrypted message
        :rtype: int
        i   (	   R   t   randnumt   randintR   R   t   coret   decrypt_intRV   R#   (   R   t	   encryptedt   blind_rR"   t	   decrypted(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   blinded_decrypt‚  s    
c         C   sY   t  j j |  j d ƒ } |  j | | ƒ } t  j j | |  j |  j ƒ } |  j | | ƒ S(   sÕ   Encrypts the message using blinding to prevent side-channel attacks.

        :param message: the message to encrypt
        :type message: int

        :returns: the encrypted message
        :rtype: int
        i   (	   R   R\   R]   R   R   R^   t   encrypt_intRV   R#   (   R   R   Ra   R"   R`   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   blinded_encrypt’  s    
c         C   ss   d d l  m } | j | ƒ \ } } | d d k rL t d | d ƒ ‚ n  t d „  | d d !Dƒ ƒ } |  | Œ  S(   s  Loads a key in PKCS#1 DER format.

        :param keyfile: contents of a DER-encoded file that contains the private
            key.
        :return: a PrivateKey object

        First let's construct a DER encoded key:

        >>> import base64
        >>> b64der = 'MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt'
        >>> der = base64.standard_b64decode(b64der)

        This loads the file:

        >>> PrivateKey._load_pkcs1_der(der)
        PrivateKey(3727264081, 65537, 3349121513, 65063, 57287)

        iÿÿÿÿ(   R8   i    s)   Unable to read this file, version %s != 0c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R@   (   t   .0t   x(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pys	   <genexpr>Ë  s    i   i	   (   R=   R8   R?   R   t   tuple(   R   R   R8   RA   RB   t   as_ints(    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR	   ¡  s    c            s  d d l  m ‰ m ‰  d d l m } d ˆ j f ‡  ‡ f d †  ƒ  Y} | ƒ  } | j d d ƒ | j d |  j ƒ | j d	 |  j ƒ | j d
 |  j	 ƒ | j d |  j
 ƒ | j d |  j ƒ | j d |  j ƒ | j d |  j ƒ | j d |  j ƒ | j | ƒ S(   sd   Saves the private key in PKCS#1 DER format.

        @returns: the DER-encoded private key.
        iÿÿÿÿ(   RN   t	   namedtype(   RC   t
   AsnPrivKeyc              sÑ   e  Z ˆ  j ˆ  j d  ˆ j ƒ  ƒ ˆ  j d ˆ j ƒ  ƒ ˆ  j d ˆ j ƒ  ƒ ˆ  j d ˆ j ƒ  ƒ ˆ  j d ˆ j ƒ  ƒ ˆ  j d ˆ j ƒ  ƒ ˆ  j d ˆ j ƒ  ƒ ˆ  j d ˆ j ƒ  ƒ ˆ  j d ˆ j ƒ  ƒ ƒ	 Z RS(	   t   versionR;   R<   t   privateExponentt   prime1t   prime2t	   exponent1t	   exponent2t   coefficient(   R$   R%   t
   NamedTypest	   NamedTypet   Integert   componentType(    (   Rj   RN   (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyRk   ×  s   Rl   i    R;   R<   Rm   Rn   Ro   Rp   Rq   Rr   (   RQ   RN   Rj   R=   RC   t   SequenceRD   R   R   RV   RW   RX   RY   RZ   R[   RE   (   R   RC   Rk   RF   (    (   Rj   RN   s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR   Î  s    "	c         C   s(   t  j j | t d ƒ ƒ } |  j | ƒ S(   sT  Loads a PKCS#1 PEM-encoded private key file.

        The contents of the file before the "-----BEGIN RSA PRIVATE KEY-----" and
        after the "-----END RSA PRIVATE KEY-----" lines is ignored.

        :param keyfile: contents of a PEM-encoded file that contains the private
            key.
        :return: a PrivateKey object
        s   RSA PRIVATE KEY(   R   RG   RH   R    R	   (   R   R   RI   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR   ò  s    c         C   s%   |  j  ƒ  } t j j | t d ƒ ƒ S(   s…   Saves a PKCS#1 PEM-encoded private key file.

        :return: contents of a PEM-encoded file that contains the private key.
        s   RSA PRIVATE KEY(   R   R   RG   RJ   R    (   R   RI   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyR     s    (   R   R   RV   RW   RX   s   exp1s   exp2s   coefN(   R$   R%   R&   R'   R2   R   R-   R.   R/   R1   R6   R7   Rc   Re   R(   R	   R   R   R   (    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyRU   '  s    								-	$c   
         sÒ   |  d ‰ |  d } |  | } |  | } t  j d |  ƒ | | ƒ } t  j d |  ƒ | | ƒ } ‡  ‡ f d †  } t }	 x; | | | ƒ sµ |	 rŸ | | ƒ } n | | ƒ } |	 }	 q{ Wt | | ƒ t | | ƒ f S(   s%  Returns a tuple of two different primes of nbits bits each.

    The resulting p * q has exacty 2 * nbits bits, and the returned p and q
    will not be equal.

    :param nbits: the number of bits in each of p and q.
    :param getprime_func: the getprime function, defaults to
        :py:func:`rsa.prime.getprime`.

        *Introduced in Python-RSA 3.1*

    :param accurate: whether to enable accurate mode or not.
    :returns: (p, q), where p > q

    >>> (p, q) = find_p_q(128)
    >>> from rsa import common
    >>> common.bit_size(p * q)
    256

    When not in accurate mode, the number of bits can be slightly less

    >>> (p, q) = find_p_q(128, accurate=False)
    >>> from rsa import common
    >>> common.bit_size(p * q) <= 256
    True
    >>> common.bit_size(p * q) > 240
    True

    i   i   s   find_p_q(%i): Finding ps   find_p_q(%i): Finding qc            s:   |  | k r t  Sˆ  s t St j j |  | ƒ } ˆ | k S(   s“   Returns True iff p and q are acceptable:

            - p and q differ
            - (p * q) has the right nr of bits (when accurate=True)
        (   R3   t   TrueR   R    t   bit_size(   RW   RX   t
   found_size(   t   accuratet
   total_bits(    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   is_acceptable8  s    (   t   logt   debugR3   t   maxt   min(
   t   nbitst   getprime_funcR{   t   shiftt   pbitst   qbitsRW   RX   R}   t   change_p(    (   R{   R|   s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   find_p_q  s     



c         C   s   |  d | d } y t  j j | | ƒ } Wn' t k
 rT t d | | f ƒ ‚ n X| | | d k r… t d | | | f ƒ ‚ n  | | f S(   s¶  Calculates an encryption and a decryption key given p, q and an exponent,
    and returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int

    i   s.   e (%d) and phi_n (%d) are not relatively primes6   e (%d) and d (%d) are not mult. inv. modulo phi_n (%d)(   R   R    R!   R   (   RW   RX   t   exponentt   phi_nRV   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   calculate_keys_custom_exponentY  s    c         C   s   t  |  | t ƒ S(   sû   Calculates an encryption and a decryption key given p and q, and
    returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime

    :return: tuple (e, d) with the encryption and decryption exponents.
    (   R‹   t   DEFAULT_EXPONENT(   RW   RX   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   calculate_keysu  s    
c         C   sm   xZ t  r\ t |  d | | ƒ \ } } y  t | | d | ƒ\ } } PWq t k
 rX q Xq W| | | | f S(   sW  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.
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int
    i   R‰   (   Rx   Rˆ   R‹   R   (   R‚   Rƒ   R{   R‰   RW   RX   R   RV   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   gen_keys‚  s    	i   c         C   sß   |  d k  r t  d ƒ ‚ n  | d k  r: t  d | ƒ ‚ n  | d k r} d d l m } d d l } | j | j d | ƒ} n t j j } t |  | d	 | d
 | ƒ\ } } }	 }
 | | } t | |	 ƒ t	 | |	 |
 | | ƒ f S(   s  Generates public and private keys, and returns them as (pub, priv).

    The public key is also known as the 'encryption key', and is a
    :py:class:`rsa.PublicKey` object. The private key is also known as the
    'decryption key' and is a :py:class:`rsa.PrivateKey` object.

    :param nbits: the number of bits required to store ``n = p*q``.
    :param accurate: when True, ``n`` will have exactly the number of bits you
        asked for. However, this makes key generation much slower. When False,
        `n`` may have slightly less bits.
    :param poolsize: the number of processes to use to generate the prime
        numbers. If set to a number > 1, a parallel algorithm will be used.
        This requires Python 2.6 or newer.
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int

    :returns: a tuple (:py:class:`rsa.PublicKey`, :py:class:`rsa.PrivateKey`)

    The ``poolsize`` parameter was added in *Python-RSA 3.1* and requires
    Python 2.6 or newer.

    i   s   Key too smalli   s   Pool size (%i) should be >= 1iÿÿÿÿ(   t   parallelNt   poolsizeR{   R‰   (
   R   R   R   t	   functoolst   partialt   getprimet   primeRŽ   R*   RU   (   R‚   R{   R   R‰   R   R‘   Rƒ   RW   RX   R   RV   R   (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   newkeysž  s    '
R•   t   __main__id   i
   i    s   %i timest   Aborteds   Doctests done("   R&   t   loggingt   rsa._compatR    t	   rsa.primeR   t   rsa.pemt
   rsa.commont   rsa.randnumt   rsa.coret	   getLoggerR$   R~   RŒ   t   objectR   R*   RU   R”   R“   Rx   Rˆ   R‹   R   RŽ   R•   t   __all__t   doctestt   ranget   countt   testmodt   failurest   testst   KeyboardInterrupt(    (    (    s$   /tmp/pip-build-kpPAdC/rsa/rsa/key.pyt   <module>"   s<   WŸäN		5"	