|
| 1 | +# Import necessary libraries. |
| 2 | +import argparse, hashlib, sys |
| 3 | + |
| 4 | +# Import functions init and Fore from the colorama library. |
| 5 | +from colorama import init, Fore |
| 6 | + |
| 7 | +# Initialize colorama to enable colored terminal text. |
| 8 | +init() |
| 9 | + |
| 10 | +# Define a function to calculate the SHA-256 hash of a file. |
| 11 | +def calculate_hash(file_path): |
| 12 | + # Create a SHA-256 hash object. |
| 13 | + sha256_hash = hashlib.sha256() |
| 14 | + |
| 15 | + # Open the file in binary mode for reading (rb). |
| 16 | + with open(file_path, "rb") as file: |
| 17 | + # Read the file in 64KB chunks to efficiently handle large files. |
| 18 | + while True: |
| 19 | + data = file.read(65536) # Read the file in 64KB chunks. |
| 20 | + if not data: |
| 21 | + break |
| 22 | + # Update the hash object with the data read from the file. |
| 23 | + sha256_hash.update(data) |
| 24 | + |
| 25 | + # Return the hexadecimal representation of the calculated hash. |
| 26 | + return sha256_hash.hexdigest() |
| 27 | + |
| 28 | + |
| 29 | +# Define a function to verify the calculated hash against an expected hash. |
| 30 | +def verify_hash(downloaded_file, expected_hash): |
| 31 | + # Calculate the hash of the downloaded file. |
| 32 | + calculated_hash = calculate_hash(downloaded_file) |
| 33 | + |
| 34 | + # Compare the calculated hash with the expected hash and return the result. |
| 35 | + return calculated_hash == expected_hash |
| 36 | + |
| 37 | + |
| 38 | +# Create a parser for handling command-line arguments. |
| 39 | +parser = argparse.ArgumentParser(description="Verify the hash of a downloaded software file.") |
| 40 | + |
| 41 | +# Define two command-line arguments: |
| 42 | +# -f or --file: Path to the downloaded software file (required). |
| 43 | +# --hash: Expected hash value (required). |
| 44 | +parser.add_argument("-f", "--file", dest="downloaded_file", required=True, help="Path to the downloaded software file") |
| 45 | +parser.add_argument("--hash", dest="expected_hash", required=True, help="Expected hash value") |
| 46 | + |
| 47 | +# Parse the command-line arguments provided when running the script. |
| 48 | +args = parser.parse_args() |
| 49 | + |
| 50 | +# Check if the required command-line arguments were provided. |
| 51 | +if not args.downloaded_file or not args.expected_hash: |
| 52 | + # Print an error message in red using 'colorama'. |
| 53 | + print(f"{Fore.RED}[-] Please Specify the file to validate and its Hash.") |
| 54 | + # Exit the script. |
| 55 | + sys.exit() |
| 56 | + |
| 57 | +# Check if the hash of the file is accurate by calling the verify_hash function. |
| 58 | +if verify_hash(args.downloaded_file, args.expected_hash): |
| 59 | + # If the hash is accurate, print a success message in green. |
| 60 | + print(f"{Fore.GREEN}[+] Hash verification successful. The software is authentic.") |
| 61 | +else: |
| 62 | + # If the hash does not match, print an error message in red. |
| 63 | + print(f"{Fore.RED}[-] Hash verification failed. The software may have been tampered with or is not authentic.") |
0 commit comments