1. Home
  2. Blog
  3. Software Development

Software Development

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.

9 min read
Python hashlib: SHA-256, file hashes, HMAC and password hashing

Key takeaways

  • Python hashlib computes digests such as SHA-256 from bytes; the same input always gives the same 64-character hex string.
  • hashlib.file_digest hashed 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 or file_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

Work with Stack Quora

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.

Keep reading

More guides on testing, AI & modern development

Enjoyed this article? Pick a topic and jump straight into our newest tutorials.

Artificial Intelligence

Confusion Matrix Explained: Precision, Recall, F1 and Thresholds in Python

Confusion matrix explained with Python: TP, FP, FN, TN, precision, recall, F1, the accuracy trap, threshold choice by cost and…

Read article →
Software Development

API Pagination: Offset vs Cursor vs Keyset, Tested on 1 Million Rows

API pagination explained: offset vs keyset and cursor pagination timed on 1 million rows, duplicates when data changes, the ties…

Read article →
DevOps

Git Submodule Explained: Add, Clone, Update and Remove Safely

Git submodule explained: add, clone, update –init, update –remote, the detached HEAD trap, foreach and clean removal, tested step by…

Read article →
Test Automation

Pytest tmp path Explained: tmp_path, tmp_path_factory and Temp Files

Pytest tmp_path explained: per-test temporary folders, tmp_path_factory for shared files, monkeypatch.chdir, legacy tmpdir, retention and basetemp.

Read article →
Software Testing

Test Case Prioritization: 6 Techniques Compared With APFD

Test case prioritization compared: risk, coverage, cost, history and random orders scored with APFD and time to find all faults…

Read article →
Databases & SQL

Full Text Search in SQL: FTS5, Ranking, Phrases and Speed

Full text search in SQL with SQLite FTS5: MATCH queries, phrases, prefixes, NEAR, stemming, BM25 ranking, snippets, two gotchas and…

Read article →
Databases & SQL

Generated Columns in SQL: Virtual vs Stored, Indexes and Limits

Generated columns in SQL explained: virtual vs stored, automatic updates, write errors, storage cost, indexing, JSON fields and support in…

Read article →
Software Development

Object Freeze in JavaScript: Shallow vs Deep, Seal and const

Object freeze in JavaScript explained: freeze vs seal vs const, strict mode errors, shallow vs deep freeze, frozen arrays, Map…

Read article →
Software Development

Python hashlib: SHA-256, File Hashes, HMAC and Password Hashing

Python hashlib explained: SHA-256 for strings and files, algorithm speeds, why passwords need PBKDF2 or scrypt, safe verification and HMAC…

Read article →
Software Development

Python UUID Explained: uuid1, uuid4, uuid5, uuid7 and Database Keys

Python UUID explained: uuid4, uuid5, uuid1 privacy, time-ordered uuid7, parsing, storage formats and UUIDs as database keys, tested on Python…

Read article →
Artificial Intelligence

Random Forest in Python: Bagging, Feature Sampling and OOB From Scratch

Random forest in Python from scratch: bootstrap sampling, feature sampling, number of trees, out-of-bag evaluation and impurity vs permutation importance.

Read article →
Software Development

MIME Types Explained: Content-Type, nosniff and a Common Types List

MIME types explained and tested in a browser: Content-Type, nosniff, scripts and stylesheets with wrong types, a common MIME types…

Read article →
DevOps

Git Clean Explained: Remove Untracked Files Without Regret

Git clean explained with dry runs: -n, -f, -d, -x, -X, exclusions, paths, nested repositories and interactive mode, plus a…

Read article →
Test Automation

Pytest approx: Comparing Floats With rel, abs and Collections

Pytest approx explained: default tolerances, the near-zero trap, rel and abs, lists and dicts, NaN, NumPy, and how it differs…

Read article →
Software Testing

Accessibility Testing: WCAG Checklist, Automated Tools and Manual Checks

Accessibility testing explained: WCAG basics, an axe-core scan of a broken and a fixed page, what automation missed, contrast checks…

Read article →
Databases & SQL

SQL UNPIVOT: Turn Columns Into Rows, 4 Ways Tested

SQL UNPIVOT explained: turn columns into rows with UNION ALL, CROSS JOIN with CASE, JSON functions or native UNPIVOT, with…

Read article →
Software Development

Intl NumberFormat in JavaScript: Currency, Compact, Percent and Units

