Skip to content

Commit dbad3f8

Browse files
committed
add file integrity verifier tutorial
1 parent 2f2d42c commit dbad3f8

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
4545
- [How to Make a Login Password Guesser in Python](https://thepythoncode.com/article/make-a-login-password-guesser-in-python). ([code](ethical-hacking/login-password-guesser))
4646
- [How to Build a Password Manager in Python](https://thepythoncode.com/article/build-a-password-manager-in-python). ([code](ethical-hacking/password-manager))
4747
- [How to List Wi-Fi Networks in Python](https://thepythoncode.com/article/list-nearby-wifi-networks-with-python). ([code](ethical-hacking/listing-wifi-networks))
48+
- [How to Verify File Integrity in Python](https://thepythoncode.com/article/verify-downloaded-files-with-checksum-in-python). ([code](ethical-hacking/verify-file-integrity))
4849

4950
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
5051
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Verify File Integrity in Python](https://thepythoncode.com/article/verify-downloaded-files-with-checksum-in-python)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)