3
¬ �e=  ã               @   sl   d dl mZ d dlmZmZmZ d dlmZ ddlm	Z	m
Z
 ddl	mZmZmZ G dd„ deƒZd	d
„ ZdS )é    )Ú	unhexlify)ÚbordÚtobytesÚis_bytes)Úget_random_bytesé   )Ú	cSHAKE128ÚSHA3_256)Ú_bytepadÚ_encode_strÚ_right_encodec               @   sH   e 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 )Ú	KMAC_Hashz[A KMAC hash object.
    Do not instantiate directly.
    Use the :func:`new` function.
    c       	      C   sL   d| | _ || _d | _ttt|ƒƒ|ƒ}|j||dƒ| _|rH| jj|ƒ d S )Nz2.16.840.1.101.3.4.2.s   KMAC)	ZoidÚdigest_sizeÚ_macr
   r   r   Z_newÚ_cshakeÚupdate)	ÚselfÚdataÚkeyÚmac_lenÚcustomZoid_variantZcshakeZrateZpartial_newX© r   úZ/var/www/html/wholesaler-api-python-env/lib/python3.6/site-packages/Crypto/Hash/KMAC128.pyÚ__init__.   s    
zKMAC_Hash.__init__c             C   s   | j rtdƒ‚| jj|ƒ | S )z©Authenticate the next chunk of message.

        Args:
            data (bytes/bytearray/memoryview): The next chunk of the message to
            authenticate.
        z8You can only call 'digest' or 'hexdigest' on this object)r   Ú	TypeErrorr   r   )r   r   r   r   r   r   =   s    zKMAC_Hash.updatec             C   s2   | j s,| jjt| jd ƒƒ | jj| jƒ| _ | j S )zŽReturn the **binary** (non-printable) MAC tag of the message.

        :return: The MAC tag. Binary form.
        :rtype: byte string
        é   )r   r   r   r   r   Úread)r   r   r   r   ÚdigestK   s    zKMAC_Hash.digestc             C   s   dj dd„ t| jƒ ƒD ƒƒS )z„Return the **printable** MAC tag of the message.

        :return: The MAC tag. Hexadecimal encoded.
        :rtype: string
        Ú c             S   s   g | ]}d t |ƒ ‘qS )z%02x)r   )Ú.0Úxr   r   r   ú
<listcomp>_   s    z'KMAC_Hash.hexdigest.<locals>.<listcomp>)ÚjoinÚtupler   )r   r   r   r   Ú	hexdigestX   s    zKMAC_Hash.hexdigestc             C   sD   t dƒ}tj|| ƒ}tj|| jƒ  ƒ}|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.
        é   zMAC check failedN)r   r	   Únewr   Ú
ValueError)r   Zmac_tagZsecretZmac1Zmac2r   r   r   Úverifya   s
    zKMAC_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)r(   r   r   )r   Zhex_mac_tagr   r   r   Ú	hexverifyu   s    zKMAC_Hash.hexverifyc             K   s   d|kr| j |d< tf |ŽS )zNReturn a new instance of a KMAC hash object.
        See :func:`new`.
        r   )r   r&   )r   Úkwargsr   r   r   r&   ƒ   s    
zKMAC_Hash.newN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r$   r(   r)   r&   r   r   r   r   r   (   s   	r   c              K   sŒ   | j ddƒ}t|ƒstdƒ‚t|ƒdk r0tdƒ‚| j ddƒ}| j ddƒ}|d	k rXtd
ƒ‚| j ddƒ}| rxtdt| ƒ ƒ‚t||||dtdƒS )aµ  Create a new KMAC128 object.

    Args:
        key (bytes/bytearray/memoryview):
            The key to use to compute the MAC.
            It must be at least 128 bits long (16 bytes).
        data (bytes/bytearray/memoryview):
            Optional. The very first chunk of the message to authenticate.
            It is equivalent to an early call to :meth:`KMAC_Hash.update`.
        mac_len (integer):
            Optional. The size of the authentication tag, in bytes.
            Default is 64. Minimum is 8.
        custom (bytes/bytearray/memoryview):
            Optional. A customization byte string (``S`` in SP 800-185).

    Returns:
        A :class:`KMAC_Hash` hash object
    r   NzYou must pass a key to KMAC128r%   z1The key must be at least 128 bits long (16 bytes)r   r   é@   r   z!'mac_len' must be 8 bytes or morer   ó    zUnknown parameters: Z19é¨   )Úpopr   r   Úlenr'   Ústrr   r   )r*   r   r   r   r   r   r   r   r&   Ž   s    r&   N)Úbinasciir   ZCrypto.Util.py3compatr   r   r   ZCrypto.Randomr   r   r   r	   r
   r   r   Úobjectr   r&   r   r   r   r   Ú<module>   s   f