U
    Øcc”"  ã                   @   sj  U d dl mZ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 ¡ Zeed< e ¡ Zeed< e ¡ Zeed< e ¡ 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ZdZ ee!e!e!ddœdd„Z"edddfe!ee!e!e!e!dœdd„Z#edddZ$G dd„ dƒZ%dddefe!e!e!ee%dœdd„Z&e%e!ddœd d!„Z'e%e!d"œd#d$„Z(dS )%é    )ÚNoReturnÚTypeVar)Ú
exceptions)ÚffiÚlib)ÚensureÚcrypto_generichash_BYTESÚcrypto_generichash_BYTES_MINÚcrypto_generichash_BYTES_MAXÚcrypto_generichash_KEYBYTESÚcrypto_generichash_KEYBYTES_MINÚcrypto_generichash_KEYBYTES_MAXÚcrypto_generichash_SALTBYTESÚ crypto_generichash_PERSONALBYTESÚcrypto_generichash_STATEBYTESz!{0} length greater than {1} bytesz{0} greater than {1}N)Údigest_sizeÚkeyÚsaltÚpersonÚreturnc                 C   sØ   t t|tƒdtjd t t|tƒdtjd t t|tƒdtjd t t| tƒdtjd t | tkt dt¡tj	d t t
|ƒtkt dt¡tj	d t t
|ƒtkt dt¡tj	d t t
|ƒtkt d	t¡tj	d d
S )zCheck hash parameterszKey must be a bytes sequence©ZraisingzSalt must be a bytes sequencezPerson must be a bytes sequencez%Digest size must be an integer numberZDigest_sizeZKeyZSaltZPersonN)r   Ú
isinstanceÚbytesÚexcÚ	TypeErrorÚintr
   Ú_TOOBIGÚformatÚ
ValueErrorÚlenr   Ú	_OVERLONGr   r   )r   r   r   r   © r!   úD/tmp/pip-unpacked-wheel-e6sdgn5m/nacl/bindings/crypto_generichash.pyÚ_checkparams)   sP    ýýýý
ý

ý

ý

ýr#   ó    )Údatar   r   r   r   r   c           	   
   C   s´   t ||||ƒ tt| tƒdtjd t d|¡}t dt¡}t dt	¡}t 
||t|ƒ¡ t 
||t|ƒ¡ t ||| t| ƒ|t|ƒ||¡}t|dkdtjd t ||¡dd… S )ab  One shot hash interface

    :param data: the input data to the hash function
    :type data: bytes
    :param digest_size: must be at most
                        :py:data:`.crypto_generichash_BYTES_MAX`;
                        the default digest size is
                        :py:data:`.crypto_generichash_BYTES`
    :type digest_size: int
    :param key: must be at most
                :py:data:`.crypto_generichash_KEYBYTES_MAX` long
    :type key: bytes
    :param salt: must be at most
                 :py:data:`.crypto_generichash_SALTBYTES` long;
                 will be zero-padded if needed
    :type salt: bytes
    :param person: must be at most
                   :py:data:`.crypto_generichash_PERSONALBYTES` long:
                   will be zero-padded if needed
    :type person: bytes
    :return: digest_size long digest
    :rtype: bytes
    ú#Input data must be a bytes sequencer   úunsigned char[]úunsigned char []r   úUnexpected failureN)r#   r   r   r   r   r   r   Únewr   r   Úmemmover   r   Z(crypto_generichash_blake2b_salt_personalÚRuntimeErrorÚbuffer)	r%   r   r   r   r   ÚdigestÚ_saltÚ_personÚrcr!   r!   r"   Ú!generichash_blake2b_salt_personal^   s.    ý       ÿr2   Ú_Blake2StateÚBlake2State)Úboundc                   @   sD   e Zd ZdZddgZedœdd„Zedœdd	„Ze	e	d
œdd„Z
dS )r4   zN
    Python-level wrapper for the crypto_generichash_blake2b state buffer
    Ú	_statebufr   )r   c                 C   s   t  dt¡| _|| _d S )Nr'   )r   r*   r   r6   r   )Úselfr   r!   r!   r"   Ú__init__    s
     ÿzBlake2State.__init__)r   c                 C   s   t d | jj¡ƒ‚dS )zc
        Raise the same exception as hashlib's blake implementation
        on copy.copy()
        zcan't pickle {} objectsN)r   r   Ú	__class__Ú__name__)r7   r!   r!   r"   Ú