Intl.NumberFormat in JavaScript: locales, currency, compact notation, percent, units, rounding, formatToParts, the non-breaking space trap and formatter reuse.

Read article →
Databases & SQL

SQL EXPLAIN Plan: How to Read Query Plans and Fix Slow Queries

SQL EXPLAIN plan explained: read scans, index searches, covering indexes and temporary sorts, with before and after timings for five…

Read article →
Artificial Intelligence

Decision Tree in Python: Gini, Entropy and Overfitting From Scratch

Decision tree in Python from scratch: Gini impurity, entropy, information gain, best splits, readable rules, and how depth and leaf…

Read article →
DevOps

Git Blame Explained: Find Who Changed a Line, and Why

Git blame explained with a real repository: ignore whitespace, line ranges and functions, ignore-revs files for formatting commits, moved code…

Read article →
Test Automation

Pytest Benchmark: Measure Speed and Catch Performance Regressions

Pytest benchmark tutorial: the benchmark fixture, reading results, parametrized and pedantic benchmarks, saving baselines and failing the build on regressions.

Read article →
Software Testing

Defect Density: Formula, Worked Examples and How to Use It

Defect density explained: the formula, a five-module worked example, per KLOC vs per function point, severity weighting, defect removal efficiency…

Read article →
Databases & SQL

Case Insensitive Search in SQL: LOWER, LIKE, COLLATE and Indexes

Case insensitive search in SQL: LOWER, LIKE, COLLATE NOCASE and expression indexes compared with query plans, plus accented letters and…

Read article →
Databases & SQL

ON DELETE CASCADE in SQL: CASCADE vs SET NULL vs RESTRICT

ON DELETE CASCADE in SQL tested: CASCADE vs SET NULL vs RESTRICT, multi-level cascades, the SQLite foreign key pragma, row…

Read article →
Artificial Intelligence

Logistic Regression in Python: Sigmoid, Log Loss and Gradient Descent From Scratch

Logistic regression in Python from scratch: sigmoid, log loss, gradient descent, odds ratios, thresholds, sigmoid overflow and separable data, tested…

Read article →
DevOps

Git Worktree Explained: Work on Two Branches at Once

Git worktree explained: add, list, remove, prune, lock and detached worktrees, a hotfix workflow, and the errors you will hit,…

Read article →
Test Automation

Pytest ini Explained: addopts, markers, testpaths and Precedence

Pytest ini explained: testpaths, addopts, strict markers, xfail_strict, filterwarnings, minversion, config file precedence and command-line overrides, tested.

Read article →
Software Testing

Test Estimation Techniques: WBS, Three Point PERT and Monte Carlo

Test estimation techniques worked through: WBS, three point PERT, total uncertainty, a Monte Carlo check, percentage rules and Wideband Delphi.

Read article →
Artificial Intelligence

Linear Regression in Python: Least Squares From Scratch, With R Squared

Linear regression in Python from scratch: closed form, NumPy least squares and gradient descent, R squared, RMSE, outliers and curved…

Read article →
DevOps

Git Remote Explained: Add, Set-URL, Rename, Remove and Push URLs

Git remote explained: add, set-url, separate push URLs, rename, remove and prune, plus how to avoid leaking tokens in remote…

Read article →
Test Automation

Pytest Mock: mocker.patch, spy, side_effect and autospec Explained

Pytest mock explained: mocker.patch, return_value, side_effect, spy, autospec and PropertyMock, with tested examples for APIs, clocks and retries.

Read article →
Software Testing

System Testing Explained: Types, Process and a Real End-to-End Example

System testing explained: types, entry and exit criteria, the process, and a real end-to-end test suite run against a small…

Read article →
Artificial Intelligence

Naive Bayes Classifier Explained: Build a Spam Filter in Python

Naive Bayes classifier explained by building a spam filter in Python: Bayes rule, the naive assumption, Laplace smoothing, log probabilities…

Read article →
DevOps

Git Branch Explained: Create, Switch, Rename, Delete and Recover

Git branch explained with tested commands: list, create, switch, rename, delete, merged checks, recovering deleted branches, remote tracking and naming.

Read article →
Test Automation

Pytest raises: Testing Exceptions, match and excinfo

Pytest raises explained with tested examples: match regexes, excinfo, custom and chained exceptions, parametrized error cases, pytest.warns and common mistakes.

Read article →
Software Testing

Non Functional Testing: Types, Metrics and a Real Load Test

Non functional testing explained: 10 key types, measurable requirements, latency percentiles and what a small real load test revealed about…

Read article →