-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadv-checksum-checker.py
240 lines (161 loc) · 6.92 KB
/
adv-checksum-checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Include standard modules
import argparse
#import time
from tqdm import tqdm
from pathlib import Path
import hashlib
import difflib as dl
from py_console import console
# //////////////////////////////////////////////
#
# Advanced Checksum Checker v1.0
# by Brett Fullam
#
# For MD5, SHA-1, SHA-256 Checksum values
#
# Generates and compares MD5, SHA-1, and SHA-256 checksum values against a user-supplied checksum value and, if they do not match, indicates the differences per character between each hash value. Python 3 script that functions like a CLI tool optimized for ingesting large files such as operating system images quickly and efficiently that includes a progress bar for enhanced UX.
#
# //////////////////////////////////////////////
# Initiate the parser
parser = argparse.ArgumentParser(description="Python Automated IOC Generator v2.0 by Brett Fullam")
parser.add_argument("-m", "--MD5", help="select MD5 checksum type")
parser.add_argument("-1", "--SHA-1", help="select SHA-1 checksum type")
parser.add_argument("-256", "--SHA-256", help="select SHA-256 checksum type")
parser.add_argument("-c", "--checksum", help="user supplied checksum")
parser.add_argument("-V", "--version", help="show program version", action="store_true")
# ////////////////////////// BEGIN File name
def get_file_stats(arg):
# store a single entry value from direct user input
target_file = arg
global fileStats
# use Path module to access .stat results -- 'name' to grab the file name, and 'st_size' to grab the file size in bytes
file_ = Path(target_file)
fileName = (file_.name)
fileStats = (file_.stat().st_size)
# Grab the name of the file, create a custom string, print output to screen
fileNameOutput = "\nFile name: " + fileName + "\n"
print(fileNameOutput)
# Create a custom string to show the file size in bytes
fileSizeBytes = ("Size in bytes: " + str(fileStats) + "\n")
# Store the 'size' value as fileSizeBytes, create a custom string, print output to screen
print(fileSizeBytes)
return
# ////////////////////////// END File name
# ////////////////////////// BEGIN MD5 Checksum
def get_md5_checksum(arg):
# store a single entry value from direct user input
target_file = arg
# /// Hashing START
global hostChecksum
md5_hash = hashlib.md5()
pbar = tqdm(desc="Progress", total=int(fileStats), unit='B', unit_scale=True)
# Open and read the file contents to create the MD5 hash of the file
with open(target_file,"rb") as f4:
# Read and update hash string value in blocks of 4K
for byte_block in iter(lambda: f4.read(4096),b""):
md5_hash.update(byte_block)
pbar.update(len(byte_block))
pbar.close()
md5hash = (md5_hash.hexdigest())
hostChecksum = md5hash
# Output the MD5 hash value created by hashlib.md5() as reference
print("\nChecksum (MD5):")
md5Data = str(md5hash)
print(md5Data + "\n")
# /// Hashing END
return
# ////////////////////////// END MD5 Checksum
# ////////////////////////// BEGIN SHA-1 Checksum
def get_sha1_checksum(arg):
# store a single entry value from direct user input
target_file = arg
# /// Hashing START
global hostChecksum
sha1_hash = hashlib.sha1()
pbar = tqdm(desc="Progress", total=int(fileStats), unit='B', unit_scale=True)
# Open and read the file contents to create the SHA-1 hash of the file
with open(target_file,"rb") as f3:
# Read and update hash string value in blocks of 4K
for byte_block in iter(lambda: f3.read(4096),b""):
sha1_hash.update(byte_block)
pbar.update(len(byte_block))
pbar.close()
sha1hash = (sha1_hash.hexdigest())
hostChecksum = sha1hash
# Output the SHA-1 hash value created by hashlib.sha1() as reference
print("\nChecksum (SHA-1):")
sha1Data = str(sha1hash + "\n")
print(sha1Data)
return
# ////////////////////////// END SHA-1 Checksum
# ////////////////////////// BEGIN SHA-256 Checksum
def get_sha256_checksum(arg):
# store a single entry value from direct user input
target_file = arg
# /// Hashing START
global hostChecksum
sha256_hash = hashlib.sha256()
pbar = tqdm(desc="Progress", total=int(fileStats), unit='B', unit_scale=True)
# Open and read the file contents to create the SHA-256 hash of the file
with open(target_file,"rb") as f2:
# Read and update hash string value in blocks of 4K
for byte_block in iter(lambda: f2.read(4096),b""):
sha256_hash.update(byte_block)
pbar.update(len(byte_block))
pbar.close()
sha256hash = (sha256_hash.hexdigest())
hostChecksum = sha256hash
# Output the SHA-256 hash value created by hashlib.sha256() as reference
print("\nChecksum (SHA-256):")
sha256Data = str(sha256hash + "\n")
print(sha256Data)
return
# ////////////////////////// END SHA-256 Checksum
# ////////////////////////// BEGIN Compare Checksum
def compare_checksum(arg):
input_checksum = arg
if hostChecksum == input_checksum:
print("Checksum (user supplied): ")
print(input_checksum + "\n" + "––\n\n")
console.success("////// Checksum values MATCH! //////\n\n", showTime=False)
else:
print("Checksum (user supplied): ")
print(input_checksum + "\n" + "––\n\n")
console.error("////// Checksum values DO NOT match! //////\n\n", showTime=False)
print("Checksum Comparison:\n")
diff_checksum(hostChecksum, input_checksum)
print("\n\n")
return
# ////////////////////////// END Compare Checksum
# ////////////////////////// BEGIN Compare Checksum DIFF
# compares the hash generated by this script to the user supplied hash, and indicates the differences between each character in each of the hash values
def diff_checksum(arg1 , arg2):
diff_items = dl.ndiff([arg1] , [arg2])
for diff in diff_items:
console.error(diff, showTime=False)
# ////////////////////////// END Compare Checksum DIFF
# //////////////////////////////////////////////
# Read arguments from the command line
args = parser.parse_args()
# Check for --MD5 or -m AND --checksum or -c
if args.MD5 and args.checksum:
get_file_stats(args.MD5)
get_md5_checksum(args.MD5)
compare_checksum(args.checksum)
# Check for --SHA_1 or -1 AND --checksum or -c
elif args.SHA_1 and args.checksum:
get_file_stats(args.SHA_1)
get_sha1_checksum(args.SHA_1)
compare_checksum(args.checksum)
# Check for --SHA_256 or -256 AND --checksum or -c
elif args.SHA_256 and args.checksum:
get_file_stats(args.SHA_256)
get_sha256_checksum(args.SHA_256)
compare_checksum(args.checksum)
# Check for --version or -V
elif args.version:
print(args.version)
print("Checksum Checker version 2.0")
# Print usage information if no arguments are provided
else:
print("usage: checksum-checker-i1.1.py [-h] [-m MD5] [-1 SHA_1] [-256 SHA_256] [-V]")