U
    ֫[J                     @   s   d dl mZmZmZ 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 Ze Ze Ze Ze Ze Ze Ze ZdZ dZ!dd	 Z"ed
d
d
fddZ#d
d
d
efddZ$dd Z%dd Z&dd Z'dS )    )absolute_importdivisionprint_function)integer_types)
exceptions)ffilib)ensurez!{0} length greater than {1} bytesz{0} greater than {1}c                 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 paramtersz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	TypeErrorr   crypto_generichash_BYTES_MAX_TOOBIGformat
ValueErrorlencrypto_generichash_KEYBYTES_MAX	_OVERLONGcrypto_generichash_SALTBYTES crypto_generichash_PERSONALBYTES)digest_sizekeysaltperson r   B/usr/lib/python3/dist-packages/nacl/bindings/crypto_generichash.py_checkparams'   s@    







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 )aL  One shot hash interface

    :param data: the input data to the hash function
    :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)	datar   r   r   r   Zdigest_salt_personrcr   r   r   !generichash_blake2b_salt_personalJ   s,    
   
r,   c                 C   s   t || || tdt}tdt}tdt}t||t| t||t| t	|| t| |||}t
|dkdtjd |S )a7  
    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: an initizialized state buffer
    :rtype: object
    r!   r"   r   r#   r
   )r   r   r$   crypto_generichash_STATEBYTESr   r   r%   r   r   Z-crypto_generichash_blake2b_init_salt_personalr	   r   r&   )r   r   r   r   statebufr)   r*   r+   r   r   r   generichash_blake2b_init}   s"      
r/   c                 C   s@   t t|tdtjd t| |t|}t |dkdtjd dS )zUpdate the blake2b hash state

    :param statebuf: an initialized blake2b state buffer as returned from
                     :py:func:`.crypto_generichash_blake2b_init`
    :type statebuf: object
    :param data:
    :type data: bytes
    r    r
   r   r#   N)	r	   r   r   r   r   r   Z!crypto_generichash_blake2b_updater   r&   )r.   r(   r+   r   r   r   generichash_blake2b_update   s    


r0   c                 C   sB   t dt}t| ||}t|dkdtjd t ||dd S )zFinalize the blake2b hash state and return the digest.

    :param statebuf:
    :type statebuf: object
    :param digest_size:
    :type digest_size: int
    :return: the blake2 digest of the passed-in data stream
    :rtype: bytes
    r!   r   r#   r
   N)	r   r$   r   r   Z crypto_generichash_blake2b_finalr	   r   r&   r'   )r.   r   Z_digestr+   r   r   r   generichash_blake2b_final   s    
r1   c                 C   s   t dt}t || t |S )z-Return a copy of the given blake2b hash stater!   )r   r$   r-   r%   )r.   Znewstater   r   r   generichash_blake2b_state_copy   s    r2   N)(Z
__future__r   r   r   Zsixr   Znaclr   r   Znacl._sodiumr   r   Znacl.exceptionsr	   Z crypto_generichash_blake2b_bytesZcrypto_generichash_BYTESZ$crypto_generichash_blake2b_bytes_minZcrypto_generichash_BYTES_MINZ$crypto_generichash_blake2b_bytes_maxr   Z#crypto_generichash_blake2b_keybytesZcrypto_generichash_KEYBYTESZ'crypto_generichash_blake2b_keybytes_minZcrypto_generichash_KEYBYTES_MINZ'crypto_generichash_blake2b_keybytes_maxr   Z$crypto_generichash_blake2b_saltbytesr   Z(crypto_generichash_blake2b_personalbytesr   Zcrypto_generichash_statebytesr-   r   r   r   r,   r/   r0   r1   r2   r   r   r   r   <module>   s:   $  
3
/