ó
‚¾^Yc           @   sD  d  Z  d Z d Z d Z y d d l Z Wn e k
 rG d d l Z n Xd d l m Z m Z d d l	 Z	 d d l
 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$ „  Z0 d% „  Z1 d& „  Z2 d' „  Z3 d( „  Z4 e d) k r+d d l5 Z5 e5 j6 ƒ  n  d* d+ d, d- d. g Z7 d S(/   s_  Deprecated version of the RSA module

.. deprecated:: 2.0

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

Module for calculating large primes, and RSA encryption, decryption,
signing and verification. Includes generating public and private keys.

WARNING: this code implements the mathematics of RSA. It is not suitable for
real-world secure cryptography purposes. It has not been reviewed by a security
expert. It does not include padding of data. There are many ways in which the
output of this module, when used without any modification, can be sucessfully
attacked.
s.   Sybren Stuvel, Marloes de Boer and Ivo Tamboers
   2010-02-05s   1.3.3iÿÿÿÿN(   t   dumpst   loads(   t   bytes@   Insecure version of the RSA module is imported as %s, be carefulsN   This submodule is deprecated and will be completely removed as of version 4.0.c         C   s@   |  | k  r t  | |  ƒ S| d k r) |  St  | t |  | ƒ ƒ S(   sN   Returns the greatest common divisor of p and q


    >>> gcd(42, 6)
    6
    i    (   t   gcdt   abs(   t   pt   q(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyR   ?   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/_version133.pyt	   bytes2intJ   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(   s+   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   (   t   numbert   string(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt	   int2bytesZ   s    *c         C   sq   |  | } g  } x+ | d k r= | j  | d @ƒ | d ?} q Wx, | rl | j ƒ  } |  | | d | } qA W| S(   s   Calculates r = a^p mod n
    i   i   (   t   appendt   pop(   t   aR   t   nt   resultt
   remainderst   rem(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   fast_exponentiationi   s    
	c         C   s)   t  |  d ƒ } t j | ƒ } t | ƒ S(   sP   Reads a random integer of approximately nbits bits rounded up
    to whole bytesg       @(   t   ceilt   ost   urandomR   (   t   nbitst   nbytest
   randomdata(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   read_random_intv   s    c         C   s   t  t j |  ƒ ƒ S(   s   ceil(x) -> int(math.ceil(x))(   t   intt   mathR   (   t   x(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyR   ~   s    c         C   sg   d } | |  } t  t j | d ƒ d ƒ } t | d | d ƒ } t j | | ƒ } t | ƒ | |  S(   s9   Returns a random integer x with minvalue <= x <= maxvaluei    i   g       @i   (   R   R&   t   logt   maxt   randomt   randintR$   (   t   minvaluet   maxvaluet	   min_nbitst   ranget
   rangebytest	   rangebitsR!   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyR+   ƒ   s    
c         C   s'   t  d |  d ƒ } t | |  d |  ƒ S(   sP   Returns 1 if p may be prime, and something else if p definitely
    is not primei   (   R+   R   (   R   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   fermat_little_theorem˜   s    c         C   s   |  | d k r d Sd } x| |  d k r˜ |  d @rk |  d | d d ?d @rW | } n  |  | |  } }  q | d d d ?d @r‹ | } n  |  d ?}  q W| S(   s4   Calculates the value of the Jacobi symbol (a/b)
    i    i   i   i   (    (   R   t   bR   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.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   (   R4   R   t   Falset   True(   R'   R   t   jt   f(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   jacobi_witness±   s
     c         C   sk   d } t  | t j d | d ƒ ƒ } x> t | d ƒ D], } t d |  d ƒ } t | |  ƒ r7 t Sq7 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 if composite, and True if it's
    probably prime.
    g      à?i   i   (   R   R&   R(   R/   R+   R9   R5   R6   (   R   t   kR   t   tt   iR'   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.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=   R6   R5   (   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   is_primeÎ   s    
c         C   sP   t  t j |  d ƒ ƒ } x0 t rK 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.
    g       @i   (   R%   R&   R   R6   R$   R>   (   R!   R"   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   getprimeà   s    	
 c         C   s   t  |  | ƒ } | d k S(   sQ   Returns True if a and b are relatively prime, and False if they
    are not.
    i   (   R   (   R   R3   t   d(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   are_relatively_primeô   s    c         C   s?   t  |  ƒ } x& t r4 t  |  ƒ } | | k s Pq q W| | f S(   s5   Returns a tuple of two different primes of nbits bits(   R?   R6   (   R!   R   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   find_p_qü   s    	 c         C   sf   | d k r |  d d f St  |  | ƒ } t |  | ƒ } t | | ƒ \ } } } | | | | | f S(   s@   Returns a tuple (d, i, j) such that d = gcd(a, b) = ia + jb
    i    i   (   R   t   longt   extended_euclid_gcd(   R   R3   R   t   rR@   R:   t   l(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyRD     s    c   	      C   sÛ   |  | } |  d | d } xE t  rc t t d | d ƒ ƒ } t | | ƒ r t | | ƒ r Pq q Wt | | ƒ \ } } } | d k s¡ t d | | f ƒ ‚ 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 primes6   e (%d) and i (%d) are not mult. inv. modulo phi_n (%d)(   R6   R?   R)   RA   RD   t	   Exception(	   R   R   R!   R   t   phi_nt   eR@   R<   R7   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   calculate_keys  s    
	 c         C   sW   xD t  rF t |  ƒ \ } } t | | |  ƒ \ } } | d k r Pq q W| | | | 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.
    i    (   R6   RB   RJ   (   R!   R   R   RI   R@   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   gen_keys,  s    	 c         C   sI   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: ....).
    RI   R   R@   R   R   (   RK   (   R!   R   R   RI   R@   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   gen_pubpriv_keys<  s    c         C   s°   t  |  ƒ t j k r+ t t |  ƒ | | ƒ St  |  ƒ t j k	 rO t d ƒ ‚ n  |  d k r  t j t j	 |  d ƒ ƒ t j t j	 | d ƒ ƒ k r  t
 d ƒ ‚ n  t |  | | ƒ S(   sD   Encrypts a message using encryption key 'ekey', working modulo
    ns   You must pass a long or an inti    i   s   The message is too long(   R   R   R   t   encrypt_intRC   R   R   R&   t   floorR(   t   OverflowErrorR   (   t   messaget   ekeyR   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyRM   H  s    6c         C   s   t  |  | | ƒ S(   sL   Decrypts a cypher text using the decryption key 'dkey', working
    modulo n(   RM   (   t
   cyphertextt   dkeyR   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   decrypt_intX  s    c         C   s   t  |  | | ƒ S(   s2   Signs 'message' using key 'dkey', working modulo n(   RT   (   RP   RS   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   sign_int^  s    c         C   s   t  |  | | ƒ S(   s4   verifies 'signed' using key 'ekey', working modulo n(   RM   (   t   signedRQ   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt
   verify_intc  s    c         C   s.   t  j t |  ƒ ƒ } t j | ƒ } | j ƒ  S(   s-   Pickles and base64encodes it's argument chops(   t   zlibt   compressR    t   base64t   encodestringt   strip(   t   chopst   valuet   encoded(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   picklechopsh  s    c         C   s   t  t j t j |  ƒ ƒ ƒ S(   s:   base64decodes and unpickes it's argument string into chops(   R   RX   t
   decompressRZ   t   decodestring(   R   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   unpicklechopso  s    c         C   sÏ   t  |  ƒ } | d } t t j t j | d ƒ ƒ ƒ } | d } | | } | | d k rh | d 7} n  g  }	 xT t | ƒ D]F }
 |
 | } |  | | | !} t | ƒ } |	 j | | | | ƒ ƒ q{ Wt |	 ƒ S(   sº   Splits 'message' into chops that are at most as long as n,
    converts these into integers, and calls funcref(integer, key, n)
    for each chop.

    Used by 'encrypt' and 'sign'.
    i   i   i    i   (	   t   lenR%   R&   RN   R(   R/   R   R   R`   (   RP   t   keyR   t   funcreft   msglent   mbitsR!   R"   t   blockst   cyphert   bindext   offsett   blockR^   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt
   chopstringt  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   (   Rc   R   (   R]   Re   R   Rf   RP   t   cpartt   mpart(    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt	   gluechops  s    c         C   s   t  |  | d | d t ƒ S(   s5   Encrypts a string 'message' with the public key 'key'RI   R   (   Rn   RM   (   RP   Re   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   encryptŸ  s    c         C   s#   t  |  | d | d | d t ƒ S(   s3   Signs a string 'message' with the private key 'key'R@   R   R   (   Rn   RT   (   RP   Re   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   sign¤  s    c         C   s#   t  |  | d | d | d t ƒ S(   s,   Decrypts a cypher with the private key 'key'R@   R   R   (   Rq   RT   (   Rj   Re   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   decrypt©  s    c         C   s   t  |  | d | d t ƒ S(   s+   Verifies a cypher with the public key 'key'RI   R   (   Rq   RM   (   Rj   Re   (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   verify®  s    t   __main__RL   Rr   Rt   Rs   Ru   (8   t   __doc__t
   __author__t   __date__t   __version__t   cPicklet   picklet   ImportErrorR    R   RZ   R&   R   R*   t   sysR   RX   t   rsa._compatR   t   warningst   warnt   __name__t   DeprecationWarningR   R   R   R   R$   R   R+   R2   R4   R9   R=   R>   R?   RA   RB   RD   RJ   RK   RL   RM   RT   RU   RW   R`   Rc   Rn   Rq   Rr   Rs   Rt   Ru   t   doctestt   testmodt   __all__(    (    (    s,   /tmp/pip-build-kpPAdC/rsa/rsa/_version133.pyt   <module>   sp   																	
																