1

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. :)

3
  • 1
    Yes, as the hash you're getting is a hash of an empty string. Read the docs. You need to initialize the hash: h.update(some_data). Commented Mar 4, 2015 at 14:18
  • 1
    you're not hashing your file at all. nowhere do you pass your selected filename to hashlib, so you're hashing a null value. and I'll bet the hash is d41d8cd98f00b204e9800998ecf8427e, which is what md5('') would return. Commented Mar 4, 2015 at 14:19
  • You are absolutely correct! So i looked further into it and with a little luck i found some old stuff. (look in main for new code) Commented Mar 4, 2015 at 15:16

2 Answers 2

5

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())
Sign up to request clarification or add additional context in comments.

Comments

2

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.