a
    db;                     @   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de  ede d	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Z d-d. Z!d/d0 Z"d1d2 Z#d3d4 Z$d5d6 Z%d7d8 Z&d9d: Z'd;d< Z(d=d> Z)d?d@ Z*dAdB Z+dCdD Z,dEdF Z-edGkrxddl.Z.e./  g dHZ0dS )IzDeprecated version of the RSA module

.. deprecated:: 3.0

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

z;Sybren Stuvel, Marloes de Boer, Ivo Tamboer, and Barry Meadz
2010-02-08z2.0    N)bytez4Insecure version of the RSA module is imported as %szNThis submodule is deprecated and will be completely removed as of version 4.0.c                 C   s   t tt| dS )zBReturns the number of bits required to hold a specific long number   )intmathceillognumber r
   `/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/rsa/_version200.pybit_size+   s    r   c                 C   s.   |dkr*| |k r||  } }|| |  } }q | S )zOReturns the greatest common divisor of p and q
    >>> gcd(48, 180)
    12
    r   r
   )pqr
   r
   r   gcd0   s    r   c                 C   s\   t | tju s$t | tju s$tdd}| D ]*}|d9 }t |tju rNt|}||7 }q,|S )z7Converts a list of bytes or a string to an integer
     You must pass a string or a listr      )typetypesListType
StringType	TypeErrorord)bytesintegerr   r
   r
   r   	bytes2int<   s    
r   c                 C   sR   t | tju s$t | tju s$tdd}| dkrNdt| d@ |f }| d } q(|S )z0
    Converts a number to a string of bytes
    You must pass a long or an int r   %s%s   r   )r   r   LongTypeIntTyper   r   r	   stringr
   r
   r   	int2bytesL   s    
r#   c                 C   s   t | tju s$t | tju s$tdd|   kr8dkrHn nt| d S d|   kr\dkrln nt| d S d|   krd	krn nt| d	 S | d
krtdS | dkrtdS td|  dS )zConverts a number in the range of 0 to 63 into base 64 digit
    character in the range of '0'-'9', 'A'-'Z', 'a'-'z','-','_'.
    r   r   	   0   
   #   7   $   =   >   -   ?   _   Invalid Base64 value: %iN)r   r   r   r    r   r   
ValueErrorr   r
   r
   r   to64\   s    r1   c                 C   s   t | tju s$t | tju s$tdd|   kr8dkrDn n| d S d|   krXdkrdn n| d S d|   krxdkrn n| d	 S | d
krdS | dkrdS td|  dS )znConverts an ordinal character value in the range of
    0-9,A-Z,a-z,-,_ to a number in the range of 0-63.
    r   r%   9   A   Z   r(   a   z   r*   r,   r+   r.   r-   r/   N)r   r   r   r    r   r0   r   r
   r
   r   from64v   s    r7   c                 C   sR   t | tju s$t | tju s$tdd}| dkrNdt| d@ |f }| d } q(|S )zsConverts a number to a string of base64 encoded characters in
    the range of '0'-'9','A'-'Z,'a'-'z','-','_'.
    r   r   r   r   r-   @   )r   r   r   r    r   r1   r!   r
   r
   r   	int2str64   s    
r9   c                 C   s`   t | tju s$t | tju s$tdd}| D ].}|d9 }t |tju rNt|}|t|7 }q,|S )zConverts a base64 encoded string into an integer.
    The chars of this string in in the range '0'-'9','A'-'Z','a'-'z','-','_'
    r   r   r8   )r   r   r   r   r   r   r7   )r"   r   r   r
   r
   r   	str642int   s    r:   c                 C   s$   t t| d }t|}t|S )zPReads a random integer of approximately nbits bits rounded up
    to whole bytesg       @)r   r   r   osurandomr   )nbitsnbytesZ
randomdatar
   r
   r   read_random_int   s    
r?   c                 C   sN   d}||  d }t |d d }t|d |d }t||}t|| |  S )z9Returns a random integer x with minvalue <= x <= maxvalue             r   )r   maxrandomrandintr?   )ZminvalueZmaxvalueZ	min_nbitsrangeZ
rangebytesZ	rangebitsr=   r
   r
   r   rF      s    rF   c                 C   s   | dkrdS d}| dkrr| d@ rN| d |d  d? d@ r>| }||  |  } }q|| d d? d@ rh| }| dL } q| dkr~dS |S )zoCalculates the value of the Jacobi symbol (a/b)
    where both a and b are positive integers, and b is odd
    r   rA   r      r
   )abresultr
   r
   r   jacobi   s    
rL   c                 C   s2   t | || }t| |d d |}||kr.dS dS )zUReturns False if n is an Euler pseudo-prime with base x, and
    True otherwise.
    rA   r   FT)rL   pow)xnjfr
   r
   r   jacobi_witness   s    rR   c                 C   s0   t |D ]"}td| d }t|| r dS qdS )zCalculates 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.
    rA   FT)rG   rF   rR   )rO   kirN   r
   r
   r   randomized_primality_testing   s    
rU   c                 C   s   t | drdS dS )z>Returns True if the number is prime, and False otherwise.
       TF)rU   r   r
   r
   r   is_prime   s    
