U
    ΂d"                     @   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_sizeKeyZSaltZPersonN)r   
isinstancebytesexc	TypeErrorintr
   _TOOBIGformat
ValueErrorlenr   	_OVERLONGr   r   )r   r   r   r    r"   ]/var/www/html/myproject/myenv/lib/python3.8/site-packages/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.           r3   _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 )r5   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   r7   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__)r8   r"   r"   r#   
__reduce__   s    zBlake2State.__reduce__)r8   r   c                 C   s"   |  | j}t|j| jt |S )N)r:   r   r   r,   r7   r   )r8   Z_str"   r"   r#   copy   s      zBlake2State.copyN)r;   
__module____qualname____doc__	__slots__r   r9   r   r<   r4   r=   r"   r"   r"   r#   r5      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$   r5   r   r+   r   r   r,   r    r   Z-crypto_generichash_blake2b_init_salt_personalr7   r   r   r-   )r   r   r   r   stater0   r1   r2   r"   r"   r#   generichash_blake2b_init   s          rC   )rB   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 )zUpdate 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   r5   r   r   r   r   Z!crypto_generichash_blake2b_updater7   r    r-   )rB   r&   r2   r"   r"   r#   generichash_blake2b_update   s     
  rE   )rB   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
    rD   r   r(   r   r*   N)r   r   r5   r   r   r   r+   r
   r   Z crypto_generichash_blake2b_finalr7   r   r-   r.   )rB   Z_digestr2   r"   r"   r#   generichash_blake2b_final  s    
  rF   ))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$   r3   r4   r5   rC   rE   rF   r"   r"   r"   r#   <module>   sf    


   780