U
    ÎZj‘   ã                   @   sˆ   d dl mZ d dlZd dlmZ d dlmZ d dlmZ	m
Z d dlmZmZ G dd„ deƒZG d	d
„ d
ejeƒZG dd„ dejeƒZdS )é    )ÚOptionalN)Úencoding)Ú
exceptions)Ú
PrivateKeyÚ	PublicKey)ÚStringFixerÚrandomc                   @   s^   e Zd ZU dZeed< eed< eeeed dœdd„ƒZeedœdd	„ƒZ	eedœd
d„ƒZ
dS )ÚSignedMessagezc
    A bytes subclass that holds a messaged that has been signed by a
    :class:`SigningKey`.
    Ú
_signatureÚ_message)Ú	signatureÚmessageÚcombinedÚreturnc                 C   s   | |ƒ}||_ ||_|S ©N)r
   r   )Úclsr   r   r   Úobj© r   úO/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/nacl/signing.pyÚ_from_parts#   s    zSignedMessage._from_parts©r   c                 C   s   | j S )zL
        The signature contained within the :class:`SignedMessage`.
        )r
   ©Úselfr   r   r   r   ,   s    zSignedMessage.signaturec                 C   s   | j S )zJ
        The message contained within the :class:`SignedMessage`.
        )r   r   r   r   r   r   3   s    zSignedMessage.messageN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚbytesÚ__annotations__Úclassmethodr   Úpropertyr   r   r   r   r   r   r	      s   
  þr	   c                   @   s”   e Zd ZdZejfeejdœdd„Zedœdd„Z	e
dœdd	„Zeed
œdd„Zeed
œdd„Zdejfeee ejedœdd„Zedœdd„ZdS )Ú	VerifyKeyzà
    The public key counterpart to an Ed25519 SigningKey for producing digital
    signatures.

    :param key: [:class:`bytes`] Serialized Ed25519 public key
    :param encoder: A class that is able to decode the `key`
    )ÚkeyÚencoderc                 C   sJ   |  |¡}t|tƒst d¡‚t|ƒtjjkr@t 	dtjj ¡‚|| _
d S )Nz'VerifyKey must be created from 32 bytesz%The key must be exactly %s bytes long)ÚdecodeÚ
isinstancer   ÚexcÚ	TypeErrorÚlenÚnaclÚbindingsZcrypto_sign_PUBLICKEYBYTESÚ
ValueErrorÚ_key)r   r"   r#   r   r   r   Ú__init__D   s    


ÿÿzVerifyKey.__init__r   c                 C   s   | j S r   )r,   r   r   r   r   Ú	__bytes__T   s    zVerifyKey.__bytes__c                 C   s   t t| ƒƒS r   ©Úhashr   r   r   r   r   Ú__hash__W   s    zVerifyKey.__hash__©Úotherr   c                 C   s&   t || jƒsdS tj t| ƒt|ƒ¡S ©NF©r%   Ú	__class__r)   r*   Zsodium_memcmpr   ©r   r3   r   r   r   Ú__eq__Z   s    zVerifyKey.__eq__c                 C   s
   | |k S r   r   r7   r   r   r   Ú__ne___   s    zVerifyKey.__ne__N)Úsmessager   r#   r   c                 C   sp   |dk	rVt |tƒs$t dtjj ¡‚t|ƒtjjkrFt dtjj ¡‚|| 	|¡ }n
| 	|¡}tj 
|| j¡S )aS  
        Verifies the signature of a signed message, returning the message
        if it has not been tampered with else raising
        :class:`~nacl.signing.BadSignatureError`.

        :param smessage: [:class:`bytes`] Either the original messaged or a
            signature and message concated together.
        :param signature: [:class:`bytes`] If an unsigned message is given for
            smessage then the detached signature must be provided.
        :param encoder: A class that is able to decode the secret message and
            signature.
        :rtype: :class:`bytes`
        Nz4Verification signature must be created from %d bytesz+The signature must be exactly %d bytes long)r%   r   r&   r'   r)   r*   Úcrypto_sign_BYTESr(   r+   r$   Zcrypto_sign_openr,   )r   r:   r   r#   r   r   r   Úverifyb   s     
ÿÿÿÿ
zVerifyKey.verifyc                 C   s   t j | j¡}t|ƒS )z›
        Converts a :class:`~nacl.signing.VerifyKey` to a
        :class:`~nacl.public.PublicKey`

        :rtype: :class:`~nacl.public.PublicKey`
        )r)   r*   Z$crypto_sign_ed25519_pk_to_curve25519r,   Ú_Curve25519_PublicKey)r   Zraw_pkr   r   r   Úto_curve25519_public_key‹   s    z"VerifyKey.to_curve25519_public_key)r   r   r   r   r   Ú
