md5_IX.py by D@CC on 2023ISep25 Purpose: to create a hash-total verification program that will ensure the integrity of a subject file. The Hash total of a subject-file is often calculated in order to ensure that the file's contents have not been changed. Normally, the hash total of the file is stored in a second file located in the same folder as the first. My issue with this process is that the procedure is compromised if the "hash-total" file is lost or separated from the subject-file. My solution is to insert one line at the beginning of the file. This can easily be done with a python program file by prefacing the "hash-total" with "##########_MD5_IX01:". Any Python compiler or interpreter will ignore this record because it is flagged as a comment by its initial "#". This can also be done with any text file. If the programs using this text file know that this special hash record should be ignored, then the fileName can be the same name as the text file without the "hash record". If the program reading the text file cannot "ignore" the initial hash record, then the file must be changed before being used. The change is merely that the first record will be removed from the text file. It is suggested that for these cases, the extension of the file contaning the hash total will be ".tx5" . If the program using the data file can ignore a first data record with the prefix of "##########_MD5_IX01:" followed by a hash total, then the hash file can use the extension of ".txt" This file will be named The initial md5_IX.py program will be given three parameters: the first parameter is -f to signify that a file is to be hashed or -r to signify that a file is to be validated. If this parameter is missing, it defaults to -r. Files to be validated are "read-only" and will NOT be changed, the second parameter is the name of the subject-file. the third parameter is the name designating the hash total. This third parameter, if missing, is understood to be "_MD5_IX01" Examples of a subject-file name are "PiR2F.txt" or "Prog01.py". The "Prog01.py" example will be used in the remainder of this article. Case -r Verify if the subject file has a valid hash total. The md5_IX.py program will read the first record of the subject file. If it does not begin with "##########_MD5_IX01:", then it cannot be validated and a log error "fileName nnn: no Hash record" will be raised and the program will abort. If it does begin with a hash record, the hash total in the record will be saved as a string named "strMD5old" The hash total of the remaining records will be calculated and saving in "strMD5curr". If strMD5old==strMD5curr no error will be raised. If strMD5old differs from strMD5curr a log error will result saying "fileName nnn: bad Hash record" If an error occurs, an error file explaining the error will be written to the disk. The error file will be named "fileName_unixtime.err" The program will exit without error. Note that Case -r will decide if a file has an improper hash total an an error file will be created. Case -f Convert the subject file to a valid "has" file with a hash total in the first record. But leave unchanged any subject file with a valid hash total. The md5_IX.py program will read the first record of the subject file. If it does begin with a hash record, the hash total in the record will be saved as a string named "strMD5old" If it doesn't begin with a hash record, then "strMD5old" will be set to "none" In either case, the hash total of the remaining records will be calculated and saved in "strMD5curr". If strMD5old==strMD5curr then no error will be raised and the program will exit without error. If strMD5old differs from strMD5curr, then 1. the subject file will be renamed by adding the suffix "yyyyMmmmdd" to the fileName. 2. a log message will say "fileName: hash total was updated. 2. a new file will be created with a new first record containing "strMD5curr" as the hash total 3. the name of the new file will be the unchanged subject fileName. 4. The program will exit without error. Note that Case -f never fails and NO error file will be created. However, the subject file may be changed. If so, a copy of the old subject file is kept with a slight name change. The resulting subject file always has a valid "hash total" record. This is the end of program definition of md5_IX.py /md5_IX.txt