ó
‚¾^Yc           @   sæ  d  Z  d Z d Z d Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 m
 Z
 d d l Z e j d e ƒ e j d e ƒ d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d  „  Z& d! „  Z' d" „  Z( d# „  Z) d$ „  Z* d% „  Z+ d& „  Z, d' „  Z- e d( k rÍd d l. Z. e. j/ ƒ  n  d) d* d+ d, d- g Z0 d S(.   s   Deprecated version of the RSA module

.. deprecated:: 3.0

    This submodule is deprecated and will be completely removed as of version 4.0.

s;   Sybren Stuvel, Marloes de Boer, Ivo Tamboer, and Barry Meads
   2010-02-08s   2.0iÿÿÿÿN(   t   bytes4   Insecure version of the RSA module is imported as %ssN   This submodule is deprecated and will be completely removed as of version 4.0.c         C   s   t  t j t j |  d ƒ ƒ ƒ S(   sB   Returns the number of bits required to hold a specific long numberi   (   t   intt   matht   ceilt   log(   t   number(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   bit_size+   s    c         C   sD   x= | d k r? |  | k  r+ | |  }  } n  | |  | }  } q W|  S(   sO   Returns the greatest common divisor of p and q
    >>> gcd(48, 180)
    12
    i    (    (   t   pt   q(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   gcd0   s
     c         C   sŒ   t  |  ƒ t j k p' t  |  ƒ t j k s9 t d ƒ ‚ n  d } xF |  D]> } | d 9} t  | ƒ t j k rz t | ƒ } n  | | 7} qF W| S(   s7   Converts a list of bytes or a string to an integer
    s    You must pass a string or a listi    i   (   t   typet   typest   ListTypet
   StringTypet	   TypeErrort   ord(   t   bytest   integerR    (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt	   bytes2int<   s    *
 c         C   sz   t  |  ƒ t j k p' t  |  ƒ t j k s9 t d ƒ ‚ n  d } x4 |  d k ru d t |  d @ƒ | f } |  d :}  qB W| S(   s0   
    Converts a number to a string of bytes
    s   You must pass a long or an intt    i    s   %s%siÿ   i   (   R
   R   t   LongTypet   IntTypeR   R    (   R   t   string(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt	   int2bytesL   s    *c         C   s÷   t  |  ƒ t j k p' t  |  ƒ t j k s9 t d ƒ ‚ n  d |  k oP d k n rc t |  d ƒ Sd |  k oz d k n r t |  d ƒ Sd |  k o¤ d	 k n r· t |  d	 ƒ S|  d
 k rÍ t d ƒ S|  d k rã t d ƒ St d |  ƒ ‚ d S(   s‚   Converts a number in the range of 0 to 63 into base 64 digit
    character in the range of '0'-'9', 'A'-'Z', 'a'-'z','-','_'.
    s   You must pass a long or an inti    i	   i0   i
   i#   i7   i$   i=   i>   i-   i?   i_   s   Invalid Base64 value: %iN(   R
   R   R   R   R   R    t
   ValueError(   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   to64\   s    *

c         C   sÙ   t  |  ƒ t j k p' t  |  ƒ t j k s9 t d ƒ ‚ n  d |  k oP d k n r] |  d Sd |  k ot d k n r |  d Sd |  k o˜ d k n r¥ |  d	 S|  d
 k rµ d S|  d k rÅ d St d |  ƒ ‚ d S(   sn   Converts an ordinal character value in the range of
    0-9,A-Z,a-z,-,_ to a number in the range of 0-63.
    s   You must pass a long or an inti0   i9   iA   iZ   i7   ia   iz   i=   i-   i>   i_   i?   s   Invalid Base64 value: %iN(   R
   R   R   R   R   R   (   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   from64v   s    *c         C   sz   t  |  ƒ t j k p' t  |  ƒ t j k s9 t d ƒ ‚ n  d } x4 |  d k ru d t |  d @ƒ | f } |  d :}  qB W| S(   ss   Converts a number to a string of base64 encoded characters in
    the range of '0'-'9','A'-'Z,'a'-'z','-','_'.
    s   You must pass a long or an intR   i    s   %s%si?   i@   (   R
   R   R   R   R   R   (   R   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt	   int2str64   s    *c         C   s’   t  |  ƒ t j k p' t  |  ƒ t j k s9 t d ƒ ‚ n  d } xL |  D]D } | d 9} t  | ƒ t j k rz t | ƒ } n  | t | ƒ 7} qF W| S(   sƒ   Converts a base64 encoded string into an integer.
    The chars of this string in in the range '0'-'9','A'-'Z','a'-'z','-','_'
    s    You must pass a string or a listi    i@   (   R
   R   R   R   R   R   R   (   R   R   R    (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt	   str642int¡   s    *
 c         C   s2   t  t j |  d ƒ ƒ } t j | ƒ } t | ƒ S(   sP   Reads a random integer of approximately nbits bits rounded up
    to whole bytesg       @(   R   R   R   t   ost   urandomR   (   t   nbitst   nbytest
   randomdata(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   read_random_int±   s    c         C   sc   d } | |  d } t  | ƒ d d } t | d | d ƒ } t j | | ƒ } t | ƒ | |  S(   s9   Returns a random integer x with minvalue <= x <= maxvaluei    i   i   i   i   (   R   t   maxt   randomt   randintR"   (   t   minvaluet   maxvaluet	   min_nbitst   ranget
   rangebytest	   rangebitsR   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyR%   ¹   s    c         C   s©   |  d k r d Sd } x| |  d k r” |  d @rg |  d | d d ?d @rS | } n  | |  |  }  } q | | d d ?d @r‡ | } n  |  d L}  q W|  d k r¥ d S| S(   so   Calculates the value of the Jacobi symbol (a/b)
    where both a and b are positive integers, and b is odd
    i    i   i   i   (    (   t   at   bt   result(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   jacobiÎ   s     


 c         C   sA   t  |  | ƒ | } t |  | d d | ƒ } | | k r= t St S(   sU   Returns False if n is an Euler pseudo-prime with base x, and
    True otherwise.
    i   i   (   R/   t   powt   Falset   True(   t   xt   nt   jt   f(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   jacobi_witnessá   s
     c         C   sA   x: t  | ƒ D], } t d |  d ƒ } t | |  ƒ r t Sq Wt S(   sÓ   Calculates whether n is composite (which is always correct) or
    prime (which is incorrect with error probability 2**-k)

    Returns False if the number is composite, and True if it's
    probably prime.
    i   (   R)   R%   R7   R1   R2   (   R4   t   kt   iR3   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   randomized_primality_testingì   s
    
 c         C   s   t  |  d ƒ r t St S(   s>   Returns True if the number is prime, and False otherwise.
    i   (   R:   R2   R1   (   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   is_primeü   s    c         C   s7   x0 t  r2 t |  ƒ } | d O} t | ƒ r Pq q W| S(   sx   Returns a prime number of max. 'math.ceil(nbits/8)*8' bits. In
    other words: nbits is rounded up to whole bytes.
    i   (   R2   R"   R;   (   R   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   getprime  s    	
 c         C   s   t  |  | ƒ } | d k S(   s¤   Returns True if a and b are relatively prime, and False if they
    are not.

    >>> are_relatively_prime(2, 3)
    1
    >>> are_relatively_prime(2, 4)
    0
    i   (   R	   (   R,   R-   t   d(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   are_relatively_prime  s    
c         C   s[   |  |  d } |  |  d } t  | ƒ } x& t rP t  | ƒ } | | k s+ Pq+ q+ W| | f S(   s5   Returns a tuple of two different primes of nbits bitsi   (   R<   R2   (   R   t   pbitst   qbitsR   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   find_p_q'  s    	 c   	      C   sÁ   d } d } d } d } |  } | } x[ | d k r t  |  | ƒ } | |  | }  } | | | | } } | | | | } } q' W| d k  r› | | 7} n  | d k  r´ | | 7} n  |  | | f S(   s@   Returns a tuple (r, i, j) such that r = gcd(a, b) = ia + jb
    i    i   (   t   long(	   R,   R-   R3   t   yt   lxt   lyt   oat   obR   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   extended_gcd2  s       c   	      C   sö   |  | } |  d | d } xE t  rc t d t | d ƒ ƒ } t | | ƒ r t | | ƒ r Pq q Wt | | ƒ \ } } } | d k s¡ t d | | f ƒ ‚ n  | d k  r¼ t d ƒ ‚ n  | | | d k sì t d | | | f ƒ ‚ n  | | f S(   sa   Calculates an encryption and a decryption key for p and q, and
    returns them as a tuple (e, d)i   i  i   s.   e (%d) and phi_n (%d) are not relatively primei    s1   New extended_gcd shouldn't return negative valuess6   e (%d) and i (%d) are not mult. inv. modulo phi_n (%d)(   R2   R#   R<   R>   RH   t	   Exception(	   R   R   R   R4   t   phi_nt   eR=   R9   R5   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   calculate_keysI  s    
	 c         C   s:   t  |  ƒ \ } } t | | |  ƒ \ } } | | | | f S(   s|   Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.
    (   RA   RL   (   R   R   R   RK   R=   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   gen_keysb  s    c         C   sX   t  d |  ƒ }  t |  ƒ \ } } } } i | d 6| | d 6i | d 6| d 6| d 6f S(   sÌ   Generates public and private keys, and returns them as (pub,
    priv).

    The public key consists of a dict {e: ..., , n: ....). The private
    key consists of a dict {d: ...., p: ...., q: ....).
    i	   RK   R4   R=   R   R   (   R#   RM   (   R   R   R   RK   R=   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   newkeysm  s    c         C   s   t  |  ƒ t j k r$ t |  ƒ }  n  t  |  ƒ t j k	 rH t d ƒ ‚ n  |  d k  s` |  | k ro t d ƒ ‚ n  t | ƒ d } |  d | >7}  t |  | | ƒ S(   s@   Encrypts a message using encryption key 'ekey', working modulo ns   You must pass a long or inti    s   The message is too longi   i   (	   R
   R   R   RB   R   R   t   OverflowErrorR   R0   (   t   messaget   ekeyR4   t   safebit(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   encrypt_inty  s    c         C   s4   t  |  | | ƒ } t | ƒ d } | d | >8} | S(   sL   Decrypts a cypher text using the decryption key 'dkey', working
    modulo ni   i   (   R0   R   (   t
   cyphertextt   dkeyR4   RP   RR   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   decrypt_int‹  s    c         C   s=   g  } x! |  D] } | j  t | ƒ ƒ q Wd j | ƒ } | S(   sA   base64encodes chops and combines them into a ',' delimited stringt   ,(   t   appendR   t   join(   t   chopst   chipst   valuet   encoded(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   encode64chops–  s
    c         C   s=   |  j  d ƒ } g  } x! | D] }  | j t |  ƒ ƒ q W| S(   s9   base64decodes and makes a ',' delimited string into chopsRW   (   t   splitRX   R   (   R   R[   RZ   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   decode64chops£  s
    c         C   s¾   t  |  ƒ } | d } t | ƒ d } | d } | | } | | d k rW | d 7} n  g  }	 xT t | ƒ D]F }
 |
 | } |  | | | !} t | ƒ } |	 j | | | | ƒ ƒ qj Wt |	 ƒ S(   sÅ  Chops the 'message' into integers that fit into n,
    leaving room for a safebit to be added to ensure that all
    messages fold during exponentiation.  The MSB of the number n
    is not independant modulo n (setting it could cause overflow), so
    use the next lower bit for the safebit.  Therefore reserve 2-bits
    in the number n for non-data bits.  Calls specified encryption
    function for each chop.

    Used by 'encrypt' and 'sign'.
    i   i   i    i   (   t   lenR   R)   R   RX   R^   (   RP   t   keyR4   t   funcreft   msglent   mbitsR   R    t   blockst   cyphert   bindext   offsett   blockR\   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt
   chopstring¯  s    



c         C   sI   d } t  |  ƒ } x0 | D]( } | | | | ƒ } | t | ƒ 7} q W| S(   s…   Glues chops back together into a string.  calls
    funcref(integer, key, n) for each chop.

    Used by 'decrypt' and 'verify'.
    R   (   R`   R   (   R   Rb   R4   Rc   RP   RZ   t   cpartt   mpart(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt	   gluechopsÏ  s    c         C   s6   d | k r t  d ƒ ‚ n  t |  | d | d t ƒ S(   s5   Encrypts a string 'message' with the public key 'key'R4   s(   You must use the public key with encryptRK   (   RI   Rk   RS   (   RP   Rb   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   encryptß  s    c         C   s>   d | k r t  d ƒ ‚ n  t |  | d | d | d t ƒ S(   s3   Signs a string 'message' with the private key 'key'R   s&   You must use the private key with signR=   R   (   RI   Rk   RS   (   RP   Rb   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   signæ  s    c         C   s>   d | k r t  d ƒ ‚ n  t |  | d | d | d t ƒ S(   s5   Decrypts a string 'cypher' with the private key 'key'R   s)   You must use the private key with decryptR=   R   (   RI   Rn   RV   (   Rg   Rb   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   decryptí  s    c         C   s6   d | k r t  d ƒ ‚ n  t |  | d | d t ƒ S(   s4   Verifies a string 'cypher' with the public key 'key'R4   s'   You must use the public key with verifyRK   (   RI   Rn   RV   (   Rg   Rb   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   verifyô  s    t   __main__RN   Ro   Rq   Rp   Rr   (1   t   __doc__t
   __author__t   __date__t   __version__R   R   R$   t   sysR   t   rsa._compatR    t   warningst   warnt   __name__t   DeprecationWarningR   R	   R   R   R   R   R   R   R"   R%   R/   R7   R:   R;   R<   R>   RA   RH   RL   RM   RN   RS   RV   R^   R`   Rk   Rn   Ro   Rp   Rq   Rr   t   doctestt   testmodt   __all__(    (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version200.pyt   <module>   s`   																											 					