Sha sum files with Python

From PedrosBrainDump
Revision as of 02:48, 8 November 2024 by 413vhcu1lq0463ob (talk | contribs) (Created page with " import hashlib def calculate_sha256(file_path): sha256_hash = hashlib.sha256() with open(file_path, "rb") as f: # Read and update hash string value in chunks of 4K for byte_block in iter(lambda: f.read(4096), b""): sha256_hash.update(byte_block) return sha256_hash.hexdigest() # Usage file_path = "path/to/your/file" print("SHA-256:", calculate_sha256(file_path))")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
import hashlib

def calculate_sha256(file_path):
    sha256_hash = hashlib.sha256()
    with open(file_path, "rb") as f:
        # Read and update hash string value in chunks of 4K
        for byte_block in iter(lambda: f.read(4096), b""):
            sha256_hash.update(byte_block)
    return sha256_hash.hexdigest()

# Usage
file_path = "path/to/your/file"
print("SHA-256:", calculate_sha256(file_path))