On this page
secrets.compare_digest
Prev: secrets.choice | Up: secrets | Next: secrets.randbelow
Rel: secrets.choice, secrets.randbelow, secrets.randbits, secrets.SystemRandom
Ref: docs.python.org — secrets.compare_digest
Tags: #public #python3 #stdlib #secrets
Import
import secrets
from secrets import compare_digest
Signature & Overview
secrets.compare_digest(a, b, /)
Return 'a == b'.
Usage
import secrets
assert secrets.compare_digest("abc123", "abc123") is True
assert secrets.compare_digest(b"abc123", b"abc124") is False
Gotchas
- Use for comparing tokens, HMACs, and API keys: Prevents timing side-channel attacks that plain
==can leak. - Both arguments must be the same type: Mixing
strandbytesraisesTypeError, and ASCII-onlystris required (preferbytes).
REPL Inspect
from secrets import compare_digest
help(compare_digest)
Linked from
- tuts/python3/stdlib_module/secrets
- tuts/python3/stdlib_module/secrets/choice
- tuts/python3/stdlib_module/secrets/randbelow
- tuts/python3/stdlib_module/secrets/randbits
- tuts/python3/stdlib_module/secrets/SystemRandom
- tuts/python3/stdlib_module/secrets/token_bytes
- tuts/python3/stdlib_module/secrets/token_hex
- tuts/python3/stdlib_module/secrets/token_urlsafe
Related notes
- tuts/python3/stdlib_module/secrets/choice Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/randbelow Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/randbits Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/SystemRandom Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/secrets/token_bytes links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/token_hex links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/token_urlsafe links to this page; shared tags: #secrets, #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib