import sys import hashlib print(" Note: Must be run in Terminal") print(" e.g. python md5_IX_v01.py -r filename hashTotal") #by D@CC #on 2023ISep25 progName="md5_IX_v01.py" print("progName:",progName) #Hash Source: https://stackoverflow.com/questions/22058048/hashing-a-file-in-python # compare to: # $ md5-console -f bigfile # $ shasum bigfile #Snap Source:https://snapcraft.io/install/md5-console/raspbian # $ sudo apt update # $ sudo apt install snapd # then reboot # $ sudo snap install core # $ sudo snap install snapcraft --classic # $ sudo snap install md5-console # the last step took several minutes import time; now=int(time.time());print ("UnixEpochTime:",now) # BUF_SIZE is totally arbitrary, change for your app! BUF_SIZE = 65536 # lets read stuff in 64kb chunks! subjectFile=sys.argv[2] print("subjectFile:",subjectFile) md5 = hashlib.md5() #sha1 = hashlib.sha1() controlString=sys.argv[1] controlString=controlString[1:2] print("controlString:",controlString) if "aefru".find(controlString) : print("controlString is valid") else: print("Error 01: Invalid Control String:",controlString) exit #if end if controlString=="r": md5old=sys.argv[3] print(md5old,":md5") #get first record only with open(subjectFile, 'r') as t: rec=t.readline().strip() lenPrefix=17 prefix=rec[0:17] if prefix=="#########_MD5_IX ": suffix=rec[17:len(rec)] #print("prefix:"+prefix+":") #print("suffix:"+suffix+":") r1h=suffix #get real MD5 from rec 1 else: r1h="bad" #if end print(r1h,":r1h") #with end t.close() # chop off rec1, write out file as "temp.txt" temp=open("temp.txt", 'w') with open(subjectFile, 'r') as t: rec=t.readline() n=1 while True: n+=1 rec=t.readline() #print(n,rec) if rec=="": break temp.write(rec) #while end #print("eof") #with end #print("temp written:",n," records") with open("temp.txt", 'rb') as f: #with open(sys.argv[2], 'rb') as f: while True: data = f.read(BUF_SIZE) if not data: break md5.update(data) #sha1.update(data) # with end f.close() print("{0} :MD5".format(md5.hexdigest())) if md5old==md5.hexdigest() and md5old==r1h: print("This is a valid md5_IX file. The 3 md5 agree.") else: print("Bad md5 hashes.") #if end #print("SHA1: {0}".format(sha1.hexdigest())) #if end r