On this page
hashlib
Prev: gzip | Up: stdlib_module | Next: heapq
Rel: hmac, secrets
Ref: docs.python.org — hashlib
Tags: #public #python3 #stdlib
Import
import hashlib
Overview
Cryptographic digest algorithms (sha256, sha512, blake2b, sha3_256) and password key derivation (scrypt, pbkdf2_hmac). Use hashlib.file_digest() (Python 3.11+) for streaming file checksums, hmac.compare_digest() for constant-time comparisons, and secrets for generating random tokens or salts.
Quick Start
import hashlib
digest = hashlib.sha256(b"hello world").hexdigest()
assert digest == "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
Implementation Walkthrough
1. Streaming File Checksums with file_digest() (Python 3.11+)
Avoid loading multi-gigabyte files into RAM; file_digest() reads in buffered chunks directly in C:
import hashlib
from pathlib import Path
def sha256_file(path: Path) -> str:
with path.open("rb") as fh:
return hashlib.file_digest(fh, "sha256").hexdigest()
2. Password Hashing with scrypt + Random Salt
Never hash passwords with plain sha256—use a slow, salted key derivation function (scrypt or pbkdf2_hmac) and constant-time verification:
import hashlib
import hmac
import secrets
salt = secrets.token_bytes(16)
key = hashlib.scrypt(b"correct-horse-battery", salt=salt, n=2**14, r=8, p=1)
# Verify candidate password:
candidate = hashlib.scrypt(b"correct-horse-battery", salt=salt, n=2**14, r=8, p=1)
assert hmac.compare_digest(key, candidate)
Gotchas & Boundaries
- Input must be
bytes, notstr: Encode text explicitly (text.encode("utf-8")) before hashing. - Do not use
md5orsha1for security or integrity: Both have practical collision attacks; default tosha256orblake2b. - Never hash passwords with plain
sha256(): Fast hashes are trivially brute-forced; usehashlib.scrypt()with a per-user random salt. - Compare secret digests with
hmac.compare_digest(): Plain==leaks timing information.
Common Functions
- hashlib.file_digest() —
file_digest(fileobj, digest, /, *, _bufsize=262144)— Hash the contents of a file-like object. Returns a digest object. - hashlib.md5() —
md5(data=b'', *, usedforsecurity=True, string=None)— Returns a md5 hash object; optionally initialized with a string - hashlib.new() —
new(name, *args, **kwargs)— optionally initialized with data (which must be a bytes-like object). - hashlib.pbkdf2_hmac() —
pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None)— Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as pseudorandom function. - hashlib.sha1() —
sha1(data=b'', *, usedforsecurity=True, string=None)— Returns a sha1 hash object; optionally initialized with a string - hashlib.sha224() —
sha224(data=b'', *, usedforsecurity=True, string=None)— Returns a sha224 hash object; optionally initialized with a string - hashlib.sha256() —
sha256(data=b'', *, usedforsecurity=True, string=None)— Returns a sha256 hash object; optionally initialized with a string - hashlib.sha384() —
sha384(data=b'', *, usedforsecurity=True, string=None)— Returns a sha384 hash object; optionally initialized with a string - hashlib.sha3_224() —
sha3_224(data=b'', *, usedforsecurity=True, string=None)— Returns a sha3-224 hash object; optionally initialized with a string - hashlib.sha3_256() —
sha3_256(data=b'', *, usedforsecurity=True, string=None)— Returns a sha3-256 hash object; optionally initialized with a string - hashlib.sha3_384() —
sha3_384(data=b'', *, usedforsecurity=True, string=None)— Returns a sha3-384 hash object; optionally initialized with a string - hashlib.sha3_512() —
sha3_512(data=b'', *, usedforsecurity=True, string=None)— Returns a sha3-512 hash object; optionally initialized with a string - hashlib.sha512() —
sha512(data=b'', *, usedforsecurity=True, string=None)— Returns a sha512 hash object; optionally initialized with a string - hashlib.shake_128() —
shake_128(data=b'', *, usedforsecurity=True, string=None)— Returns a shake-128 variable hash object; optionally initialized with a string - hashlib.shake_256() —
shake_256(data=b'', *, usedforsecurity=True, string=None)— Returns a shake-256 variable hash object; optionally initialized with a string
Classes & Objects
- hashlib.blake2b — Return a new BLAKE2b hash object. (Methods:
.copy(),.digest(),.hexdigest(),.update()) - hashlib.blake2s — Return a new BLAKE2s hash object. (Methods:
.copy(),.digest(),.hexdigest(),.update())
Constants & Attributes
hashlib.algorithms_available(set) ={'sm3', 'md5', 'shake_128', 'sha3_256', 'blake2b', 'md5-s...hashlib.algorithms_guaranteed(set) ={'sha224', 'shake_128', 'blake2s', 'shake_256', 'sha3_256...
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/gzip
- tuts/python3/stdlib_module/hashlib/blake2b
- tuts/python3/stdlib_module/hashlib/blake2s
- tuts/python3/stdlib_module/hashlib/file_digest
- tuts/python3/stdlib_module/hashlib/md5
- tuts/python3/stdlib_module/hashlib/new
- tuts/python3/stdlib_module/hashlib/pbkdf2_hmac
- tuts/python3/stdlib_module/hashlib/sha1
- tuts/python3/stdlib_module/hashlib/sha224
- tuts/python3/stdlib_module/hashlib/sha256
- tuts/python3/stdlib_module/hashlib/sha384
- tuts/python3/stdlib_module/hashlib/sha3_224
- tuts/python3/stdlib_module/hashlib/sha3_256
- tuts/python3/stdlib_module/hashlib/sha3_384
- tuts/python3/stdlib_module/hashlib/sha3_512
- tuts/python3/stdlib_module/hashlib/sha512
- tuts/python3/stdlib_module/hashlib/shake_128
- tuts/python3/stdlib_module/hashlib/shake_256
- tuts/python3/stdlib_module/heapq
- tuts/python3/stdlib_module/hmac
- tuts/python3/stdlib_module/secrets
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/zlib
Related notes
- tuts/python3/stdlib_module/gzip Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib/blake2b Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib/blake2s Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib/file_digest Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib/md5 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib/new Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib/pbkdf2_hmac Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib/sha1 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib/sha224 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib/sha256 Linked here; links to this page; shared tags: #stdlib