Package Crypto :: Package Hash :: Module HMAC :: Class HMAC
[frames] | no frames]

Class HMAC

Class that implements HMAC
Instance Methods
 
__init__(self, key, msg=None, digestmod=None)
Create a new HMAC object.
 
update(self, msg)
Continue authentication of a message by consuming the next chunk of data.
 
copy(self)
Return a copy ("clone") of the MAC object.
 
digest(self)
Return the binary (non-printable) MAC of the message that has been authenticated so far.
 
hexdigest(self)
Return the printable MAC of the message that has been authenticated so far.
Class Variables
  digest_size = None
The size of the authentication tag produced by the MAC. It matches the digest size on the underlying hashing module used.
Method Details

__init__(self, key, msg=None, digestmod=None)
(Constructor)

 
Create 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.
  • digestmod (A hash module or object instantiated from Crypto.Hash) - The hash algorithm the HMAC is based on. Default is Crypto.Hash.MD5.

update(self, msg)

 

Continue 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

copy(self)

 

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

digest(self)

 

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.

Returns:
A byte string of digest_size bytes. It may contain non-ASCII characters, including null bytes.

hexdigest(self)

 

Return the printable MAC of the message that has been authenticated so far.

This method does not change the state of the MAC object.

Returns:
A string of 2* digest_size bytes. It contains only hexadecimal ASCII digits.