I am trying to generate hashes of files using hashlib inside Tkinter modules. My goal:
Step 1:- Button (clicked), opens up a browser (click file you want a hash of). Step 2:- Once file is chosen, choose output file (.txt) where the hash will be 'printed'. Step 3:- Repeat and have no clashes.
from tkinter.filedialog import askopenfilename
import hashlib
def hashing():
hash = askopenfilename(title="Select file for Hashing")
savename = askopenfilename(title="Select output")
outputhash = open(savename, "w")
hash1 = open(hash, "r")
h = hashlib.md5()
print(h.hexdigest(), file=outputhash)
love.flush()
It 'works' to some extent, it allows an input file and output file to be selected. It prints the hash into the output file.
HOWEVER - If i choose ANY different file, i get the same hash everytime.
Im new to Python and its really stumping me.
Thanks in advance.
Thanks for all your comments.
I figured the problem and this is my new code:
from tkinter.filedialog import askopenfilename
import hashlib
def hashing():
hash = askopenfilename(title="Select file for Hashing")
savename = askopenfilename(title="Select output")
outputhash = open(savename, "w")
curfile = open(hash, "rb")
hasher = hashlib.md5()
buf = curfile.read()
hasher.update(buf)
print(hasher.hexdigest(), file=outputhash)
outputhash.flush()
This code works, You guys rock. :)