all Python 3hmacterm
hmac.HMAC
message authentication
The class of the HMAC object returned by hmac.new(): it accumulates data with update() and gives the signature through digest() or hexdigest().
Syntax
h = hmac.HMAC(key, msg=None, digestmod='sha256'); h.update(data); h.hexdigest()
Calling hmac.HMAC(key, msg, digestmod) does the same as hmac.new(). digest() does not finalize the object: you may keep calling update() afterwards. copy() snapshots the state, which helps with many messages sharing a prefix. The name attribute looks like 'hmac-sha256', and digest_size is the signature length in bytes.
Examples
import hmac
h = hmac.HMAC(b'key', digestmod='sha256')
h.update(b'hello ')
h.update(b'world')
whole = hmac.new(b'key', b'hello world', 'sha256')
print(h.hexdigest() == whole.hexdigest(), h.name, h.digest_size) # → True hmac-sha256 32
See also
Open in the full glossarydocs.python.orgReport an inaccuracy