U +Mbp@srddlmZmZddlmZddlmZddlmZddlm Z ddl m Z ddgZ Gd dde Zd d dZd S))bordtobytes) unhexlify)MD5)BLAKE2s)strxor)get_random_bytesnewHMACc@sReZdZdZdddZddZdd Zd d Zd d ZddZ ddZ ddZ dS)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 NcCs|dkr t}|dkrd}|j|_||_t|tr8|}zNt||jkr`|d|jt|}n$|| }|d|jt|}Wnt k rt dYnXt |dt|}|||_ |j |t |dt|}|||_dS)Nr zHash type incompatible to HMAC6\)r digest_size _digestmod isinstance memoryviewrlen block_sizer digestAttributeError ValueErrorr_innerupdate_outer)selfkeymsg digestmodZkey_0Zhash_kZ key_0_ipadZ key_0_opadr7/tmp/pip-target-t616c12r/lib/python/Crypto/Hash/HMAC.py__init__6s(   z HMAC.__init__cCs|j||S)zAuthenticate the next chunk of message. Args: data (byte string/byte array/memoryview): The next chunk of data )rr)rrrrr r_s z HMAC.updatecCs|j|j|j||}|S)z2Carry out the expensive inner loop for PBKDF2-HMAC)r_pbkdf2_hmac_assistrr)rZ first_digest iterationsresultrrr r"iszHMAC._pbkdf2_hmac_assistcCs*td|jd}|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` sfake key)r)r rrcopyr)rZnew_hmacrrr r%ss   z HMAC.copycCs"|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 )rr%rrr)rZfrozen_outer_hashrrr rs z HMAC.digestcCsHtd}tjd||d}tjd||d}||krDtddS)ahVerify 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_bitsrdatazMAC check failedN)rrr rr)rZmac_tagsecretZmac1Zmac2rrr verifys z HMAC.verifycCsdddt|DS)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 cSsg|]}dt|qS)z%02x)r).0xrrr sz"HMAC.hexdigest..)jointupler)rrrr hexdigests  zHMAC.hexdigestcCs|tt|dS)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*rr)rZ hex_mac_tagrrr hexverifys zHMAC.hexverify)r N) __name__ __module__ __qualname____doc__r!rr"r%rr*r1r2rrrr r .s )    r NcCs t|||S)a=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:`Crypto.Hash.MD5`. Returns: An :class:`HMAC` object )r )rrrrrr r s)r N)ZCrypto.Util.py3compatrrbinasciirZ Crypto.HashrrZCrypto.Util.strxorrZ Crypto.Randomr__all__objectr r rrrr "s