0

I'm trying to encrypt files inside selected folder with SHA256. First file is always got the true SHA256sum. But after the first file, other hashes are false. The paths true, selected files and folders are true, but sha256sums are false. Can anyone explain me why? Thanks.

window = Tk()
window.title("Crypt")

l1 = Listbox(window)


def folderScan():
    counter=1
    hashvalue=0
    BLOCKSIZE= 65536
    hasher = hashlib.sha256()

    try:
        folder = askdirectory(parent=window, title='Select a folder')

    except:
        tkMessageBox.showerror("ERROR!", "Folder selection error. Exiting..")
        window.destroy()

    try:
        for i in os.walk(folder):
            print("-------------*---------------*--------------")
            print("i[0]= ",i[0]) # The current path.
            print("i[1]= ",i[1]) # Folders inside our path.
            print("i[2]= ",i[2]) # Files inside our path.

            for j in i[2]:
                fullPath = i[0]+"/"+j

                try:           
                    with open(fullPath,'rb') as tara:
                        buf = tara.read(BLOCKSIZE)
                        while len(buf) > 0:
                            hasher.update(buf)
                            buf = tara.read(BLOCKSIZE)
                            hashvalue = hasher.hexdigest()
                    l1.insert(counter,fullPath)
                    print("For "fullPath+" the hash is: "+hashvalue)

                except:
                    print("Encrypt failed!")

    except:
        tkMessageBox.showerror("Error!", "Failed to get folder content. Exiting..")
        window.destroy()
  • what is the size of the first file (which is ok) and the second (which is nok)? – daouzli Jun 20 '14 at 08:15
  • 2
    Start with [Hashing vs Encryption](http://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms) and then clarify your question. – Oleg Estekhin Jun 20 '14 at 08:16
  • 1
    Does Python's `hasher` need to be reset when starting a new independent hashing session? – Oleg Estekhin Jun 20 '14 at 08:16

1 Answers1

0

You are reusing the hasher object for every file, so the resulting hash value will be of the current file concatenated to every file before it. Use a different hasher for each file will give the correct result.

...
for j in i[2]:
    hasher = hashlib.sha256()
    fullPath = i[0]+"/"+j
    try:           
        with open(fullPath,'rb') as tara:
            buf = tara.read(BLOCKSIZE)
            while len(buf) > 0:
                hasher.update(buf)
                buf = tara.read(BLOCKSIZE)
                hashvalue = hasher.hexdigest()
        l1.insert(counter,fullPath)
        print("For "fullPath+" the hash is: "+hashvalue)
    except:
        print("Encrypt failed!")
....

Also, SHA-256 is a hash, not an encryption. You'd better get that misconception out of your mind.

Kien Truong
  • 11,179
  • 2
  • 30
  • 36