3
¬ �eÏ$  ã               @   sr   d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZmZ eddƒZG dd„ deƒZd	d
„ ZdS )é    )Ú	unhexlify)ÚbordÚtobytes)Úget_random_bytes)Úload_pycryptodome_raw_libÚVoidPointerÚSmartPointerÚcreate_string_bufferÚget_raw_bufferÚc_size_tÚc_uint8_ptrzCrypto.Hash._BLAKE2ba²  
                        int blake2b_init(void **state,
                                         const uint8_t *key,
                                         size_t key_size,
                                         size_t digest_size);
                        int blake2b_destroy(void *state);
                        int blake2b_update(void *state,
                                           const uint8_t *buf,
                                           size_t len);
                        int blake2b_digest(const void *state,
                                           uint8_t digest[64]);
                        int blake2b_copy(const void *src, void *dst);
                        c               @   sL   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚBLAKE2b_Hasha–  A BLAKE2b hash object.
    Do not instantiate directly. Use the :func:`new` function.

    :ivar oid: ASN.1 Object ID
    :vartype oid: string

    :ivar block_size: the size in bytes of the internal message block,
                      input to the compression function
    :vartype block_size: integer

    :ivar digest_size: the size in bytes of the resulting hash
    :vartype digest_size: integer
    é@   c             C   sŒ   || _ || _d| _|dkr.| r.dt|ƒ | _tƒ }tj|jƒ t	|ƒt
t|ƒƒt
|ƒƒ}|rhtd| ƒ‚t|jƒ tjƒ| _|rˆ| j|ƒ d S )	NFé   é    é0   r   z1.3.6.1.4.1.1722.12.2.1.z$Error %d while instantiating BLAKE2b)r   r   r   r   )Údigest_sizeÚ_update_after_digestÚ_digest_doneÚstrZoidr   Ú_raw_blake2b_libZblake2b_initZ
address_ofr   r   ÚlenÚ
ValueErrorr   ÚgetZblake2b_destroyÚ_stateÚupdate)ÚselfÚdataÚkeyÚdigest_bytesÚupdate_after_digestÚstateÚresult© r#   úZ/var/www/html/wholesaler-api-python-env/lib/python3.6/site-packages/Crypto/Hash/BLAKE2b.pyÚ__init__L   s     



zBLAKE2b_Hash.__init__c             C   sJ   | j r| j rtdƒ‚tj| jjƒ t|ƒtt	|ƒƒƒ}|rFt
d| ƒ‚| S )zµContinue hashing of a message by consuming the next chunk of data.

        Args:
            data (bytes/bytearray/memoryview): The next chunk of the message being hashed.
        z8You can only call 'digest' or 'hexdigest' on this objectz#Error %d while hashing BLAKE2b data)r   r   Ú	TypeErrorr   Zblake2b_updater   r   r   r   r   r   )r   r   r"   r#   r#   r$   r   f   s    zBLAKE2b_Hash.updatec             C   sB   t dƒ}tj| jjƒ |ƒ}|r*td| ƒ‚d| _t|ƒd| j… S )zçReturn the **binary** (non-printable) digest of the message that has been hashed so far.

        :return: The hash digest, computed over the data processed so far.
                 Binary form.
        :rtype: byte string
        r   z&Error %d while creating BLAKE2b digestTN)	r	   r   Zblake2b_digestr   r   r   r   r
   r   )r   Zbfrr"   r#   r#   r$   Údigestx   s    zBLAKE2b_Hash.digestc             C   s   dj dd„ t| jƒ ƒD ƒƒS )zÝReturn the **printable** digest of the message that has been hashed so far.

        :return: The hash digest, computed over the data processed so far.
                 Hexadecimal encoded.
        :rtype: string
        Ú c             S   s   g | ]}d t |ƒ ‘qS )z%02x)r   )Ú.0Úxr#   r#   r$   ú
<listcomp>“   s    z*BLAKE2b_Hash.hexdigest.<locals>.<listcomp>)ÚjoinÚtupler'   )r   r#   r#   r$   Ú	hexdigest‹   s    zBLAKE2b_Hash.hexdigestc             C   sD   t dƒ}td||d�}td|| jƒ d�}|jƒ |jƒ kr@tdƒ‚dS )a`  Verify that a given **binary** MAC (computed by another party)
        is valid.

        Args:
          mac_tag (bytes/bytearray/memoryview): the expected MAC of the message.

        Raises:
            ValueError: if the MAC does not match. It means that the message
                has been tampered with or that the MAC key is incorrect.
        é   é    )Údigest_bitsr   r   zMAC check failedN)r   Únewr'   r   )r   Zmac_tagZsecretZmac1Zmac2r#   r#   r$   Úverify–   s
    zBLAKE2b_Hash.verifyc             C   s   | j tt|ƒƒƒ dS )an  Verify that a given **printable** MAC (computed by another party)
        is valid.

        Args:
            hex_mac_tag (string): the expected MAC of the message, as a hexadecimal string.

        Raises:
            ValueError: if the MAC does not match. It means that the message
                has been tampered with or that the MAC key is incorrect.
        N)r3   r   r   )r   Zhex_mac_tagr#   r#   r$   Ú	hexverify«   s    zBLAKE2b_Hash.hexverifyc             K   s$   d|krd|kr| j |d< tf |ŽS )zQReturn a new instance of a BLAKE2b hash object.
        See :func:`new`.
        r   r1   )r   r2   )r   Úkwargsr#   r#   r$   r2   º   s    
zBLAKE2b_Hash.newN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
block_sizer%   r   r'   r.   r3   r4   r2   r#   r#   r#   r$   r   :   s   r   c              K   sò   | j ddƒ}| j ddƒ}| j ddƒ}| j ddƒ}d||fkrDtdƒ‚d||fkrTd}|dk	r~d	|  kondkn  s°td
ƒ‚n2d|  ko�dkn   s |d r¨tdƒ‚|d }| j ddƒ}t|ƒdkrÐtdƒ‚| rätdt| ƒ ƒ‚t||||ƒS )a×  Create a new hash object.

    Args:
        data (bytes/bytearray/memoryview):
            Optional. The very first chunk of the message to hash.
            It is equivalent to an early call to :meth:`BLAKE2b_Hash.update`.
        digest_bytes (integer):
            Optional. The size of the digest, in bytes (1 to 64). Default is 64.
        digest_bits (integer):
            Optional and alternative to ``digest_bytes``.
            The size of the digest, in bits (8 to 512, in steps of 8).
            Default is 512.
        key (bytes/bytearray/memoryview):
            Optional. The key to use to compute the MAC (1 to 64 bytes).
            If not specified, no key will be used.
        update_after_digest (boolean):
            Optional. By default, a hash object cannot be updated anymore after
            the digest is computed. When this flag is ``True``, such check
            is no longer enforced.

    Returns:
        A :class:`BLAKE2b_Hash` hash object
    r   Nr    Fr   r1   z*Only one digest parameter must be providedr   é   z!'digest_bytes' not in range 1..64é   i   z2'digest_bits' not in range 8..512, with steps of 8r   ó    z"BLAKE2b key cannot exceed 64 byteszUnknown parameters: )NN)Úpopr&   r   r   r   r   )r5   r   r    r   r1   r   r#   r#   r$   r2   Å   s(    
"r2   N)Úbinasciir   ZCrypto.Util.py3compatr   r   ZCrypto.Randomr   ZCrypto.Util._raw_apir   r   r   r	   r
   r   r   r   Úobjectr   r2   r#   r#   r#   r$   Ú<module>   s   $ 