Skip to content

Instantly share code, notes, and snippets.

@JacobHearst
Last active November 6, 2018 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobHearst/2f4cffe17447116c850f7241f655e9a2 to your computer and use it in GitHub Desktop.
Save JacobHearst/2f4cffe17447116c850f7241f655e9a2 to your computer and use it in GitHub Desktop.
Simple GUI MD5/SHA256/SHA1 Hashsum Checker and Generator
import hashlib
from tkinter import *
from tkinter.filedialog import askopenfilename
class App:
BLOCK_SIZE = 4096
# Initialize the application window
def __init__(self, master):
frame = Frame(master)
frame.grid()
# Row 1
Label(master, text="File:").grid(row=0, sticky=W, padx=5)
self.file_input = Entry(master, width=43)
self.file_input.grid(row=0, column=1, padx=5, pady=5)
file_select = Button(master, text="...",
command=self.select_file, relief=GROOVE)
file_select.grid(row=0, column=2, padx=5, sticky=W)
# Row 2
Label(master, text="Sum:").grid(row=1, sticky=W, padx=5)
self.hashsum = Entry(master, width=43)
self.hashsum.grid(row=1, column=1, sticky=W, padx=5, pady=5)
self.hashsum_pf = StringVar()
hashsum_pf_label = Label(master, textvariable=self.hashsum_pf)
hashsum_pf_label.grid(row=1, column=2)
# Row 3
Label(master, text="Type:").grid(row=2, padx=5)
self.enc_type = StringVar(master)
self.enc_type.set("MD5")
enc_dropdown = OptionMenu(master, self.enc_type, "MD5", "SHA256", "SHA1")
enc_dropdown.grid(row=2, column=1, pady=5, sticky=W)
enc_dropdown.config(relief=GROOVE)
Button(master, text="Go", relief=GROOVE, command=self.check_hash).grid(
row=2, column=2, pady=5, sticky=W)
# Present the user with a file choosing dialog
def select_file(self):
file_path = askopenfilename()
# Update the file path field
self.file_input.delete(0, END)
self.file_input.insert(0, file_path)
# Generate the checksum for the specified file and check if it matches the one provided
def check_hash(self):
# Map dropdown values to their respective hashing functions
func_map = {
"MD5": hashlib.md5,
"SHA256": hashlib.sha256,
"SHA1": hashlib.sha1
}
# Select the proper hashing function
hasher = func_map[self.enc_type.get()]()
# Read in the file in blocks instead of trying to process it all at once
with open(self.file_input.get(), "rb") as f:
for chunk in iter(lambda: f.read(self.BLOCK_SIZE), b""):
hasher.update(chunk)
# Did the user insert a hashsum to check?
if len(self.hashsum.get()) > 0:
# Compare the generated hashsum to the provided one
if hasher.hexdigest() == self.hashsum.get().lower():
self.hashsum_pf.set("P")
else:
self.hashsum_pf.set("F")
else:
# Update the hashsum field with the generated sum
self.hashsum.insert(0, hasher.hexdigest())
self.hashsum_pf.set("")
master = Tk()
master.title("Checksum Tool")
master.geometry("350x100")
master.resizable(0, 0)
app = App(master)
master.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment