B c@sFdZdZdddgZddlmZddlTdZGd ddZd d dZdS) aHMAC (Hash-based Message Authentication Code) algorithm HMAC is a MAC defined in RFC2104_ and FIPS-198_ and constructed using a cryptograpic hash algorithm. It is usually named *HMAC-X*, where *X* is the hash algorithm; for instance *HMAC-SHA1* or *HMAC-MD5*. The strength of an HMAC depends on: - the strength of the hash algorithm - the length and entropy of the secret key An example of possible usage is the following: >>> from Crypto.Hash import HMAC >>> >>> secret = b'Swordfish' >>> h = HMAC.new(secret) >>> h.update(b'Hello') >>> print h.hexdigest() .. _RFC2104: http://www.ietf.org/rfc/rfc2104.txt .. _FIPS-198: http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf z$Id$new digest_sizeHMAC)strxor_c)*Nc@s>eZdZdZdZd ddZddZddZd d Zd d Z dS)rzClass that implements HMACNcCs|dkrddlm}|}||_||_||_y |j|_Wn$tk rbt|j |_YnXy |j }Wntk rd}YnXd}d}t||kr|| }|t d|t|}|j t |||j t |||r| |dS)aCreate a new HMAC object. :Parameters: key : byte string secret key for the MAC object. It must be long enough to match the expected security level of the MAC. However, there is no benefit in using keys longer than the `digest_size` of the underlying hash algorithm. msg : byte string The very first chunk of the message to authenticate. It is equivalent to an early call to `update()`. Optional. :Parameter digestmod: The hash algorithm the HMAC is based on. Default is `Crypto.Hash.MD5`. :Type digestmod: A hash module or object instantiated from `Crypto.Hash` N)MD5@6\r)r digestmodrouterinnerrAttributeErrorlendigestZ block_sizeZbchrupdater)selfkeymsgr rZ blocksizeZipadZopadrC/opt/alt/python37/lib64/python3.7/site-packages/Crypto/Hash/HMAC.py__init__Rs.       z HMAC.__init__cCs|j|dS)aContinue authentication of a message by consuming the next chunk of data. Repeated calls are equivalent to a single call with the concatenation of all the arguments. In other words: >>> m.update(a); m.update(b) is equivalent to: >>> m.update(a+b) :Parameters: msg : byte string The next chunk of the message being authenticated N)rr)rrrrrrsz HMAC.updatecCs0ttd}|j|_|j|_|j|_|S)a!Return a copy ("clone") of the MAC object. The copy will have the same internal state as the original MAC object. This can be used to efficiently compute the MAC of strings that share a common initial substring. :Returns: An `HMAC` object r )rbr rcopyr)rotherrrrrs   z HMAC.copycCs"|j}||j|S)a}Return the **binary** (non-printable) MAC of the message that has been authenticated so far. This method does not change the state of the MAC object. You can continue updating the object after calling this function. :Return: A byte string of `digest_size` bytes. It may contain non-ASCII characters, including null bytes. )rrrrr)rhrrrrs z HMAC.digestcCsdddt|DS)aReturn the **printable** MAC of the message that has been authenticated so far. This method does not change the state of the MAC object. :Return: A string of 2* `digest_size` bytes. It contains only hexadecimal ASCII digits. r cSsg|]}dt|qS)z%02x)Zbord).0xrrr sz"HMAC.hexdigest..)jointupler)rrrr hexdigests zHMAC.hexdigest)NN) __name__ __module__ __qualname____doc__rrrrrr#rrrrrJs 1cCs t|||S)aCreate a new HMAC object. :Parameters: key : byte string key for the MAC object. It must be long enough to match the expected security level of the MAC. However, there is no benefit in using keys longer than the `digest_size` of the underlying hash algorithm. msg : byte string The very first chunk of the message to authenticate. It is equivalent to an early call to `HMAC.update()`. Optional. :Parameter digestmod: The hash to use to implement the HMAC. Default is `Crypto.Hash.MD5`. :Type digestmod: A hash module or instantiated object from `Crypto.Hash` :Returns: An `HMAC` object )r)rrr rrrrs)NN) r'Z __revision____all__ZCrypto.Util.strxorrZCrypto.Util.py3compatrrrrrrr9s  v