def warn(): print(" Note: Must be run in Terminal") print(" e.g. python md5_IX_v01o1.py -r filename hashTotal") #def end import sys import hashlib #by D@CC #on 2023ISep25 progName="md5_IX_v01o1.py" print("progName:",progName) warn() #Issue: The subjectFile must NOT begin with more than 1 "#########_"+"MD5_IX " records # Result: the hashed file (without Record 1) will begin with a bad "###..." record #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 prefixMD5='#########'+'_MD5_IX ' print('try grep "'+prefixMD5+'" fileName(s)') 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() md5woR1 = hashlib.md5() #sha1 = hashlib.sha1() controlString=sys.argv[1] controlString=controlString[1:2] print("controlString:",controlString) if "aefhru".find(controlString) >-1: 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 (parm3)") #get first record only with open(subjectFile, 'r') as t: rec=t.readline().strip() lenPrefix=17 prefix=rec[0:17] if prefix==prefixMD5: suffix=rec[17:len(rec)] #print("prefix:"+prefix+":") #print("suffix:"+suffix+":") r1h=suffix #get real MD5 from rec 1 isMD5inRec1=True else: r1h="missing rec1" isMD5inRec1=False #if end #print("line 64 r1h",r1h,":",rec," r1h") print(" ",r1h,":r1h") #with end t.close() # chop off rec1, write out file as "temp.txt" lenSubjectFile=len(subjectFile) tempStr=subjectFile[0:lenSubjectFile-4] #print("tempStr:",tempStr) tempName=tempStr+"_woR1"+subjectFile[-4:lenSubjectFile] #print("at 85 tempName:",tempName) temp=open(tempName, 'w') n=1 with open(subjectFile, 'r') as t: if isMD5inRec1 : # skip record 1 rec=t.readline() n+=1 isMD5inRec1=False #if end while True: n+=1 rec=t.readline() #print(n,rec) if rec=="": break temp.write(rec) #while end #print("eof") #with end temp.close() t.close() #print("temp written:",n," records") #print("at 107 tempName:",tempName) #print("a {0} :MD5".format(md5.hexdigest())) #print("hexdigest",md5.hexdigest()) with open(tempName, 'rb') as f: #with open(sys.argv[2], 'rb') as f: while True: data = f.read(BUF_SIZE) #print("data:",data,":") if not data: break md5.update(data) #sha1.update(data) # with end f.close() print(" {0} :MD5".format(md5.hexdigest())) #print("hexdigest",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 exist.") #if end print("Name of hashed file (any MD5_IX record 1 was excluded):") print(" ",tempName) #print("SHA1: {0}".format(sha1.hexdigest())) #if end r if controlString=="h": print() print("md5_IX help information") warn() print("invoke with 3 parameters:") print(" controlString") print(" fileName") print(" md5-hash-total (expected) or 0") print("valid controlStrings are: -r, -h") print("File actually hashed is named before program exits") print('Use grep "'+prefixMD5+'" fileName(s)') print(" which will list hash totals for all files previously processed by md5_IX") print("Users should insert a hash_MD5_IX record as rec1 of each file") print(' rec1 format is "'+prefixMD5+'md5-hash-total"') print('Source: http://www.ePhotoCaption.com/a/206/206.html ') #if end