Python Program to find hash of file

In this tutorial, we will learn how to create a Python program to compute the hash of a file using various hashing algorithms. The program uses the hashlib module and handles large files efficiently by reading them in chunks. It also includes error handling for invalid file paths and hashing algorithms

Setting Up the Environment to find hash of file in Python

Prerequisites

  • Python installed on your machine (Python 3.6+ recommended).
  • Basic knowledge of Python programming.

Installing Python

If Python is not already installed, learn how to install Python.

Writing the Python Program to find hash of file

Step 1: Importing Necessary Modules

We will use the hashlib module, which provides a common interface to many secure hash and message digest algorithms.

import hashlib

Step 2: Defining the compute_file_hash Function

This function will take two parameters: the file path and the hashing algorithm. It will read the file in chunks and compute the hash.

Python
def compute_file_hash(file_path, algorithm='sha256'):
    """Compute the hash of a file using the specified algorithm."""
    hash_func = hashlib.new(algorithm)
    
    with open(file_path, 'rb') as file:
        # Read the file in chunks of 8192 bytes
        while chunk := file.read(8192):
            hash_func.update(chunk)
    
    return hash_func.hexdigest()


Explanation

  • hashlib.new(algorithm): Creates a new hash object using the specified algorithm.
  • with open(file_path, 'rb') as file: Opens the file in binary mode.
  • while chunk := file.read(8192): Reads the file in chunks of 8192 bytes. This approach handles large files efficiently.
  • hash_func.update(chunk): Updates the hash object with each chunk of data.
  • hash_func.hexdigest(): Returns the final hash value as a hexadecimal string.

Step 3: Defining the main Function

The main function will prompt the user for the file path and the desired hashing algorithm, then call compute_file_hash and display the result.

Python
def main():
    file_path = input("Enter the path to the file: ")
    algorithm = input("Enter the hash algorithm (e.g., md5, sha1, sha256): ")
    
    try:
        file_hash = compute_file_hash(file_path, algorithm)
        print(f"The {algorithm} hash of the file is: {file_hash}")
    except FileNotFoundError:
        print("File not found. Please enter a valid file path.")
    except ValueError:
        print(f"Invalid hash algorithm: {algorithm}. Please enter a valid algorithm (e.g., md5, sha1, sha256).")

if __name__ == "__main__":
    main()


Explanation

  • file_path = input("Enter the path to the file: "): Prompts the user for the file path.
  • algorithm = input("Enter the hash algorithm (e.g., md5, sha1, sha256): "): Prompts the user for the hashing algorithm.
  • compute_file_hash(file_path, algorithm): Calls the function to compute the hash.
  • Exception Handling:
    • FileNotFoundError: Handles the case where the file path is invalid.
    • ValueError: Handles invalid hashing algorithms.

Complete Program

Here is the complete program with all the steps integrated:

Python
import hashlib

def compute_file_hash(file_path, algorithm='sha256'):
    """Compute the hash of a file using the specified algorithm."""
    hash_func = hashlib.new(algorithm)
    
    with open(file_path, 'rb') as file:
        while chunk := file.read(8192):  # Read the file in chunks of 8192 bytes
            hash_func.update(chunk)
    
    return hash_func.hexdigest()

def main():
    file_path = input("Enter the path to the file: ")
    algorithm = input("Enter the hash algorithm (e.g., md5, sha1, sha256): ")
    
    try:
        file_hash = compute_file_hash(file_path, algorithm)
        print(f"The {algorithm} hash of the file is: {file_hash}")
    except FileNotFoundError:
        print("File not found. Please enter a valid file path.")
    except ValueError:
        print(f"Invalid hash algorithm: {algorithm}. Please enter a valid algorithm (e.g., md5, sha1, sha256).")

if __name__ == "__main__":
    main()

Output

Enter the path to the file: example.txt

Enter the hash algorithm (e.g., md5, sha1, sha256): sha256

The sha256 hash of the file is: d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2

Python Program to find hash of file

  1. Save the script to a file, e.g., file_hash.py.
  2. Open your command line interface (CLI).
  3. Navigate to the directory containing file_hash.py.
  4. Run the script:
    python file_hash.py
  5. Enter the file path and the desired hashing algorithm when prompted.


Contact Us