o
    gf(                     @   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raisingN)
r   newr
   r   r   crypto_sign_keypairr   excRuntimeErrorbuffer)pkskrc r   S/var/www/html/api-tag/env/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   signed
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!   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*   curve_public_key_len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   )r0   curve_secret_key_lencurve_secret_keyr   r   r   r   r1      r/   r1   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	   r0   r   r   r   crypto_sign_ed25519_sk_to_pk      
r7   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
    r4   Nr5   r6   r   r   r   crypto_sign_ed25519_sk_to_seed   r8   r9   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   r;   r   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__r>   r   r   r   r   r:      s    r:   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   
isinstancer:   r   	TypeErrorbytesr   crypto_sign_ed25519ph_updater;   r   r   )rD   rE   r   r   r   r   rJ      s   rJ   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 )	aR  
    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 part of the signing key
    :type sk: bytes
    :return: ed25519ph signature
    :rtype: bytes
    rF   r   z+secret key parameter must be a bytes objectz secret key must be {} bytes longr   r   r   N)r   rG   r:   r   rH   rI   r   r   formatr   r   r   r   "crypto_sign_ed25519ph_final_creater;   NULLr   r   )rD   r   	signaturer   r   r   r   rL      s.   
rL   rN   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
    rF   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   rG   r:   r   rH   rI   r   r   rK   r
   r   "crypto_sign_ed25519ph_final_verifyr;   r(   )rD   rN   r   r   r   r   r   rO     s>   


rO   )%typingr   naclr   r   nacl._sodiumr   r   nacl.exceptionsr   crypto_sign_bytesr   int__annotations__crypto_sign_secretkeybytesr	   crypto_sign_publickeybytesr
   r   crypto_box_secretkeybytesr    crypto_sign_ed25519ph_statebytesr   rI   r   r    r#   r'   r,   r1   r7   r9   r:   rJ   rL   boolrO   r   r   r   r   <module>   sT   

)