Skip to content
Tutorials Center
My learning

Topics

Python Reference Β· Standard Library: Math, Data and Text

The hashlib module

Reference for Python's hashlib module: compute SHA-256, SHA-3 and BLAKE2 digests, hash files in chunks, derive keys with pbkdf2_hmac and avoid weak hashes.

The hashlib module computes cryptographic hashes (digests): fixed-size fingerprints of data. The same input always gives the same digest, a tiny change gives a completely different one, and you cannot realistically work backward from the digest to the input. Typical uses are verifying file integrity, deduplicating content, building cache keys and deriving keys from passwords.

A hash object is created by name (hashlib.sha256()), fed bytes with update(), and asked for its result with hexdigest() or digest(). Input must be bytes, not str.

Python
import hashlib

print(hashlib.sha256(b"hello").hexdigest())

Try it

Python Digests and incremental hashing

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Files and comparing safely

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Key derivation is not available in the browser runner (its Python build omits pbkdf2_hmac and scrypt), so this one is read-only:

Python
import hashlib, secrets

salt = secrets.token_bytes(16)
key = hashlib.pbkdf2_hmac("sha256", b"correct horse", salt, 600_000)
print(len(key))  # 32

Main members

Name Description
hashlib.sha256(data=b""), sha512, sha224, sha384 The SHA-2 family. SHA-256 is the usual default.
hashlib.sha3_256, sha3_512, ... The SHA-3 family.
hashlib.blake2b, blake2s Fast, flexible hashes with digest_size, key and salt options.
hashlib.sha1, hashlib.md5 Legacy, broken for security. Fine for non-security checksums only.
hashlib.new(name, data=b"") Creates a hash by algorithm name string.
hashlib.algorithms_guaranteed / algorithms_available Sets of names available on every platform / on this build.
hashlib.file_digest(fileobj, digest) Hashes a binary file object in chunks (Python 3.11+).
hashlib.pbkdf2_hmac(name, password, salt, iterations, dklen=None) Derives a key from a password.
hashlib.scrypt(password, *, salt, n, r, p, ...) Memory-hard key derivation.

Hash object

Member Description
.update(data) Feeds more bytes. Calling it twice equals one call with the joined data.
.digest() The result as bytes.
.hexdigest() The result as a lowercase hexadecimal str (twice as long as the byte digest).
.copy() A clone, useful to hash a shared prefix once.
.name, .digest_size, .block_size Algorithm name, output length and internal block size in bytes.

Usage notes

  • Encode strings first. Hashing str raises TypeError. Pick the encoding explicitly (usually UTF-8), because different encodings give different digests.
  • Do not use plain hashes for passwords. SHA-256 is far too fast; attackers can try billions of guesses per second. Use pbkdf2_hmac, scrypt or a dedicated library such as bcrypt or argon2, with a random salt per password (secrets.token_bytes(16)) and a high iteration count (hundreds of thousands for PBKDF2 today).
  • MD5 and SHA-1 are broken for collision resistance. Avoid them for signatures, certificates and security decisions. Some builds in restricted environments disable them.
  • Compare digests with hmac.compare_digest() to avoid timing attacks when checking secrets or MACs.
  • A hash is not encryption. You cannot decrypt it, and it does not hide small input spaces (such as phone numbers) from brute force.
  • For message authentication (proving data came from someone with a key), use the standard hmac module or BLAKE2's key argument, not sha256(key + message).
  • Digests are deterministic across runs. Unlike built-in hash() for strings, they are not randomized.