hashlib

The Python hashlib module provides a common interface to many secure hash and message digest algorithms, such as SHA-256 and MD5.

These algorithms allow you to generate fixed-size hash values from arbitrary input data, which is useful for data integrity checks, password storage, and more.

Here’s a quick example:

Python
>>> import hashlib
>>> hashlib.sha256(b"Real Python").hexdigest()
'4a2b42c72ead91c16165d81622a347d5d65addb3a6984927b8322af26827baf3'

Key Features

  • Provides a common API for many hash algorithms
  • Supports secure hash functions like SHA-256, SHA-512, and others
  • Includes the MD5 algorithm for legacy purposes
  • Allows creating hash objects for iterative data feeding

Frequently Used Classes and Functions

Object Type Description
hashlib.new() Function Creates a hash object for a given algorithm name
hashlib.sha256() Function Creates a SHA-256 hash object
hashlib.md5() Function Creates an MD5 hash object
hashlib.algorithms_guaranteed Constant Set of algorithms guaranteed to be supported

Examples

Using md5 for a quick checksum (not secure for cryptographic purposes):

Python
>>> hash_object = hashlib.md5(b"Real Python")
>>> hash_object.hexdigest()
'5245ae598714e551418aa6d5cc2cf5bc'

Iteratively updating a hash object:

Python
>>> hash_object = hashlib.sha256()
>>> hash_object.update(b"Real ")
>>> hash_object.update(b"Python")
>>> hash_object.hexdigest()
'4a2b42c72ead91c16165d81622a347d5d65addb3a6984927b8322af26827baf3'

Common Use Cases

  • Verifying data integrity with checksums
  • Storing passwords securely using dedicated password hashing algorithms
  • Creating unique identifiers for data

Real-World Example

Suppose you want to verify the integrity of a downloaded file by comparing its SHA-256 hash to a known value. Here’s how you could accomplish this using hashlib:

Python
>>> import hashlib

>>> def calculate_file_hash(file_path):
...     hash_obj = hashlib.sha256()
...     with open(file_path, "rb") as file:
...         while chunk := file.read(8192):
...             hash_obj.update(chunk)
...     return hash_obj.hexdigest()

>>> known_hash = "expected_sha256_hash_value"
>>> file_hash = calculate_file_hash("downloaded_file.txt")
>>> file_hash == known_hash
True

In this example, you use hashlib to compute the SHA-256 hash of a file in chunks, ensuring efficient memory usage for large files. This allows you to verify the file’s integrity by comparing the computed hash to a known value.