HMAC2 Documentation
Overview
The HMAC class implements an HMAC (Hash-based Message Authentication Code) using a specified hash function. It provides a simple interface to generate HMAC values using a key and a hash algorithm like SHA256, MD5, etc.
Usage
Creating an HMAC Instance
from hmac2 import HMAC
key = b'secret_key'
message = b'my message'
h = HMAC(key, message, digestmod='sha256')
Updating with More Data
If you need to add more data to the existing HMAC computation:
h.update(b' additional message')
Getting the Final HMAC Value
print(h.hexdigest())
print(h.digest())
Methods
-
__init__(self, key, msg=None, digestmod='sha256')
Initializes an HMAC instance with a given key, an optional message, and a digest algorithm. The default digest algorithm is 'sha256'.
-
update(self, msg)
Updates the HMAC object with more message bytes. You can call this method multiple times with different parts of the message.
-
digest(self)
Returns the raw HMAC value as a byte sequence.
-
hexdigest(self)
Returns the HMAC value as a hexadecimal string.
Alternative Usage with new Function
You can also use the new function for a more concise syntax:
import hmac2
key = b'secret_key'
message = b'my message'
hmac_value = hmac2.new(key, message, 'sha256').hexdigest()
print("HMAC: ", hmac_value)