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

Tales Pádua
  • 1,331
  • 1
  • 16
  • 36
MonksyJunior
  • 11
  • 1
  • 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)`. – ForceBru Mar 04 '15 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. – Marc B Mar 04 '15 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) – MonksyJunior Mar 04 '15 at 15:16

2 Answers2

4

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())
Community
  • 1
  • 1
Thomas8
  • 1,117
  • 1
  • 11
  • 22
1

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()
ForceBru
  • 43,482
  • 10
  • 63
  • 98