__reduce__¦   s    ÿzBlake2State.__reduce__)r7   r   c                 C   s"   |   | j¡}t |j| jt¡ |S )N)r9   r   r   r+   r6   r   )r7   Z_str!   r!   r"   Úcopy¯   s      ÿzBlake2State.copyN)r:   Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r8   r   r;   r3   r<   r!   r!   r!   r"   r4   ™   s
   	)r   r   r   r   r   c                 C   s„   t || ||ƒ t|ƒ}t dt¡}t dt¡}t ||t|ƒ¡ t ||t|ƒ¡ t 	|j
| t| ƒ|||¡}t|dkdtjd |S )a@  
    Create a new initialized blake2b hash state

    :param key: must be at most
                :py:data:`.crypto_generichash_KEYBYTES_MAX` long
    :type key: bytes
    :param salt: must be at most
                 :py:data:`.crypto_generichash_SALTBYTES` long;
                 will be zero-padded if needed
    :type salt: bytes
    :param person: must be at most
                   :py:data:`.crypto_generichash_PERSONALBYTES` long:
                   will be zero-padded if needed
    :type person: bytes
    :param digest_size: must be at most
                        :py:data:`.crypto_generichash_BYTES_MAX`;
                        the default digest size is
                        :py:data:`.crypto_generichash_BYTES`
    :type digest_size: int
    :return: a initialized :py:class:`.Blake2State`
    :rtype: object
    r(   r   r)   r   )r#   r4   r   r*   r   r   r+   r   r   Z-crypto_generichash_blake2b_init_salt_personalr6   r   r   r,   )r   r   r   r   Ústater/   r0   r1   r!   r!   r"   Úgenerichash_blake2b_init·   s          ÿrB   )rA   r%   r   c                 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 blake2b hash state

    :param state: a initialized Blake2bState object as returned from
                     :py:func:`.crypto_generichash_blake2b_init`
    :type state: :py:class:`.Blake2State`
    :param data:
    :type data: bytes
    ú"State must be a Blake2State objectr   r&   r   r)   N)r   r   r4   r   r   r   r   Z!crypto_generichash_blake2b_updater6   r   r,   )rA   r%   r1   r!   r!   r"   Úgenerichash_blake2b_updateç   s     
ýý  ÿrD   )rA   r   c                 C   s^   t t| tƒdtjd t dt¡}t 	| j
|| j¡}t |dkdtjd t || j¡dd… S )a:  Finalize the blake2b hash state and return the digest.

    :param state: a initialized Blake2bState object as returned from
                     :py:func:`.crypto_generichash_blake2b_init`
    :type state: :py:class:`.Blake2State`
    :return: the blake2 digest of the passed-in data stream
    :rtype: bytes
    rC   r   r'   r   r)   N)r   r   r4   r   r   r   r*   r
   r   Z crypto_generichash_blake2b_finalr6   r   r,   r-   )rA   Z_digestr1   r!   r!   r"   Úgenerichash_blake2b_final  s    
ý  ÿrE   ))Útypingr   r   Znaclr   r   Znacl._sodiumr   r   Znacl.exceptionsr   Z crypto_generichash_blake2b_bytesr   r   Ú__annotations__Z$crypto_generichash_blake2b_bytes_minr	   Z$crypto_generichash_blake2b_bytes_maxr
   Z#crypto_generichash_blake2b_keybytesr   Z'crypto_generichash_blake2b_keybytes_minr   Z'crypto_generichash_blake2b_keybytes_maxr   Z$crypto_generichash_blake2b_saltbytesr   Z(crypto_generichash_blake2b_personalbytesr   Zcrypto_generichash_statebytesr   r    r   r   r#   r2   r3   r4   rB   rD   rE   r!   r!   r!   r"   Ú<module>   sf    ÿ
ÿ
ÿ
   þ7ûú8üû0