o
    »­§ik(  ã                   @   s–  U d dl mZ d dlmZ d dlmZmZ d dlm	Z	 e 
¡ Zeed< e ¡ d Zeed< e ¡ Zeed< e ¡ Zeed	< e ¡ Zeed
< e ¡ Zeed< deeef fdd„Zdedeeef fdd„Zdededefdd„Zdededefdd„Zdedefdd„Zdedefdd„Zdedefd d!„Zdedefd"d#„ZG d$d%„ d%ƒZ d&e d'edd(fd)d*„Z!d&e dedefd+d,„Z"d&e d-edede#fd.d/„Z$d(S )0é    )ÚTuple)Ú
exceptions)ÚffiÚlib)ÚensureÚcrypto_sign_BYTESé   Úcrypto_sign_SEEDBYTESÚcrypto_sign_PUBLICKEYBYTESÚcrypto_sign_SECRETKEYBYTESÚcrypto_sign_curve25519_BYTESÚ crypto_sign_ed25519ph_STATEBYTESÚreturnc                  C   s`   t  dt¡} t  dt¡}t | |¡}t|dkdtjd t  	| t¡dd… t  	|t¡dd… fS )zu
    Returns a randomly generated public key and secret key.

    :rtype: (bytes(public_key), bytes(secret_key))
    úunsigned char[]r   úUnexpected library error©ZraisingN)
r   Únewr
   r   r   Úcrypto_sign_keypairr   ÚexcÚRuntimeErrorÚbuffer)ÚpkÚskÚrc© r   úP/home/app/Keep/.python/lib/python3.10/site-packages/nacl/bindings/crypto_sign.pyr       s   þr   Úseedc                 C   sx   t | ƒtkrt d¡‚t dt¡}t dt¡}t 	||| ¡}t
|dkdtjd t |t¡dd… t |t¡dd… fS )z 
    Computes and returns the public key and secret key using the seed ``seed``.

    :param seed: bytes
    :rtype: (bytes(public_key), bytes(secret_key))
    zInvalid seedr   r   r   r   N)Úlenr	   r   Ú
ValueErrorr   r   r
   r   r   Úcrypto_sign_seed_keypairr   r   r   )r   r   r   r   r   r   r   r   2   s   
þr   Úmessager   c                 C   s`   t  dt| ƒt ¡}t  d¡}t ||| t| ƒ|¡}t|dkdtjd t  	||d ¡dd… S )z©
    Signs the message ``message`` using the secret key ``sk`` and returns the
    signed message.

    :param message: bytes
    :param sk: bytes
    :rtype: bytes
    r   úunsigned long long *r   r   r   N)
r   r   r   r   r   Úcrypto_signr   r   r   r   )r    r   ÚsignedZ
signed_lenr   r   r   r   r"   H   s
   	
r"   r#   r   c                 C   sV   t  dt| ƒ¡}t  d¡}t ||| t| ƒ|¡dkrt d¡‚t  ||d ¡dd… S )zÄ
    Verifies the signature of the signed message ``signed`` using the public
    key ``pk`` and returns the unsigned message.

    :param signed: bytes
    :param pk: bytes
    :rtype: bytes
    r   r!   r   úSignature was forged or corruptN)r   r   r   r   Úcrypto_sign_openr   ÚBadSignatureErrorr   )r#   r   r    Zmessage_lenr   r   r   r%   Z   s   	
ÿ
r%   Úpublic_key_bytesc                 C   óZ   t | ƒtkrt d¡‚t}t d|¡}t || ¡}t	|dkdtj
d t ||¡dd… S )a  
    Converts a public Ed25519 key (encoded as bytes ``public_key_bytes``) to
    a public Curve25519 key as bytes.

    Raises a ValueError if ``public_key_bytes`` is not of length
    ``crypto_sign_PUBLICKEYBYTES``

    :param public_key_bytes: bytes
    :rtype: bytes
    zInvalid curve public keyr   r   r   r   N)r   r
   r   r   r   r   r   r   Ú$crypto_sign_ed25519_pk_to_curve25519r   r   r   )r'   Zcurve_public_key_lenZcurve_public_keyr   r   r   r   r)   o   ó   
ÿr)   Úsecret_key_bytesc                 C   r(   )a  
    Converts a secret Ed25519 key (encoded as bytes ``secret_key_bytes``) to
    a secret Curve25519 key as bytes.

    Raises a ValueError if ``secret_key_bytes``is not of length
    ``crypto_sign_SECRETKEYBYTES``

    :param secret_key_bytes: bytes
    :rtype: bytes
    zInvalid curve secret keyr   r   r   r   N)r   r   r   r   r   r   r   r   Ú$crypto_sign_ed25519_sk_to_curve25519r   r   r   )r+   Zcurve_secret_key_lenZcurve_secret_keyr   r   r   r   r,   ˆ   r*   r,   c                 C   s"   t | ƒtkrt d¡‚| td… S )a	  
    Extract the public Ed25519 key from a secret Ed25519 key (encoded
    as bytes ``secret_key_bytes``).

    Raises a ValueError if ``secret_key_bytes``is not of length
    ``crypto_sign_SECRETKEYBYTES``

    :param secret_key_bytes: bytes
    :rtype: bytes
    úInvalid secret keyN©r   r   r   r   r	   ©r+   r   r   r   Úcrypto_sign_ed25519_sk_to_pk¡   ó   