RawEncoderr   ÚEncoderr-   r.   Úintr1   ÚobjectÚboolr8   r9   r   r<   r=   r>   r   r   r   r   r!   ;   s&   	ÿ ÿüû)r!   c                   @   sž   e Zd ZdZejfeejdœdd„Zedœdd„Z	e
dœdd	„Zeed
œdd„Zeed
œdd„Zed dœdd„ƒZejfeejedœdd„Zedœdd„ZdS )Ú
SigningKeyaÙ  
    Private key for producing digital signatures using the Ed25519 algorithm.

    Signing keys are produced from a 32-byte (256-bit) random seed value. This
    value can be passed into the :class:`~nacl.signing.SigningKey` as a
    :func:`bytes` whose length is 32.

    .. warning:: This **must** be protected and remain secret. Anyone who knows
        the value of your :class:`~nacl.signing.SigningKey` or it's seed can
        masquerade as you.

    :param seed: [:class:`bytes`] Random 32-byte value (i.e. private key)
    :param encoder: A class that is able to decode the seed

    :ivar: verify_key: [:class:`~nacl.signing.VerifyKey`] The verify
        (i.e. public) key that corresponds with this signing key.
    )Úseedr#   c                 C   sj   |  |¡}t|tƒst d¡‚t|ƒtjjkr@t 	dtjj ¡‚tj 
|¡\}}|| _|| _t|ƒ| _d S )Nz.SigningKey must be created from a 32 byte seedz&The seed must be exactly %d bytes long)r$   r%   r   r&   r'   r(   r)   r*   Úcrypto_sign_SEEDBYTESr+   Zcrypto_sign_seed_keypairÚ_seedÚ_signing_keyr!   Z
verify_key)r   rE   r#   Z
public_keyZ
secret_keyr   r   r   r-   ©   s    

ÿÿÿzSigningKey.__init__r   c                 C   s   | j S r   )rG   r   r   r   r   r.   Â   s    zSigningKey.__bytes__c                 C   s   t t| ƒƒS r   r/   r   r   r   r   r1   Å   s    zSigningKey.__hash__r2   c                 C   s&   t || jƒsdS tj t| ƒt|ƒ¡S r4   r5   r7   r   r   r   r8   È   s    zSigningKey.__eq__c                 C   s
   | |k S r   r   r7   r   r   r   r9   Í   s    zSigningKey.__ne__c                 C   s   | t tjjƒtjd�S )z�
        Generates a random :class:`~nacl.signing.SigningKey` object.

        :rtype: :class:`~nacl.signing.SigningKey`
        )r#   )r   r)   r*   rF   r   r?   )r   r   r   r   ÚgenerateÐ   s    
þzSigningKey.generate)r   r#   r   c                 C   sT   t j || j¡}t jj}| |d|… ¡}| ||d… ¡}| |¡}t |||¡S )zñ
        Sign a message using this key.

        :param message: [:class:`bytes`] The data to be signed.
        :param encoder: A class that is used to encode the signed message.
        :rtype: :class:`~nacl.signing.SignedMessage`
        N)r)   r*   Zcrypto_signrH   r;   Úencoder	   r   )r   r   r#   Z
raw_signedr;   r   Úsignedr   r   r   ÚsignÜ   s    
zSigningKey.signc                 C   s   | j }tj |¡}t|ƒS )zž
        Converts a :class:`~nacl.signing.SigningKey` to a
        :class:`~nacl.public.PrivateKey`

        :rtype: :class:`~nacl.public.PrivateKey`
        )rH   r)   r*   Z$crypto_sign_ed25519_sk_to_curve25519Ú_Curve25519_PrivateKey)r   ÚskZraw_privater   r   r   Úto_curve25519_private_keyñ   s    z$SigningKey.to_curve25519_private_keyN)r   r   r   r   r   r?   r   r@   r-   r.   rA   r1   rB   rC   r8   r9   r   rI   r	   rL   rM   rO   r   r   r   r   rD   –   s&   ýýýürD   )Útypingr   Znacl.bindingsr)   r   r   r&   Znacl.publicr   rM   r   r=   Z
nacl.utilsr   r   r   r	   Z	Encodabler!   rD   r   r   r   r   Ú<module>   s   ![