o
    ç÷¡gÈ9  ã                   @   sÎ   d dl mZmZmZmZmZ d dlZd dlmZ d dlm	Z
 d dlmZ d dlmZmZmZ G dd„ dejeƒZG d	d
„ d
ejeƒZeddd�ZG dd„ dejeƒZedeeƒZG dd„ dee ejeƒZdS )é    )ÚClassVarÚGenericÚOptionalÚTypeÚTypeVarN)Úencoding)Ú
exceptions)ÚEncoder)ÚEncryptedMessageÚStringFixerÚrandomc                   @   s€   e Zd ZU dZejjZee	 e
d< ejfdedejfdd„Zdefdd	„Zde	fd
d„Zdedefdd„Zdedefdd„ZdS )Ú	PublicKeya=  
    The public key counterpart to an Curve25519 :class:`nacl.public.PrivateKey`
    for encrypting messages.

    :param public_key: [:class:`bytes`] Encoded Curve25519 public key
    :param encoder: A class that is able to decode the `public_key`

    :cvar SIZE: The size that the public key is required to be
    ÚSIZEÚ
public_keyÚencoderc                 C   sH   |  |¡| _t| jtƒst d¡‚t| jƒ| jkr"t d 	| j¡¡‚d S )Nz'PublicKey must be created from 32 bytesz,The public key must be exactly {} bytes long)
ÚdecodeÚ_public_keyÚ
isinstanceÚbytesÚexcÚ	TypeErrorÚlenr   Ú
ValueErrorÚformat)Úselfr   r   © r   ú6/usr/local/lib/python3.10/dist-packages/nacl/public.pyÚ__init__$   s   
ÿÿÿzPublicKey.__init__Úreturnc                 C   ó   | j S ©N©r   ©r   r   r   r   Ú	__bytes__4   ó   zPublicKey.__bytes__c                 C   s   t t| ƒƒS r    )Úhashr   r"   r   r   r   Ú__hash__7   s   zPublicKey.__hash__Úotherc                 C   s&   t || jƒsdS tj t| ƒt|ƒ¡S ©NF)r   Ú	__class__ÚnaclÚbindingsÚsodium_memcmpr   ©r   r'   r   r   r   Ú__eq__:   s   zPublicKey.__eq__c                 C   ó
   | |k S r    r   r-   r   r   r   Ú__ne__?   ó   
zPublicKey.__ne__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r*   r+   Úcrypto_box_PUBLICKEYBYTESr   r   ÚintÚ__annotations__r   Ú
RawEncoderr   r	   r   r#   r&   ÚobjectÚboolr.   r0   r   r   r   r   r      s   
 
ýþ
ýr   c                   @   sÄ   e Zd ZU dZejjZee	 e
d< ejjZee	 e
d< ejfdedejfdd„Zeejfdedejd	d fd
d„ƒZd	efdd„Zd	e	fdd„Zded	efdd„Zded	efdd„Zeddd„ƒZdS )Ú
PrivateKeya{  
    Private key for decrypting messages using the Curve25519 algorithm.

    .. warning:: This **must** be protected and remain secret. Anyone who
        knows the value of your :class:`~nacl.public.PrivateKey` can decrypt
        any message encrypted by the corresponding
        :class:`~nacl.public.PublicKey`

    :param private_key: The private key used to decrypt messages
    :param encoder: The encoder class used to decode the given keys

    :cvar SIZE: The size that the private key is required to be
    :cvar SEED_SIZE: The size that the seed used to generate the
                     private key is required to be
    r   Ú	SEED_SIZEÚprivate_keyr   c                 C   sT   |  |¡}t|tƒrt|ƒ| jkst d | j¡¡‚tj	 
|¡}|| _t|ƒ| _d S )Nz>PrivateKey must be created from a {} bytes long raw secret key)r   r   r   r   r   r   r   r   r*   r+   Úcrypto_scalarmult_baseÚ_private_keyr   r   )r   r>   r   Úraw_public_keyr   r   r   r   W   s   
ÿüzPrivateKey.__init__Úseedr   c                 C   sL   |  |¡}t|tƒrt|ƒ| jkst d | j¡¡‚tj	 
|¡\}}| |ƒS )ah  
        Generate a PrivateKey using a deterministic construction
        starting from a caller-provided seed

        .. warning:: The seed **must** be high-entropy; therefore,
            its generator **must** be a cryptographic quality
            random function like, for example, :func:`~nacl.utils.random`.

        .. warning:: The seed **must** be protected and remain secret.
            Anyone who knows the seed is really in possession of
            the corresponding PrivateKey.

        :param seed: The seed used to generate the private key
        :rtype: :class:`~nacl.public.PrivateKey`
        z7PrivateKey seed must be a {} bytes long binary sequence)r   r   r   r   r=   r   r   r   r*   r+   Úcrypto_box_seed_keypair)ÚclsrB   r   Úraw_pkÚraw_skr   r   r   Ú	from_seedn   s   
