---
source: https://iinventedlight.com/library/notes/python-hmac.md
title: "Python hmac - keyed hashing and compare_digest"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, hmac - Keyed-hashing for message authentication; python/cpython (GitHub), Lib/hmac.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: hmac"
tags: [python, hmac, hashing, compare-digest, stdlib]
client: Librarian
sources:
  - id: S1
    title: "hmac - Keyed-hashing for message authentication"
    publisher: Python docs
    url: https://docs.python.org/3/library/hmac.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/hmac.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/hmac.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python hmac - keyed hashing and compare_digest
**Answer:** `hmac.new` returns a new hmac object. The module implements the HMAC algorithm as a keyed hash. Use `compare_digest` to return a == b for two digest values.

## Examples

```python
import hmac, hashlib, os

key = os.urandom(32)
sig = hmac.new(key, b"payload", hashlib.sha256).hexdigest()

def ok(given: str) -> bool:
    expected = hmac.new(key, b"payload", hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, given)
```

## Rules

- Pass a `digestmod` such as `"sha256"` when you create the object.
- `digest()` returns bytes; `hexdigest()` returns a hex string.

## See also

- [python-ssl](https://iinventedlight.com/library/notes/python-ssl/)

## Claims

- id: C1
  claim: new returns a new hmac object.
  source: S1
  evidence: "Return a new hmac object"

- id: C2
  claim: compare_digest compares values as a == b.
  source: S1
  evidence: "Return a == b"

- id: C3
  claim: The module implements the HMAC algorithm.
  source: S2
  evidence: "Implements the HMAC algorithm"

## Sources

- [S1] hmac - Keyed-hashing for message authentication. Python docs. https://docs.python.org/3/library/hmac.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/hmac.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/hmac.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, hmac - Keyed-hashing for message authentication; python/cpython (GitHub), Lib/hmac.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
