Skip to content

File Integrity Checker  #1

@poorni286

Description

@poorni286

Here’s a clear plan and sample implementation for your File Integrity Checker in Python, as requested for your CODTECH internship task.


Task Overview

Goal: Build a Python tool that monitors file changes by calculating and comparing hash values (using hashlib) to ensure file integrity.


How It Works

  1. Initial Hashing: Calculate and store the hashes (e.g., SHA256) of target files.
  2. Monitoring: On subsequent runs, recalculate and compare hashes to detect changes.
  3. Reporting: Notify if any file has changed, been deleted, or added.

Sample Python Script: File Integrity Checker

import hashlib
import os
import json

HASH_FILE = 'file_hashes.json'  # Where hashes are stored

def get_file_hash(filename):
    """Calculate SHA256 hash of a file."""
    sha256 = hashlib.sha256()
    try:
        with open(filename, 'rb') as f:
            while chunk := f.read(8192):
                sha256.update(chunk)
        return sha256.hexdigest()
    except FileNotFoundError:
        return None

def scan_directory(directory):
    """Scan all files in the directory and get their hashes."""
    file_hashes = {}
    for root, _, files in os.walk(directory):
        for name in files:
            filepath = os.path.join(root, name)
            file_hashes[filepath] = get_file_hash(filepath)
    return file_hashes

def load_hashes():
    """Load stored file hashes."""
    if os.path.exists(HASH_FILE):
        with open(HASH_FILE, 'r') as f:
            return json.load(f)
    return {}

def save_hashes(hashes):
    """Save file hashes to disk."""
    with open(HASH_FILE, 'w') as f:
        json.dump(hashes, f, indent=2)

def compare_hashes(old_hashes, new_hashes):
    """Compare old and new hashes to find changes."""
    changed = []
    deleted = []
    added = []

    for file, old_hash in old_hashes.items():
        if file not in new_hashes:
            deleted.append(file)
        elif old_hash != new_hashes[file]:
            changed.append(file)
    for file in new_hashes:
        if file not in old_hashes:
            added.append(file)
    return changed, deleted, added

if __name__ == "__main__":
    directory = input("Enter directory to monitor: ").strip()
    current_hashes = scan_directory(directory)
    previous_hashes = load_hashes()

    changed, deleted, added = compare_hashes(previous_hashes, current_hashes)

    if not previous_hashes:
        print("Initial scan complete. Hashes saved.")
    else:
        if changed:
            print("Changed files:", *changed, sep="\n- ")
        if deleted:
            print("Deleted files:", *deleted, sep="\n- ")
        if added:
            print("Added files:", *added, sep="\n- ")
        if not (changed or deleted or added):
            print("No changes detected.")

    save_hashes(current_hashes)

Instructions

  1. Save the script (e.g., as file_integrity_checker.py).
  2. Run the script:
    python file_integrity_checker.py
    
  3. Enter the directory you want to monitor.
  4. First run saves the baseline.
    Subsequent runs will report any changes.

You can further enhance this script (optional):

  • Email/SMS alerts
  • Monitor only specific file types
  • Hash only files above/below certain sizes
  • Integrate with a GUI

Let me know if you need explanation or modifications!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions