o
    n~b$                     @   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Cryptodome.Hash._BLAKE2sa  
                        int blake2s_init(void **state,
                                         const uint8_t *key,
                                         size_t key_size,
                                         size_t digest_size);
                        int blake2s_destroy(void *state);
                        int blake2s_update(void *state,
                                           const uint8_t *buf,
                                           size_t len);
                        int blake2s_digest(const void *state,
                                           uint8_t digest[32]);
                        int blake2s_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 )BLAKE2s_Hasha  A BLAKE2s 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v r|sdt| | _t }t| t	|t
t|t
|}|r3td| t| tj| _|rE| | d S d S )NF)         r   z1.3.6.1.4.1.1722.12.2.2.z$Error %d while instantiating BLAKE2s)digest_size_update_after_digest_digest_donestroidr   _raw_blake2s_libZblake2s_initZ
address_ofr   r   len
ValueErrorr   getZblake2s_destroy_stateupdate)selfdatakeydigest_bytesupdate_after_digeststateresult r$   B/usr/local/lib/python3.10/dist-packages/Cryptodome/Hash/BLAKE2s.py__init__L   s&   

zBLAKE2s_Hash.__init__c                 C   sH   | j r
| js
tdt| j t|tt	|}|r"t
d| | S )zContinue hashing of a message by consuming the next chunk of data.

        Args:
            data (byte string/byte array/memoryview): The next chunk of the message being hashed.
        z8You can only call 'digest' or 'hexdigest' on this objectz#Error %d while hashing BLAKE2s data)r   r   	TypeErrorr   Zblake2s_updater   r   r   r   r   r   )r   r   r#   r$   r$   r%   r   f   s   
zBLAKE2s_Hash.updatec                 C   sB   t d}t| j |}|rtd| d| _t|d| j S )zReturn 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 BLAKE2s digestTN)	r	   r   Zblake2s_digestr   r   r   r   r
   r   )r   Zbfrr#   r$   r$   r%   digestx   s   zBLAKE2s_Hash.digestc                 C   s   d dd t|  D S )zReturn 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*BLAKE2s_Hash.hexdigest.<locals>.<listcomp>)jointupler(   )r   r$   r$   r%   	hexdigest   s   zBLAKE2s_Hash.hexdigestc                 C   sD   t d}td||d}td||  d}| | kr tddS )ag  Verify that a given **binary** MAC (computed by another party)
        is valid.

        Args:
          mac_tag (byte string/byte array/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.
        r      )digest_bitsr   r   zMAC check failedN)r   newr(   r   )r   Zmac_tagsecretZmac1Zmac2r$   r$   r%   verify   s   zBLAKE2s_Hash.verifyc                 C   s   |  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)r4   r   r   )r   Zhex_mac_tagr$   r$   r%   	hexverify   s   zBLAKE2s_Hash.hexverifyc                 K   s(   d|vrd|vr| j |d< tdi |S )zQReturn a new instance of a BLAKE2s hash object.
        See :func:`new`.
        r    r1   Nr$   )r   r2   )r   kwargsr$   r$   r%   r2      s   
zBLAKE2s_Hash.newN)__name__
__module____qualname____doc__
block_sizer&   r   r(   r/   r4   r5   r2   r$   r$   r$   r%   r   :   s    r   c                  K   s   |  dd}|  dd}|  dd}|  dd}d||fvr"tdd||fkr*d	}|durAd
|  kr;d	ks@td tdnd|  krKdkrTn td|d rXtd|d }|  dd}t|d	krltd| rvtdt|  t||||S )a  Create a new hash object.

    Args:
        data (byte string/byte array/memoryview):
            Optional. The very first chunk of the message to hash.
            It is equivalent to an early call to :meth:`BLAKE2s_Hash.update`.
        digest_bytes (integer):
            Optional. The size of the digest, in bytes (1 to 32). Default is 32.
        digest_bits (integer):
            Optional and alternative to ``digest_bytes``.
            The size of the digest, in bits (8 to 256, in steps of 8).
            Default is 256.
        key (byte string):
            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:`BLAKE2s_Hash` hash object
    r   Nr!   Fr    r1   z*Only one digest parameter must be provided)NNr      z!'digest_bytes' not in range 1..32      z3'digest_bytes' not in range 8..256, with steps of 8r       z"BLAKE2s key cannot exceed 32 byteszUnknown parameters: )popr'   r   r   r   r   )r6   r   r!   r    r1   r   r$   r$   r%   r2      s2   r2   N)binasciir   ZCryptodome.Util.py3compatr   r   ZCryptodome.Randomr   ZCryptodome.Util._raw_apir   r   r   r	   r
   r   r   r   objectr   r2   r$   r$   r$   r%   <module>   s   $ 