Python hashlib: SHA-256, File Hashes, HMAC and Password Hashing
Python hashlib turns any data into a fixed-size fingerprint: SHA-256 checksums for downloads, change detection for files, signatures with HMAC, and slow, salted hashes for passwords. We hashed strings and a 50 MB file, timed six algorithms, measured why plain SHA-256 is dangerous for passwords, and verified PBKDF2 and HMAC the safe way.
- Python hashlib computes digests such as SHA-256 from bytes; the same input always gives the same 64-character hex string.
hashlib.file_digesthashed a 50 MB file in 0.04 s, matching our manual chunked loop.- One plain SHA-256 took 0.83 microseconds, which lets attackers test passwords very fast; 600,000 PBKDF2 iterations took 0.33 seconds.
- On our machine SHA-256 (1,455 MB/s) was faster than MD5 (794 MB/s), so there is no speed excuse for MD5.
A hash function maps data of any size to a short, fixed-size value, and even a one-character change produces a completely different result. That makes hashes ideal for checking that a download is intact, detecting changed files, building cache keys and proving a message was not tampered with. Python hashlib provides all the standard algorithms, and, used correctly, the tools for storing passwords too.
Every example ran on Python 3.13.6 on a Windows laptop; throughput depends heavily on the CPU, so treat our speeds as relative.
What a cryptographic hash is
A cryptographic hash is a one-way function: easy to compute from the input, practically impossible to reverse, and with no known way to find two inputs giving the same output (a collision) for modern algorithms such as SHA-256. It is not encryption, because there is no key and no way back. Python’s hashlib.algorithms_guaranteed listed 14 algorithms available on every platform.
Hashing a string
import hashlib
hashlib.sha256(b"hello").hexdigest()
# '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
hashlib.sha256(b"hellp").hexdigest()[:16]
# 'fdd7585e08c4e2af' one letter changed, completely different digest
Call the algorithm’s constructor with bytes, then hexdigest() for a readable string or digest() for raw bytes. Changing one letter changed every part of the result, the “avalanche effect” that makes hashes good at detecting any modification.
Hash bytes, not strings
hashlib.sha256("hello")
# TypeError: Strings must be encoded before hashing
hashlib.sha256("café".encode()).hexdigest()[:16] # '850f7dc43910ff89' UTF-8
hashlib.sha256("café".encode("latin-1")).hexdigest()[:16] # 'dafd66c0b98965e6' Latin-1
Hashes work on bytes, so text must be encoded first, and the encoding changes the result. The same word encoded as UTF-8 and as Latin-1 gave different digests. Always encode explicitly as UTF-8, and normalise text first (case, whitespace, Unicode form) if two spellings should hash the same.
Incremental hashing with update()
h = hashlib.sha256()
h.update(b"hel")
h.update(b"lo")
h.hexdigest() == hashlib.sha256(b"hello").hexdigest() # True
Feeding data in pieces gives exactly the same digest as hashing it all at once. That is how you hash large files or network streams without loading everything into memory.
Algorithms and digest sizes
| Algorithm | Digest bits | “hello” (first 20 hex digits) | Status |
|---|---|---|---|
| md5 | 128 | 5d41402abc4b2a76b971 | Broken for security |
| sha1 | 160 | aaf4c61ddcc5e8a2dabe | Broken for security |
| sha256 | 256 | 2cf24dba5fb0a30e26e8 | Recommended |
| sha512 | 512 | 9b71d224bd62f3785d96 | Recommended |
| sha3_256 | 256 | 3338be694f50c5f33881 | Recommended |
| blake2b | 512 (configurable) | e4cfa39a3d37be31c596 | Recommended |
hashlib.new("sha3_256", data) accepts the algorithm name as a string, handy when it comes from configuration. BLAKE2 lets you choose a shorter digest: hashlib.blake2b(b"hello", digest_size=16) gave a 32-hex-digit result, useful for compact cache keys.
Hashing files
# Manual, works on any Python version
h = hashlib.sha256()
with open("big.bin", "rb") as f:
for chunk in iter(lambda: f.read(1 << 20), b""): # 1 MB at a time
h.update(chunk)
# Python 3.11+
with open("big.bin", "rb") as f:
digest = hashlib.file_digest(f, "sha256").hexdigest()
# 50 MB file: chunked 0.06 s, file_digest 0.04 s, same digest: True
Open files in binary mode and read in chunks, so memory use stays small no matter how large the file is. hashlib.file_digest does this for you and was slightly faster. Comparing the result with a published checksum verifies a download; storing digests lets you detect changed or duplicate files, combined with the directory tools in our pathlib guide and file handling guide.
How fast is each algorithm?
| Algorithm | Throughput on 50 MB |
|---|---|
| sha1 | 1,829 MB/s |
| sha256 | 1,455 MB/s |
| md5 | 794 MB/s |
| sha512 | 705 MB/s |
| blake2b | 625 MB/s |
| sha3_256 | 435 MB/s |
The surprise: SHA-256 was nearly twice as fast as MD5. Many modern processors have dedicated SHA instructions, which OpenSSL (behind hashlib) uses, while MD5 runs in plain software. Results differ between CPUs, but the old argument that MD5 is “faster for checksums” often no longer holds. Any of these is far faster than reading data from disk or network.
Why not plain SHA-256 for passwords?
plain sha256 of a password: 0.83 microseconds per guess
Speed is exactly what you do not want for passwords. If a database of SHA-256 password hashes leaks, an attacker can try guesses at the same speed: at 0.83 microseconds each, one CPU core tests over a million passwords per second, and graphics cards are thousands of times faster. Without a salt, identical passwords also share identical hashes, so one cracked hash cracks every account using it. Password storage needs a deliberately slow, salted function.
PBKDF2 and scrypt in hashlib
salt = os.urandom(16)
dk = hashlib.pbkdf2_hmac("sha256", password, salt, 600_000) # 32 bytes
# 100,000 iterations: 0.047 s 600,000 iterations: 0.331 s
hashlib.scrypt(password, salt=salt, n=2**14, r=8, p=1)
# 0.053 s, and memory-hard
# same password, different salts give different hashes: True
pbkdf2_hmac repeats HMAC-SHA256 many times. At 600,000 iterations, which OWASP recommends for PBKDF2-SHA256, one check took 0.33 seconds, about 400,000 times slower than a plain SHA-256: barely noticeable at login, devastating for an attacker. scrypt also uses a configurable amount of memory, which makes graphics-card attacks expensive. Store the algorithm, iteration count, salt and hash together so you can raise the cost later. Dedicated libraries such as argon2-cffi or bcrypt handle this format for you and are usually the best choice; we did not run them here.
Verifying a password safely
import hmac
def verify(password, salt, stored, iterations=600_000):
candidate = hashlib.pbkdf2_hmac("sha256", password, salt, iterations)
return hmac.compare_digest(candidate, stored)
verify(b"correct horse battery staple", salt, stored) # True
verify(b"wrong", salt, stored) # False
Recompute the hash with the stored salt and compare with hmac.compare_digest, which takes the same time whether the first byte or the last byte differs. A plain == can stop at the first difference, leaking timing information. Rate-limit login attempts as well; see our API rate limiting guide.
Tip: never invent your own scheme such as “SHA-256 of password plus a secret”. Use PBKDF2, scrypt, bcrypt or Argon2 with a per-user random salt.
HMAC: proving a message was not changed
key = secrets.token_bytes(32)
msg = b'{"order":42,"amount":99}'
sig = hmac.new(key, msg, "sha256").hexdigest() # 64 hex characters
hmac.compare_digest(sig, hmac.new(key, msg, "sha256").hexdigest()) # True
hmac.compare_digest(sig, hmac.new(key, msg.replace(b"99", b"1"), "sha256").hexdigest()) # False
A plain hash proves nothing about who created it, because anyone can hash modified data. HMAC mixes in a secret key, so only someone with the key can produce a valid signature. Webhook providers sign payloads this way, and signed cookies use the same idea, as explained in our HTTP cookies guide. Do not build a signature as sha256(key + message): it is vulnerable to length-extension attacks, which HMAC’s construction prevents. Keep the key out of source code, as in our environment variables guide.
MD5 and SHA-1 today
hashlib.md5(b"x", usedforsecurity=False).hexdigest()
Researchers can create MD5 and SHA-1 collisions, so they must not be used for signatures, certificates or anything an attacker could exploit. They are still acceptable as non-security checksums, for example deduplicating your own files or matching legacy systems. Passing usedforsecurity=False documents that intent and keeps the call working on systems in FIPS mode, where security uses of MD5 are blocked.
Common hashlib mistakes
- Hashing a str without encoding, which raises a TypeError, or encoding with a different codec on each side, which silently produces different digests.
- Reading huge files into memory with
f.read()instead of chunks orfile_digest. - Comparing secrets with == instead of
hmac.compare_digest. - Using a global salt, or none, for passwords; every user needs a fresh random salt stored with the hash.
- Treating a hash as encryption: you cannot get the data back, and short predictable inputs such as phone numbers can be recovered by trying every possibility.
Which hash should you use?
| Purpose | Use |
|---|---|
| File checksums, integrity | SHA-256 (or BLAKE2) |
| Cache keys, deduplication | SHA-256 or BLAKE2 with a short digest |
| Message authentication | HMAC-SHA256 |
| Password storage | Argon2, bcrypt, scrypt or PBKDF2 with 600,000+ iterations |
| Random tokens and IDs | secrets module, or uuid4, not a hash |
Final thoughts
Python hashlib makes hashing a one-liner, and the important decisions are about purpose rather than syntax. Encode text explicitly, hash files in chunks or with file_digest, use SHA-256 or BLAKE2 for integrity, HMAC for authenticity, and slow salted functions for passwords, verified with compare_digest. In our tests, the difference between a fast hash and a password hash was a factor of about 400,000, which is exactly the protection a leaked database needs.
Frequently asked questions
How do I hash a string with SHA-256 in Python?
Encode the string to bytes, usually as UTF-8, then call hashlib.sha256 with those bytes and hexdigest for a 64-character hexadecimal result.
How do I get the SHA-256 hash of a file in Python?
Open the file in binary mode and pass it to hashlib.file_digest with the name sha256 on Python 3.11 or newer, or read it in chunks and call update on a sha256 object.
Is SHA-256 safe for storing passwords?
Not on its own. It is too fast and identical passwords give identical hashes. Use a salted, slow function such as PBKDF2 with hundreds of thousands of iterations, scrypt, bcrypt or Argon2.
What is the difference between hashlib and hmac?
hashlib computes plain hashes that anyone can recreate. hmac combines a hash with a secret key, so only key holders can produce or verify the signature.
Is MD5 still usable?
Only for non-security purposes such as checksums against accidental corruption. It has practical collision attacks, so never use it for signatures, certificates or passwords.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.