03 / INTEGRITY AND CONFIGURATION · LESSON 09

Build a SHA-256 evidence inventory

Hash original file bytes with a portable command wrapper and retain their paths.

16 min · learn at your pace

Fingerprints, not verdicts

SHA-256 produces a digest of file bytes. Matching digests are useful for checking whether a file changed between two observations. A digest alone does not tell you that a file is safe, who created it, or whether it was already altered before collection. Record provenance and protect a trusted baseline separately.

Linux provides sha256sum; macOS provides shasum -a 256. Wrap the choice in a small function using command -v. Redirect the file to standard input so the utility does not include its potentially awkward filename in the result. Extract the first space-delimited field, then print the path yourself with a constant format.

Preserve bytes and boundaries

Do not put file contents through command substitution before hashing. Substitution removes trailing newlines, which changes the bytes and therefore the digest. Hash the original stream directly, including empty files and non-ASCII text.

The supplied manifest is trusted and lists basenames, not arbitrary paths. Keep spaces and Unicode intact with IFS= read -r and quoted expansions. This lab hashes inert files and never executes their contents. A report with both path and digest lets another analyst reproduce the observation without confusing two differently named files that happen to contain the same bytes.

Try an example.sh
sha256_file() {
    if command -v sha256sum >/dev/null 2>&1; then
        value=$(sha256sum < "$1") || return 1
    else
        value=$(shasum -a 256 < "$1") || return 1
    fi
    printf '%s\n' "${value%% *}"
}
printf 'sha256=%s\n' "$(sha256_file './evidence/note.txt')"
YOUR TURN

Put it into practice.

Read evidence/list.txt, whose trusted basenames contain no slash, tab or newline. Write manifest.tsv with header path<TAB>sha256, then evidence/<basename> and the lowercase SHA-256 of each original file, in manifest order. Preserve all original bytes. Missing files must fail nonzero with Missing evidence on stderr. Support both Linux sha256sum and macOS shasum -a 256. Replace the report on repeated runs.

See an explained solution
One way to solve it.sh
sha256_file() {
    if command -v sha256sum >/dev/null 2>&1; then
        value=$(sha256sum < "$1") || return 1
    else
        value=$(shasum -a 256 < "$1") || return 1
    fi
    printf '%s\n' "${value%% *}"
}
printf 'path\tsha256\n' > manifest.tsv
while IFS= read -r name || [ -n "$name" ]; do
    [ -f "./evidence/$name" ] || { printf 'Missing evidence: %s\n' "$name" >&2; exit 1; }
    hash=$(sha256_file "./evidence/$name") || exit 1
    printf 'evidence/%s\t%s\n' "$name" "$hash" >> manifest.tsv
done < evidence/list.txt

The wrapper hashes the input stream without changing its bytes and hides platform-specific command names behind one interface. The manifest keeps the original path beside a lowercase digest and is replaced before a repeat run.