Reputation: 11
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. :)
Upvotes: 1
Views: 5536
Reputation: 1149
In your case you do the digest of the empty string and probably you get: d41d8cd98f00b204e9800998ecf8427e
I used this method to digest, that is better for big files (see here).
md5 = hashlib.md5()
with open(File, "rb") as f:
for block in iter(lambda: f.read(128), ""):
md5.update(block)
print(md5.hexdigest())
Upvotes: 4
Reputation: 44906
A very simple way
from hashlib import md5
f=open("file.txt","r")
data=f.read()
f.close()
Hash=md5(data).hexdigest()
out=open("out.txt","w")
out.write(Hash)
out.close()
Upvotes: 1