rW   c                 C   s    t | }|dO }t|r qq |S )zxReturns a prime number of max. 'math.ceil(nbits/8)*8' bits. In
    other words: nbits is rounded up to whole bytes.
    rA   )r?   rW   )r=   r   r
   r
   r   getprime  s    rX   c                 C   s   t | |}|dkS )zReturns 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
    rA   )r   )rI   rJ   dr
   r
   r   are_relatively_prime  s    

rZ   c                 C   s<   | | d  }| | d  }t |}t |}||ks q4q ||fS )z5Returns a tuple of two different primes of nbits bits   )rX   )r=   ZpbitsZqbitsr   r   r
   r
   r   find_p_q'  s    r\   c           	      C   s   d}d}d}d}| }|}|dkr`t | | }|| |  } }|||  | }}|||  | }}q|dk rp||7 }|dk r||7 }| ||fS )z@Returns a tuple (r, i, j) such that r = gcd(a, b) = ia + jb
    r   rA   )long)	rI   rJ   rN   yZlxZlyZoaobr   r
   r
   r   extended_gcd2  s    r`   c           	      C   s   | | }| d |d  }t dt|d }t||rt||rqBqt||\}}}|dksjtd||f |dk rztd|| | dkstd|||f ||fS )zaCalculates an encryption and a decryption key for p and q, and
    returns them as a tuple (e, d)rA   i     z.e (%d) and phi_n (%d) are not relatively primer   z1New extended_gcd shouldn't return negative valuesz6e (%d) and i (%d) are not mult. inv. modulo phi_n (%d))rD   rX   rZ   r`   	Exception)	r   r   r=   rO   Zphi_nerY   rT   rP   r
   r
   r   calculate_keysI  s    rd   c                 C   s(   t | \}}t||| \}}||||fS )z|Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.
    )r\   rd   r=   r   r   rc   rY   r
   r
   r   gen_keysb  s    rf   c                 C   s4   t d| } t| \}}}}||| d|||dfS )zGenerates 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: ....).
    r$   )rc   rO   )rY   r   r   )rD   rf   re   r
   r
   r   newkeysm  s    
rg   c                 C   sh   t | tju rt| } t | tjur,td| dk s<| |krDtdt|d }| d|> 7 } t| ||S )z@Encrypts a message using encryption key 'ekey', working modulo nzYou must pass a long or intr   zThe message is too longr   rA   )	r   r   r    r]   r   r   OverflowErrorr   rM   )messageZekeyrO   safebitr
   r
   r   encrypt_inty  s    rk   c                 C   s(   t | ||}t|d }|d|> 8 }|S )zLDecrypts a cypher text using the decryption key 'dkey', working
    modulo nr   rA   )rM   r   )Z
cyphertextZdkeyrO   ri   rj   r
   r
   r   decrypt_int  s    rl   c                 C   s*   g }| D ]}| t| qd|}|S )zAbase64encodes chops and combines them into a ',' delimited string,)appendr9   join)chopschipsvalueencodedr
   r
   r   encode64chops  s
    
rt   c                 C   s*   |  d}g }|D ]} |t|  q|S )z9base64decodes and makes a ',' delimited string into chopsrm   )splitrn   r:   )r"   rq   rp   r
   r
   r   decode64chops  s
    
rv   c                 C   s   t | }|d }t|d }|d }|| }|| dkr@|d7 }g }	t|D ]6}
|
| }| |||  }t|}|	|||| qLt|	S )a  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'.
    rC   r   r   rA   )lenr   rG   r   rn   rt   )ri   keyrO   funcrefZmsglenZmbitsr=   r>   blockscypherZbindexoffsetblockrr   r
   r
   r   
chopstring  s    r~   c                 C   s2   d}t | }|D ]}||||}|t|7 }q|S )zGlues chops back together into a string.  calls
    funcref(integer, key, n) for each chop.

    Used by 'decrypt' and 'verify'.
    r   )rv   r#   )r"   rx   rO   ry   ri   rp   ZcpartZmpartr
   r
   r   	gluechops  s    r   c                 C   s&   d|vrt dt| |d |d tS )z5Encrypts a string 'message' with the public key 'key'rO   z(You must use the public key with encryptrc   rb   r~   rk   ri   rx   r
   r
   r   encrypt  s    r   c                 C   s.   d|vrt dt| |d |d |d  tS )z3Signs a string 'message' with the private key 'key'r   z&You must use the private key with signrY   r   r   r   r
   r
   r   sign  s    r   c                 C   s.   d|vrt dt| |d |d |d  tS )z5Decrypts a string 'cypher' with the private key 'key'r   z)You must use the private key with decryptrY   r   rb   r   rl   r{   rx   r
   r
   r   decrypt  s    r   c                 C   s&   d|vrt dt| |d |d tS )z4Verifies a string 'cypher' with the public key 'key'rO   z'You must use the public key with verifyrc   r   r   r
   r
   r   verify  s    r   __main__)rg   r   r   r   r   )1__doc__
__author____date____version__r   r;   rE   sysr   Zrsa._compatr   warningswarn__name__DeprecationWarningr   r   r   r#   r1   r7   r9   r:   r?   rF   rL   rR   rU   rW   rX   rZ   r\   r`   rd   rf   rg   rk   rl   rt   rv   r~   r   r   r   r   r   doctesttestmod__all__r
   r
   r
   r   <module>   sb    
