B
    ?[                 @   s.   d Z ddlZddlZddlT G dd dZdS )z$Id$    N)*c               @   s   e Zd Z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&S )'pubkeyzxAn abstract class for a public key object.

    :undocumented: __getstate__, __setstate__, __eq__, __ne__, validate
    c             C   s   d S )N )selfr   r   6lib/python3.7/site-packages/Crypto/PublicKey/pubkey.py__init__&   s    zpubkey.__init__c             C   s2   | j }x&| jD ]}||krt|| ||< qW |S )zTo keep key objects platform-independent, the key data is
        converted to standard Python long integers before being
        written out.  It will then be reconverted as necessary on
        restoration.)__dict__keydataint)r   dkeyr   r   r   __getstate__)   s
     zpubkey.__getstate__c             C   s.   x(| j D ]}||krt|| | j|< qW dS )zOn unpickling a key object, the key data is converted to the big
number representation being used, whether that is Python long
integers, MPZ objects, or whatever.N)r	   Zbignumr   )r   r   r   r   r   r   __setstate__3   s     zpubkey.__setstate__c             C   sR   d}t |trt|}d}t |tr,t|}| ||}|rJttt|S |S dS )aj  Encrypt a piece of data.

        :Parameter plaintext: The piece of data to encrypt.
        :Type plaintext: byte string or long

        :Parameter K: A random parameter required by some algorithms
        :Type K: byte string or long

        :Return: A tuple with two items. Each item is of the same type as the
         plaintext (string or long).
        r      N)
isinstancebytesbytes_to_longZ_encrypttuplemaplong_to_bytes)r   	plaintextK	wasString
ciphertextr   r   r   encrypt:   s    
 
 zpubkey.encryptc             C   sR   d}t |ts|f}t |d tr4ttt|}d}| |}|rJt|S |S dS )a.  Decrypt a piece of data. 

        :Parameter ciphertext: The piece of data to decrypt.
        :Type ciphertext: byte string, long or a 2-item tuple as returned by `encrypt`

        :Return: A byte string if ciphertext was a byte string or a tuple
         of byte strings. A long otherwise.
        r   r   N)r   r   r   r   r   Z_decryptr   )r   r   r   r   r   r   r   decryptO   s    	
 
 zpubkey.decryptc             C   s@   |   stdt|tr"t|}t|tr4t|}| ||S )a  Sign a piece of data.

        :Parameter M: The piece of data to encrypt.
        :Type M: byte string or long

        :Parameter K: A random parameter required by some algorithms
        :Type K: byte string or long

        :Return: A tuple with two items.
        z(Private key not available in this object)has_private	TypeErrorr   r   r   Z_sign)r   Mr   r   r   r   signa   s    
 
 zpubkey.signc             C   s   t |trt|}| ||S )a>  Verify the validity of a signature.

        :Parameter M: The expected message.
        :Type M: byte string or long

        :Parameter signature: The signature to verify.
        :Type signature: tuple with two items, as return by `sign`

        :Return: True if the signature is correct, False otherwise.
        )r   r   r   Z_verify)r   r   	signaturer   r   r   verifyr   s    
 zpubkey.verifyc             C   s   t dt d S )Nz0validate() method name is obsolete; use verify())warningswarnDeprecationWarning)r   r   r    r   r   r   validate   s    zpubkey.validatec             C   sL   d}t |trt|}d}t |tr,t|}| ||}|rDt|S |S dS )a&  Blind a message to prevent certain side-channel attacks.
       
        :Parameter M: The message to blind.
        :Type M: byte string or long

        :Parameter B: Blinding factor.
        :Type B: byte string or long

        :Return: A byte string if M was so. A long otherwise.
        r   r   N)r   r   r   Z_blindr   )r   r   Br   Zblindedmessager   r   r   blind   s    
 
  zpubkey.blindc             C   sL   d}t |trt|}d}t |tr,t|}| ||}|rDt|S |S dS )zUnblind a message after cryptographic processing.
        
        :Parameter M: The encoded message to unblind.
        :Type M: byte string or long

        :Parameter B: Blinding factor.
        :Type B: byte string or long
        r   r   N)r   r   r   Z_unblindr   )r   r   r&   r   Zunblindedmessager   r   r   unblind   s    	
 
  zpubkey.unblindc             C   s   dS )a(  Tell if the algorithm can deal with cryptographic signatures.

        This property concerns the *algorithm*, not the key itself.
        It may happen that this particular key object hasn't got
        the private information required to generate a signature.

        :Return: boolean
        r   r   )r   r   r   r   can_sign   s    	zpubkey.can_signc             C   s   dS )a  Tell if the algorithm can deal with data encryption.
       
        This property concerns the *algorithm*, not the key itself.
        It may happen that this particular key object hasn't got
        the private information required to decrypt data.

        :Return: boolean
        r   r   )r   r   r   r   can_encrypt   s    	zpubkey.can_encryptc             C   s   dS )a  Tell if the algorithm can deal with data blinding.
       
        This property concerns the *algorithm*, not the key itself.
        It may happen that this particular key object hasn't got
        the private information required carry out blinding.

        :Return: boolean
        r   r   )r   r   r   r   	can_blind   s    	zpubkey.can_blindc             C   s   dS )z_Tell the maximum number of bits that can be handled by this key.

        :Return: int
        r   r   )r   r   r   r   size   s    zpubkey.sizec             C   s   dS )zSTell if the key object contains private components.

        :Return: bool
        r   r   )r   r   r   r   r      s    zpubkey.has_privatec             C   s   | S )zkConstruct a new key carrying only the public information.

        :Return: A new `pubkey` object.
        r   )r   r   r   r   	publickey   s    zpubkey.publickeyc             C   s   |   |  kS )zF__eq__(other): 0, 1
        Compare us to other for equality.
        )r   )r   otherr   r   r   __eq__   s    zpubkey.__eq__c             C   s   |  | S )zH__ne__(other): 0, 1
        Compare us to other for inequality.
        )r/   )r   r.   r   r   r   __ne__   s    zpubkey.__ne__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r!   r%   r'   r(   r)   r*   r+   r,   r   r-   r/   r0   r   r   r   r   r   !   s&   
r   )Z__revision__typesr"   ZCrypto.Util.numberr   r   r   r   r   <module>   s   