Course contentsShow
← Python
Lesson 729 of 1,06011. Standard Library EssentialsFree lesson

Checking Data Integrity with hashlib

Compute SHA-256 digests from encoded bytes and chunked files with hashlib, distinguishing integrity checks from encryption and secure password storage.

In this lesson, you will learn how to verify that your data has not been corrupted or tampered with by generating SHA-256 digital fingerprints using Python’s hashlib module.

Understanding Data Integrity

Data integrity is about ensuring that a file or message remains unchanged. Unlike encryption, which hides data, a hash is a "one-way" mathematical function that turns input data into a fixed-size string of characters. If even a single bit of the original data changes, the resulting hash will look completely different. This makes it an ideal tool for verifying that a file downloaded from the internet matches the original version.

Computing SHA-256 Digests

To generate a digest, you must first convert your data into bytes. The hashlib.sha256() function creates a hash object, and you update it with your data.

import hashlib

data = b"Hello, world!"
digest = hashlib.sha256(data).hexdigest()
print(f"SHA-256 Hash: {digest}")

Processing Files in Chunks

When dealing with large files, loading the entire content into memory at once is inefficient. Instead, you should read the file in small, manageable chunks to calculate the hash incrementally:

sha256_hash = hashlib.sha256()

with open("large_file.zip", "rb") as f:
    while chunk := f.read(8192):  # Read in 8KB chunks
        sha256_hash.update(chunk)

print(f"File Hash: {sha256_hash.hexdigest()}")

Important Distinctions

It is crucial to remember that hashlib provides integrity checks, not security for sensitive data.

  1. Integrity vs. Encryption: Hashes cannot be reversed to reveal the original data; encryption is designed to be reversible with a key.
  2. Passwords: Never store passwords using simple SHA-256 hashing. Because hashes are fast to calculate, attackers can use "brute force" to guess passwords easily. For passwords, always use specialized, slow, salted hashing algorithms (like bcrypt or argon2) to prevent such attacks.

Key Takeaway: Use hashlib to create fingerprints of your data to ensure it hasn't changed during transfer or storage, but remember that standard hashing is for integrity verification, not for securing passwords or hiding confidential information.