Security and Identifier Modules

hmac

Creates keyed message authentication codes using a shared secret and a fixed-output hash algorithm. A receiver can recompute the tag and compare it with compare_digest; this provides integrity and authentication when the key is protected, not confidentiality or replay protection. Example keys are public teaching fixtures only.

Formal Syntax & Structure

python syntaxpython
import hmac

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import hmackey = b"demo-only-key"message = b"total=5"tag = hmac.digest(key, message, "sha256")print(hmac.compare_digest(tag, hmac.digest(key, message, "sha256")))
Python Reference