üzPrivateKey.from_seedc                 C   r   r    )r@   r"   r   r   r   r#   ’   r$   zPrivateKey.__bytes__c                 C   s   t t| ƒt| jƒfƒS r    )r%   Útyper   r   r"   r   r   r   r&   •   s   zPrivateKey.__hash__r'   c                 C   s   t || jƒsdS | j|jkS r(   )r   r)   r   r-   r   r   r   r.   ˜   s   zPrivateKey.__eq__c                 C   r/   r    r   r-   r   r   r   r0   �   r1   zPrivateKey.__ne__c                 C   s   | t tjƒtjd�S )z~
        Generates a random :class:`~nacl.public.PrivateKey` object

        :rtype: :class:`~nacl.public.PrivateKey`
        ©r   )r   r<   r   r   r9   )rD   r   r   r   Úgenerate    s   zPrivateKey.generateN)r   r<   )r2   r3   r4   r5   r*   r+   Úcrypto_box_SECRETKEYBYTESr   r   r7   r8   Úcrypto_box_SEEDBYTESr=   r   r9   r   r	   r   ÚclassmethodrG   r#   r&   r:   r;   r.   r0   rJ   r   r   r   r   r<   C   s2   
 ýþ
ýýþýü#r<   Ú_BoxÚBox)Úboundc                
   @   sÔ   e Zd ZU dZejjZee	 e
d< ee
d< dedefdd„Zdefd	d
„Zeejfdee dededefdd„ƒZdejfdedee dejdefdd„Zdejfdedee dejdefdd„Zdefdd„ZdS )rO   a/  
    The Box class boxes and unboxes messages between a pair of keys

    The ciphertexts generated by :class:`~nacl.public.Box` include a 16
    byte authenticator which is checked as part of the decryption. An invalid
    authenticator will cause the decrypt function to raise an exception. The
    authenticator is not a signature. Once you've decrypted the message you've
    demonstrated the ability to create arbitrary valid message, so messages you
    send are repudiable. For non-repudiable messages, sign them after
    encryption.

    :param private_key: :class:`~nacl.public.PrivateKey` used to encrypt and
        decrypt messages
    :param public_key: :class:`~nacl.public.PublicKey` used to encrypt and
        decrypt messages

    :cvar NONCE_SIZE: The size that the nonce is required to be.
    Ú
NONCE_SIZEÚ_shared_keyr>   r   c                 C   sF   t |tƒr
t |tƒst d¡‚tj |jt	j
d�|jt	j
d�¡| _d S )Nz5Box must be created from a PrivateKey and a PublicKeyrI   )r   r<   r   r   r   r*   r+   Úcrypto_box_beforenmÚencoder   r9   rR   )r   r>   r   r   r   r   r   Ä   s   ÿÿ
þzBox.__init__r   c                 C   r   r    ©rR   r"   r   r   r   r#   Ð   r$   zBox.__bytes__rD   Úencodedr   c                 C   s   |   | ¡}| |¡|_|S )z[
        Alternative constructor. Creates a Box from an existing Box's shared key.
        )Ú__new__r   rR   )rD   rV   r   Úboxr   r   r   r   Ó   s   
z
Box.decodeNÚ	plaintextÚnoncec                 C   sn   |du r	t | jƒ}t|ƒ| jkrt d| j ¡‚tj ||| j¡}| 	|¡}| 	|¡}t
 ||| 	|| ¡¡S )a¯  
        Encrypts the plaintext message using the given `nonce` (or generates
        one randomly if omitted) and returns the ciphertext encoded with the
        encoder.

        .. warning:: It is **VITALLY** important that the nonce is a nonce,
            i.e. it is a number used only once for any given key. If you fail
            to do this, you compromise the privacy of the messages encrypted.

        :param plaintext: [:class:`bytes`] The plaintext message to encrypt
        :param nonce: [:class:`bytes`] The nonce to use in the encryption
        :param encoder: The encoder to use to encode the ciphertext
        :rtype: [:class:`nacl.utils.EncryptedMessage`]
        Nú'The nonce must be exactly %s bytes long)r   rQ   r   r   r   r*   r+   Úcrypto_box_afternmrR   rT   r
   Ú_from_parts)r   rY   rZ   r   Ú
ciphertextÚencoded_nonceÚencoded_ciphertextr   r   r   Úencryptâ   s$   
ÿý

