Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

hashlib

Python’s hashlib module provides a common interface for generating digests with standard hash algorithms. Create a hash object, supply bytes-like data, and read the result as bytes or hexadecimal text.

How it works

Import hashlib, create an algorithm-specific hash object, feed it bytes with update(), and retrieve the accumulated digest as raw bytes or hexadecimal text. The module also exposes helpers for hashing files or file-like objects efficiently.

Syntax

hashlib.<algorithm>(data=b'')
hash.update(data)
hash.digest()
hash.hexdigest()

Parameters

NameTypeDescription
databytes-like objectInitial bytes-like data accepted by a named hash constructor; omitted when no initial data is needed.

Returns

Named constructors return hash objects. digest() returns bytes; hexdigest() returns a hexadecimal string.

Key points

  • hashlib gives different hashing algorithms a consistent Python interface.
  • Select an algorithm through its constructor, such as sha256() or blake2b().
  • Hash objects share operations including update(), digest(), and hexdigest().
  • Core constructors include SHA-1, SHA-2, SHA-3, SHAKE, and BLAKE2 families.
  • Use the file-hashing helper when processing file or file-like input.

Common mistakes

  • Hash input must be bytes-like; encode text explicitly before passing it to a constructor or update().
  • Calling digest() or hexdigest() reports all data supplied so far, so update() calls are cumulative.

Example: Hash bytes with SHA-256

import hashlib

payload = b"hello backend"
digest = hashlib.sha256(payload).hexdigest()
print(digest)

Creates a SHA-256 hash from bytes and prints its hexadecimal digest.

When to use it

Integrity checks

Verify that downloaded or generated data matches an expected checksum.

Content-based identifiers

Create stable digest values for non-secret identifiers or cache keys.

File hashing

Process large files incrementally without reading them all at once.

Interview questions

QHow is a hash object used?

hashlib offers named constructors such as sha256() that return hash objects. Feed bytes-like data with update(), then use digest() for bytes or hexdigest() for hexadecimal text.

Follow-up: How would you hash a large input? Read it incrementally and call update() for each bytes chunk, avoiding loading the entire input into memory.

QWhat is the difference between digest() and hexdigest()?

digest() returns the binary digest, while hexdigest() returns the same result represented as hexadecimal characters. The digest reflects the concatenation of every input supplied to the object so far.

Follow-up: When would binary output be preferable? Use digest() when another API expects raw bytes or when compact binary storage matters.

QWhich algorithms does hashlib provide?

The module guarantees constructors for SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-3 variants, SHAKE variants, BLAKE2b, and BLAKE2s. Additional algorithms can be discovered through algorithms_available.

Follow-up: Why might an algorithm list differ between systems? Some algorithms depend on the linked OpenSSL implementation, while guaranteed constructors remain available.

  • hash object
  • update()
  • digest()
  • hexdigest()
  • algorithms_available
  • file_digest()
  • hmac

Source: hashlib — Secure hashes and message digests. Checked 2026-10-16.