o
    _¨Êh˜  ã                   @   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   úI/var/www/html/env/lib/python3.10/site-packages/Crypto/PublicKey/pubkey.pyÚ__init__&   s   zpubkey.__init__c                 C   s.   | j }| jD ]}||v rt|| ƒ||< q|S )zÐTo 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*   | j D ]}||v rt|| ƒ| j|< qdS )z¢On 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
   Úbignumr	   r   r   r   r   Ú__setstate__3   s   
€ÿzpubkey.__setstate__c                 C   sN   d}t |tƒrt|ƒ}d}t |tƒrt|ƒ}|  ||¡}|r%ttt|ƒƒS |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   é   )Ú
isinstanceÚbytesÚbytes_to_longÚ_encryptÚtupleÚmapÚlong_to_bytes)r   Ú	plaintextÚKÚ	wasStringÚ
ciphertextr   r   r   Úencrypt:   s   

zpubkey.encryptc                 C   sN   d}t |tƒs
|f}t |d tƒrttt|ƒƒ}d}|  |¡}|r%t|ƒS |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   )r   r   r   r   r   Ú_decryptr   )r   r   r   r   r   r   r   ÚdecryptO   s   	

zpubkey.decryptc                 C   s@   |   ¡ stdƒ‚t|tƒrt|ƒ}t|tƒrt|ƒ}|  ||¡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   Ú_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   Ú_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ÚDeprecationWarningr'   r   r   r   Úvalidate�   s   ÿzpubkey.validatec                 C   óH   d}t |tƒrt|ƒ}d}t |tƒrt|ƒ}|  ||¡}|r"t|ƒS |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   )r   r   r   Ú_blindr   )r   r$   ÚBr   Úblindedmessager   r   r   Úblind…   s   
zpubkey.blindc                 C   r.   )zëUnblind 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   )r   r   r   Ú_unblindr   )r   r$   r0   r   Úunblindedmessager   r   r   Úunblind˜   s   	
zpubkey.unblindc                 C   ó   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­   ó   	zpubkey.can_signc                 C   r6   )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¸   r8   zpubkey.can_encryptc                 C   r6   )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Ã   r8   zpubkey.can_blindc                 C   r6   )z_Tell the maximum number of bits that can be handled by this key.

        :Return: int
        r   r   r   r   r   r   ÚsizeÑ   ó   zpubkey.sizec                 C   r6   )zSTell if the key object contains private components.

        :Return: bool
        r   r   r   r   r   r   r!   Ø   r<   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ß   r<   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   Ú__ne__ì   s   zpubkey.__ne__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r    r%   r)   r-   r2   r5   r7   r9   r:   r;   r!   r=   r@   rA   r   r   r   r   r   !   s(    
r   )Ú__revision__Útypesr*   ÚCrypto.Util.numberr   r   r   r   r   Ú<module>   s   