# pydoc > hmac

---
type: CommandReference
command: hmac
mode: pydoc
section: 
source: pydoc3
---

## Quick Reference

- `import hmac; h = hmac.new(key, msg=None, digestmod='')` — create HMAC object
- `h.update(msg)` — feed data
- `h.digest()` — return hash as bytes
- `h.hexdigest()` — return hash as hex string
- `h.copy()` — return copy of object
- `hmac.compare_digest(a, b)` — constant-time comparison
- `hmac.digest(key, msg, digest)` — fast inline HMAC
- `hmac.new(key, msg, digestmod)` — alias for HMAC constructor

## Name

hmac — HMAC (Keyed-Hashing for Message Authentication) module. Implements the HMAC algorithm as described by RFC 2104.

## Synopsis

python
import hmac
# Create HMAC object:
h = hmac.new(key, msg=None, digestmod='')

# Or use fast inline:
hmac.digest(key, msg, digest)
## Options

### `hmac.new(key, msg=None, digestmod='')`
- `key` — bytes or buffer, key for the keyed hash object.
- `msg` — bytes or buffer, initial input for the hash or None.
- `digestmod` — hash name suitable for `hashlib.new()`, or a hashlib constructor, or a PEP 247 module. Required as of Python 3.8.

### `hmac.digest(key, msg, digest)`
- `key` — bytes or buffer, key.
- `msg` — bytes or buffer, input message.
- `digest` — hash name, constructor, or module.

### `hmac.compare_digest(a, b)`
- `a` — str (ASCII only) or bytes-like object.
- `b` — same type as `a`.

### `hmac.HMAC` class methods
- `__init__(self, key, msg=None, digestmod='')` — same as `new`.
- `update(self, msg)` — feed data into this hashing object.
- `digest(self)` — return hash value as bytes.
- `hexdigest(self)` — return hash as hexadecimal string.
- `copy(self)` — return a separate copy of the hashing object.

### Properties (readonly)
- `name` — hash name
- `block_size` — block size
- `digest_size` — digest size
- `blocksize` (attribute) = 64

## Examples

python
import hmac
key = b'secret'
msg = b'message'
h = hmac.new(key, msg, 'sha256')
print(h.hexdigest())
# Compare digests
a = h.digest()
b = h.digest()
print(hmac.compare_digest(a, b))  # True
## See Also

- [RFC 2104](https://tools.ietf.org/html/rfc2104)
- [Python hashlib module](https://docs.python.org/3/library/hashlib.html)
- [PEP 247](https://www.python.org/dev/peps/pep-0247/)