Verify file modifications with this simple script

Start
#Generating SHA256 hash of every file in all directories and storing them to a file checked-hash.txt at /home/rocheston 

import os
import hashlib

#Path of the directory where we want to generate hashes
base_path = "/home/rocheston"

#Creating the output file in the same directory
output_file = open(os.path.join(base_path,'checked-hash.txt'),'w')

#Iterating through each file
for root,dirs,files in os.walk(base_path):
    for item in files:
        #Getting the full path of the file
        item_full_path = os.path.join(root,item)
        #Opening the file
        with open(item_full_path,"rb") as f:
            #Reading the content from the file
            content = f.read()
            #Generating the sha256 hash
            sha256_hash = hashlib.sha256(content).hexdigest()
            #Writing the path, filename and the sha256 hash to the output file
            output_file.write("{} {} {}\n".format(item_full_path,item,sha256_hash))
            
#Closing the output file
output_file.close()

#Displaying the changes to files
import time

#Path of the directory where we want to generate hashes
base_path = "/home/rocheston"

#Checking the modification time of files
while True:
    for root,dirs,files in os.walk(base_path):
        for item in files:
            #Getting the full path of the file
            item_full_path = os.path.join(root,item)

            #Comparing the current modification time of the file with the previous one
            current_modification_time = os.path.getmtime(item_full_path)
            if current_modification_time != previous_modification_time:
                print("Changes detected in {}".format(item_full_path))
            previous_modification_time = current_modification_time

    #Delaying for 5 seconds
    time.sleep(5)
Previous Story

Hive ransomware servers shut down at last, says FBI

Next Story

AA23-025A: Protecting Against Malicious Use of Remote Monitoring and Management Software