1

I have created a simple checksum script that checks a checksum of a file called tecu.a2l and compares it to a few .md5 files - ensuring that they all have the exact same checksum whenever this script is running.

To make things easier to understand:

Lets say i have tecu.a2l with the checksum 1x2x3x. So the md5 files (if generated correctly) should have the same checksum (1x2x3x). If one of the md5 files has a different checksum than what the latest tecu.a2l has it will give an error.

Hopefully the code might fill in the blanks if you did not quite understand my description.

import hashlib
import dst_creator_constants as CONST
import Tkinter

path_a2l = 'C:<path>\tecu.a2l'
md5 = hashlib.md5()
blocks = 65565
with open(path_a2l, 'rb') as a2l:
    readA2L = a2l.read(blocks)
    generatedMD5 = md5.hexdigest()
    print "stop1"

ihx_md5_files = CONST.PATH_DELIVERABLES_DST
for file in ihx_md5_files:
    print "stop2"
    if file.endswith('.md5'):
        print "stop3"
        readMD5 = file.read()
        if compare_checksums:
            print "Yes"
            # Add successful TkInter msg here
        else:
            print "No" 
            # Add error msg here


def compare_checksums(generatedMD5, readMD5):
    if generatedMD5 == readMD5:
        return True
    else:
        return False

When i run this script, nothing happens. No messages, nothing. If i type in python checksum.py into cmd - it returns no message. So i put in some print statements to see what could be the issue. The issue is, that stop3 is never shown in the command prompt - which means that the problem has something to do with if file.endswith('.md5'): statement.

I have no idea why it's the culprit as I have used this file.endswith() statement on a previous script I wrote in relation to this and there it has worked so I am turning to you.

Ren
  • 2,852
  • 2
  • 23
  • 45
e_phed
  • 21
  • 3
  • If it really should work, really, really, then maybe the problem is somewhere else. Can you show us the definition, or a print, of `CONST.PATH_DELIVERABLES_DST` ? – aghast Mar 22 '16 at 13:35
  • Also, `compare_checksums` is a reference to a function. You have to use `compare_checksums()` to call it. – aghast Mar 22 '16 at 13:36
  • It's just a path just as `path_a2l` I can as simply manually add the dir as `ihx_md5_files` and i did.. but it did not help. I've also used the same path definition before so there is no problem with it. adding parenthesis did not help CMD never writes `stop3` unfortunately. – e_phed Mar 22 '16 at 15:23
  • But you process it as a list, not a path. Instead of printing "stop2", try printing file. – aghast Mar 22 '16 at 22:20
  • I found the issue - the current working dir was different. Before the `for file in ihx_md5_files:` statement I simply added `os.chdir(CONST.PATH_DELIVERABLES_DST)` and now it's working :) Thanks @Austin Hastings I appreciate your efforts to help me. I guess you were on the right track. – e_phed Mar 23 '16 at 10:21

1 Answers1

0

You are not creating a hash object. Your file stays in your readA2L variable. Also, your file may be larger than the 65565 byte buffer you allow it. Try to update your hasher like the function below and let us know what the result is.

import hashlib as h
from os.path import isfile

hasher = h.md5()
block_size = 65536


def get_hexdigest(file_path, hasher, block_size):

    if isfile(file_path):
        with open(file_path, 'rb') as f:
            buf = f.read(block_size)

            while len(buf) > 0:
                # Update the hasher until the entire file has been read
                hasher.update(buf)
                buf = f.read(block_size)

        digest = hasher.hexdigest()
    else:
        return None
    return digest
A Magoon
  • 1,180
  • 2
  • 13
  • 21