ýzBox.encryptr^   c                 C   sb   |  |¡}|du r|d| j… }|| jd… }t|ƒ| jkr&t d| j ¡‚tj ||| j¡}|S )aá  
        Decrypts the ciphertext using the `nonce` (explicitly, when passed as a
        parameter or implicitly, when omitted, as part of the ciphertext) and
        returns the plaintext message.

        :param ciphertext: [:class:`bytes`] The encrypted message to decrypt
        :param nonce: [:class:`bytes`] The nonce used when encrypting the
            ciphertext
        :param encoder: The encoder used to decode the ciphertext.
        :rtype: [:class:`bytes`]
        Nr[   )	r   rQ   r   r   r   r*   r+   Úcrypto_box_open_afternmrR   )r   r^   rZ   r   rY   r   r   r   Údecrypt  s   
ÿýzBox.decryptc                 C   r   )a§  
        Returns the Curve25519 shared secret, that can then be used as a key in
        other symmetric ciphers.

        .. warning:: It is **VITALLY** important that you use a nonce with your
            symmetric cipher. If you fail to do this, you compromise the
            privacy of the messages encrypted. Ensure that the key length of
            your cipher is 32 bytes.
        :rtype: [:class:`bytes`]
        rU   r"   r   r   r   Ú
shared_key3  s   zBox.shared_key)r2   r3   r4   r5   r*   r+   Úcrypto_box_NONCEBYTESrQ   r   r7   r8   r   r<   r   r   r#   rM   r   r9   r   rN   r	   r   r   r
   ra   rc   rd   r   r   r   r   rO   ­   sP   
 ÿÿÿÿþüþýü
û.üþýü
û&Ú_Keyc                	   @   s‚   e Zd ZU dZeed< ee ed< defdd„Zdefdd	„Z	e
jfd
ede
jdefdd„Ze
jfdddede
jdefdd„ZdS )Ú	SealedBoxa_  
    The SealedBox class boxes and unboxes messages addressed to
    a specified key-pair by using ephemeral sender's keypairs,
    whose private part will be discarded just after encrypting
    a single plaintext message.

    The ciphertexts generated by :class:`~nacl.public.SecretBox` include
    the public part of the ephemeral key before the :class:`~nacl.public.Box`
    ciphertext.

    :param recipient_key: a :class:`~nacl.public.PublicKey` used to encrypt
        messages and derive nonces, or a :class:`~nacl.public.PrivateKey` used
        to decrypt messages.

    .. versionadded:: 1.2
    r   r@   Úrecipient_keyc                 C   s^   t |tƒr|jtjd�| _d | _d S t |tƒr*|jtjd�| _|jjtjd�| _d S t	 
d¡‚)NrI   z:SealedBox must be created from a PublicKey or a PrivateKey)r   r   rT   r   r9   r   r@   r<   r   r   r   )r   rh   r   r   r   r   Z  s   
ÿ

ÿÿÿzSealedBox.__init__r   c                 C   r   r    r!   r"   r   r   r   r#   l  r$   zSealedBox.__bytes__rY   r   c                 C   s   t j || j¡}| |¡}|S )ay  
        Encrypts the plaintext message using a random-generated ephemeral
        keypair and returns a "composed ciphertext", containing both
        the public part of the keypair and the ciphertext proper,
        encoded with the encoder.

        The private part of the ephemeral key-pair will be scrubbed before
        returning the ciphertext, therefore, the sender will not be able to
        decrypt the generated ciphertext.

        :param plaintext: [:class:`bytes`] The plaintext message to encrypt
        :param encoder: The encoder to use to encode the ciphertext
        :return bytes: encoded ciphertext
        )r*   r+   Úcrypto_box_sealr   rT   )r   rY   r   r^   r`   r   r   r   ra   o  s   
zSealedBox.encryptr   zSealedBox[PrivateKey]r^   c                 C   s4   |  |¡}| jdu rtdƒ‚tj || j| j¡}|S )a  
        Decrypts the ciphertext using the ephemeral public key enclosed
        in the ciphertext and the SealedBox private key, returning
        the plaintext message.

        :param ciphertext: [:class:`bytes`] The encrypted message to decrypt
        :param encoder: The encoder used to decode the ciphertext.
        :return bytes: The original plaintext
        :raises TypeError: if this SealedBox was created with a
            :class:`~nacl.public.PublicKey` rather than a
            :class:`~nacl.public.PrivateKey`.
        Nz4SealedBoxes created with a public key cannot decrypt)r   r@   r   r*   r+   Úcrypto_box_seal_openr   )r   r^   r   rY   r   r   r   rc   ‰  s   

ÿýzSealedBox.decryptN)r2   r3   r4   r5   r   r8   r   rf   r   r#   r   r9   r	   ra   rc   r   r   r   r   rg   E  s0   
 ýþý
üýÿþýürg   )Útypingr   r   r   r   r   Únacl.bindingsr*   r   r   r   Únacl.encodingr	   Ú
nacl.utilsr
   r   r   Ú	Encodabler   r<   rN   rO   rf   rg   r   r   r   r   Ú<module>   s   ,g 