r0   c                 C   s"   t | ƒtkrt d¡‚| dt… S )zû
    Extract the seed from a secret Ed25519 key (encoded
    as bytes ``secret_key_bytes``).

    Raises a ValueError if ``secret_key_bytes``is not of length
    ``crypto_sign_SECRETKEYBYTES``

    :param secret_key_bytes: bytes
    :rtype: bytes
    r-   Nr.   r/   r   r   r   Úcrypto_sign_ed25519_sk_to_seed²   r1   r2   c                   @   s    e Zd ZdZdgZddd„ZdS )Úcrypto_sign_ed25519ph_statezO
    State object wrapping the sha-512 state used in ed25519ph computation
    Ústater   Nc                 C   s2   t  dt¡| _t | j¡}t|dkdtjd d S )Nr   r   r   r   )	r   r   r   r4   r   Zcrypto_sign_ed25519ph_initr   r   r   )Úselfr   r   r   r   Ú__init__Ê   s
   ÿz$crypto_sign_ed25519ph_state.__init__)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r6   r   r   r   r   r3   Ã   s    r3   ÚedphÚpmsgNc                 C   sX   t t| tƒdtjd t t|tƒdtjd t | j|t	|ƒ¡}t |dkdtj
d dS )zÛ
    Update the hash state wrapped in edph

    :param edph: the ed25519ph state being updated
    :type edph: crypto_sign_ed25519ph_state
    :param pmsg: the partial message
    :type pmsg: bytes
    :rtype: None
    ú/edph parameter must be a ed25519ph_state objectr   z%pmsg parameter must be a bytes objectr   r   N)r   Ú
isinstancer3   r   Ú	TypeErrorÚbytesr   Úcrypto_sign_ed25519ph_updater4   r   r   )r<   r=   r   r   r   r   rB   Ô   s   ýýrB   c                 C   s’   t t| tƒdtjd t t|tƒdtjd t t|ƒtkd t¡tjd t	 
dt¡}t | j|t	j|¡}t |dkdtjd t	 |t¡dd… S )	aW  
    Create a signature for the data hashed in edph
    using the secret key sk

    :param edph: the ed25519ph state for the data
                 being signed
    :type edph: crypto_sign_ed25519ph_state
    :param sk: the ed25519 secret key (secret and public part)
    :type sk: bytes
    :return: ed25519ph signature
    :rtype: bytes
    r>   r   z+secret key parameter must be a bytes objectz secret key must be {} bytes longr   r   r   N)r   r?   r3   r   r@   rA   r   r   Úformatr   r   r   r   Ú"crypto_sign_ed25519ph_final_creater4   ZNULLr   r   )r<   r   Ú	signaturer   r   r   r   rD   î   s.   ýý
ÿûÿrD   rE   c                 C   s¤   t t| tƒdtjd t t|tƒdtjd t t|ƒtkd t¡tjd t t|tƒdtjd t t|ƒt	kd t	¡tjd t
 | j||¡}|dkrPt d¡‚d	S )
aÚ  
    Verify a prehashed signature using the public key pk

    :param edph: the ed25519ph state for the data
                 being verified
    :type edph: crypto_sign_ed25519ph_state
    :param signature: the signature being verified
    :type signature: bytes
    :param pk: the ed25519 public part of the signing key
    :type pk: bytes
    :return: True if the signature is valid
    :rtype: boolean
    :raises exc.BadSignatureError: if the signature is not valid
    r>   r   z*signature parameter must be a bytes objectzsignature must be {} bytes longz+public key parameter must be a bytes objectz public key must be {} bytes longr   r$   T)r   r?   r3   r   r@   rA   r   r   rC   r
   r   Ú"crypto_sign_ed25519ph_final_verifyr4   r&   )r<   rE   r   r   r   r   r   rF     s>   ýý
ýý
ÿû
rF   )%Útypingr   Znaclr   r   Znacl._sodiumr   r   Znacl.exceptionsr   Zcrypto_sign_bytesr   ÚintÚ__annotations__Zcrypto_sign_secretkeybytesr	   Zcrypto_sign_publickeybytesr
   r   Zcrypto_box_secretkeybytesr   Z crypto_sign_ed25519ph_statebytesr   rA   r   r   r"   r%   r)   r,   r0   r2   r3   rB   rD   ÚboolrF   r   r   r   r   Ú<module>   sT   ÿÿ
þÿÿ
þ)ÿÿÿþ