03 / EVIDENCE AND CORRELATION · LESSON 09

Build an evidence hash inventory

Hash original file bytes and record relative paths for a reproducible manifest.

15 min · learn at your pace

Bytes are the evidence

Opening a file as text can decode characters or translate line endings. Those are useful operations for reading but inappropriate before an integrity hash. Use Path.read_bytes() to calculate SHA-256 from the exact stored content. Even a one-byte change should produce a different digest.

Record a path, byte count, and lowercase hexadecimal digest. A relative path makes a manifest portable when the evidence directory moves. Use POSIX-style separators so the result has a consistent representation across environments.

Make the inventory reproducible

Filesystem enumeration does not promise a useful order. Sort paths before producing rows, include nested files, and omit directories. An empty evidence directory produces an empty list. A missing directory raises an error because it says nothing about whether evidence exists elsewhere.

Two files with identical content have the same digest but still need separate path entries. A hash identifies bytes, not a unique file location. A manifest becomes more valuable when you also retain how, when, and by whom the evidence was collected; this browser exercise covers the byte comparison portion.

For large evidence, read in chunks and update a hash object incrementally. Our supplied files are small, so a whole-file read is sufficient. Neither approach should modify the source. The checks include binary and Unicode data to catch accidental text decoding.

Try an example.py
from pathlib import Path
from hashlib import sha256
payload = Path("evidence/readme.txt").read_bytes()
print(len(payload), sha256(payload).hexdigest())
YOUR TURN

Put it into practice.

Define hash_inventory(root). Require an existing directory, otherwise raise FileNotFoundError. Return every nested regular file as {path: relative POSIX path, size: byte count, sha256: lowercase digest}, sorted by path. Exclude directories, preserve original bytes, and return [] for an empty directory. No symlinks are supplied.

See an explained solution
One way to solve it.py
from pathlib import Path
from hashlib import sha256

def hash_inventory(root):
    directory = Path(root)
    if not directory.is_dir():
        raise FileNotFoundError(root)
    rows = []
    for path in sorted(directory.rglob("*")):
        if path.is_file():
            content = path.read_bytes()
            rows.append({"path": path.relative_to(directory).as_posix(), "size": len(content), "sha256": sha256(content).hexdigest()})
    return rows

The inventory sorts relative paths and hashes original bytes. It preserves separate locations even when their contents are identical.