o
    n~b                     @   sr   d dl mZmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ ddgZG d	d deZdddZdS )    )bordtobytes)	unhexlify)MD5)BLAKE2s)strxor)get_random_bytesnewHMACc                   @   sR   e Zd ZdZdd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 )r
   zAn HMAC hash object.
    Do not instantiate directly. Use the :func:`new` function.

    :ivar digest_size: the size in bytes of the resulting MAC tag
    :vartype digest_size: integer
        Nc                 C   s   |d u rt }|d u rd}|j| _|| _t|tr| }z't||jkr0|d|jt|   }n||	 }|d|jt|   }W n t
yN   tdw t|dt| }||| _| j| t|dt| }||| _d S )Nr       zHash type incompatible to HMAC   6   \)r   digest_size
_digestmod
isinstance
memoryviewr   len
block_sizer	   digestAttributeError
ValueErrorr   _innerupdate_outer)selfkeymsg	digestmodZkey_0Zhash_kZ
key_0_ipadZ
key_0_opad r   ?/usr/local/lib/python3.10/dist-packages/Cryptodome/Hash/HMAC.py__init__6   s,   
zHMAC.__init__c                 C   s   | j | | S )zAuthenticate the next chunk of message.

        Args:
            data (byte string/byte array/memoryview): The next chunk of data
        )r   r   )r   r   r   r   r    r   _   s   zHMAC.updatec                 C   s   | j | j| j||}|S )z2Carry out the expensive inner loop for PBKDF2-HMAC)r   _pbkdf2_hmac_assistr   r   )r   Zfirst_digestZ
iterationsresultr   r   r    r"   i   s   zHMAC._pbkdf2_hmac_assistc                 C   s*   t d| jd}| j |_| j |_|S )a+  Return a copy ("clone") of the HMAC object.

        The copy will have the same internal state as the original HMAC
        object.
        This can be used to efficiently compute the MAC tag of byte
        strings that share a common initial substring.

        :return: An :class:`HMAC`
        s   fake key)r   )r
   r   r   copyr   )r   Znew_hmacr   r   r    r$   s   s   z	HMAC.copyc                 C   s"   | j  }|| j  | S )zReturn the **binary** (non-printable) MAC tag of the message
        authenticated so far.

        :return: The MAC tag digest, computed over the data processed so far.
                 Binary form.
        :rtype: byte string
        )r   r$   r   r   r   )r   Zfrozen_outer_hashr   r   r    r      s   
	zHMAC.digestc                 C   sH   t d}tjd||d}tjd||  d}| | kr"tddS )ah  Verify that a given **binary** MAC (computed by another party)
        is valid.

        Args:
          mac_tag (byte string/byte string/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.
              )Zdigest_bitsr   datazMAC check failedN)r   r   r	   r   r   )r   Zmac_tagsecretZmac1Zmac2r   r   r    verify   s   zHMAC.verifyc                 C   s   d dd t|  D S )zReturn the **printable** MAC tag of the message authenticated so far.

        :return: The MAC tag, 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"HMAC.hexdigest.<locals>.<listcomp>)jointupler   )r   r   r   r    	hexdigest   s   

zHMAC.hexdigestc                 C   s   |  tt| dS )a~  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)r)   r   r   )r   Zhex_mac_tagr   r   r    	hexverify   s   zHMAC.hexverifyr   N)__name__
__module____qualname____doc__r!   r   r"   r$   r   r)   r0   r1   r   r   r   r    r
   .   s    
)

r   Nc                 C   s   t | ||S )aA  Create a new MAC object.

    Args:
        key (bytes/bytearray/memoryview):
            key for the MAC object.
            It must be long enough to match the expected security level of the
            MAC.
        msg (bytes/bytearray/memoryview):
            Optional. The very first chunk of the message to authenticate.
            It is equivalent to an early call to :meth:`HMAC.update`.
        digestmod (module):
            The hash to use to implement the HMAC.
            Default is :mod:`Cryptodome.Hash.MD5`.

    Returns:
        An :class:`HMAC` object
    )r
   )r   r   r   r   r   r    r	      s   r2   )ZCryptodome.Util.py3compatr   r   binasciir   ZCryptodome.Hashr   r   ZCryptodome.Util.strxorr   ZCryptodome.Randomr   __all__objectr
   r	   r   r   r   r    <module>   s   ! 