PREP WITH PROOF
Menu
SECURITY & INFRASTRUCTURE / PRACTICAL GUIDE

Verify SHA256 Checksums: Files, Manifests and Trust Limits

Verify files with GNU sha256sum and Python. Diagnose mismatches, missing files, malformed manifests and path errors with a reproducible 50-check local lab.

THE QUICK ANSWER

A SHA256 match shows that the bytes agree with the expected digest; it does not authenticate an unsigned checksum list. Use the right release reference, check the entire command result, resolve relative filenames from the correct directory, and choose missing-file and malformed-line policies deliberately.

Decide what a matching checksum establishes

A download finishes successfully, yet the installer fails. A copied dataset has the expected size but different contents. A deployment job prints “OK” for one file and continues even though another is missing. SHA256 verification can help with these problems, provided you verify the intended bytes against the intended reference and act on the command’s exit status.

A SHA256 digest is a fixed-size fingerprint computed from bytes. The usual hexadecimal representation has 64 characters. A comparison with a trusted expected digest checks consistency with that reference; it does not inspect the file’s behavior, establish its publisher by itself or decide whether an executable is safe to run.

Keep the artifact and reference trust questions separate. If an adversary can replace both a download and the unsigned checksum list beside it, they can publish a matching pair. Use the publisher’s documented authenticated release process, such as verified signed release metadata, when origin matters. Checking a signature also requires an independently trusted verification key and the publisher’s actual instructions.

This guide demonstrates ordinary local file verification with GNU sha256sum and Python 3.12. It uses invented temporary files, not a real software download. The result is a troubleshooting workflow for byte consistency, with explicit boundaries around what the check does not authenticate.

Source: GNU coreutils: SHA-2 checksum utilities ↗

Verify the named artifact and the command result

Begin with the publisher’s documented algorithm and release. Confirm that the expected filename, architecture, version and archive format refer to the artifact you actually downloaded. A valid digest for a different release is not a useful reference. Avoid regenerating the expected value from a suspicious file and then treating the comparison as independent evidence.

For a GNU-format checksum list, run verification from the directory containing the files named by its relative entries. In the example below, SHA256SUMS and package.bin are in the same download directory. The conditional prevents the next stage from treating a failed check as permission to continue. It deliberately does not install or execute the artifact.

Use the exit status as the gate. A visible OK line describes that entry, not necessarily the outcome of every entry in a larger manifest. A missing or unreadable listed file and a mismatching digest can cause verification to fail. Keep the diagnostics when investigating rather than reducing the evidence to a screenshot of the successful line.

When you have a single trusted digest instead of a manifest, compute the file’s SHA256 and compare the entire hexadecimal value, not a short prefix. Confirm which algorithm produced the value: a digest length or a familiar-looking string is not sufficient identification. Preserve the trusted reference separately from the artifact while investigating.

# Run inside the directory holding the intended downloaded files.
# SHA256SUMS must come from the release process you have verified.
if sha256sum --check --strict SHA256SUMS; then
    printf '%s\n' 'Listed checks passed; continue the documented review.'
else
    printf '%s\n' 'Verification failed; stop and inspect diagnostics.' >&2
    exit 1
fi

# Compute a single file digest without changing its contents.
sha256sum -- package.bin

Source: GNU coreutils: verification, strict and missing-file options ↗

Three manifest traps that change the result

First, the manifest’s pathname does not set the working directory for its relative file entries. Passing /downloads/SHA256SUMS from another directory does not automatically make package.bin resolve under /downloads. The fixture demonstrates a successful check from the expected directory and a failed check from an empty sibling directory using the same absolute manifest path.

Second, a malformed line can be skipped by the default GNU checker when other valid lines exist. In our fixture, adding “not a checksum line” leaves the default command successful, while --strict makes the malformed line a failure. This is a syntax rule, not an authenticity check: a well-formed attacker-controlled manifest remains attacker-controlled.

Third, --ignore-missing is a deliberate subset policy. It can be useful when a release manifest lists several artifacts but you downloaded only one. It can also conceal an unexpectedly absent artifact if your job was supposed to verify the complete release. The fixture confirms both a successful present-file subset check and failure when every listed file is missing.

Choose that policy explicitly before adding flags. For a required artifact set, separately confirm that the expected files and expected manifest entries exist. A successful consistency check cannot tell you that an unsigned list was complete. Treat release inventory and checksum comparison as related checks with different evidence.

Source: GNU coreutils: verification, strict and missing-file options ↗

Same size, same visible text and same filename are different claims

The first corruption fixture changes one byte without changing file length. Verification fails even though the byte count remains equal. A second fixture truncates the file and also fails. File size is useful supporting evidence, but it cannot replace a digest when the task is to compare exact content.

Two text files can look the same in an editor and still hash differently. LF and CRLF line endings are different byte sequences. A byte-order mark, a final newline, character encoding or an archive wrapper can also make the downloaded file differ from the reference. Hash the artifact as received before opening it in software that might save a transformed version.

On the GNU/Linux environment tested here, sha256sum --binary and --text produce the same digest for the same file. They do not convert CRLF to LF to make different files agree. The mode marker is not a repair instruction. Keep cross-platform behavior separate and use the release’s documented verification method on your operating system.

Renaming a file does not change its contents, so its byte digest remains the same. A manifest still names a path, however. After a rename, checking an old relative filename can fail because the file is missing rather than corrupted. Read the error before redownloading or modifying a file that may already contain the correct bytes.

Source: GNU coreutils: verification, strict and missing-file options ↗

Hash files in binary mode without loading everything

The reusable streamed function in the listing opens a file with rb, reads fixed-size chunks and feeds each chunk into the same hashlib.sha256 object. Chunk boundaries do not become part of the input. The resulting digest describes the concatenation of bytes read, regardless of how those bytes were divided into reads.

Python also provides hashlib.file_digest for suitable binary file objects. The explicit loop here keeps the chunk behavior visible for the experiment. It is not a faster-hashing claim, and the small fixture is not a benchmark. For a production tool, define expected file sizes, I/O error handling and where the trusted digest comes from.

A hash read is not a filesystem snapshot. If another process modifies a file during the read, a simple loop does not prove that the observed bytes correspond to a stable release artifact. Verify an immutable completed download or an appropriately controlled snapshot, and avoid modifying the file between verification and the operation that consumes it.

Save the complete listing as sha256_lab.py and run python3 sha256_lab.py on Linux with GNU sha256sum installed. It creates its own temporary directory and removes that directory on exit. No downloaded program is executed, no network request is made, and no production file is renamed or removed.

import hashlib
from pathlib import Path
import subprocess
import sys
import tempfile

checks = 0

def check(value, label):
    global checks
    if not value:
        raise AssertionError(label)
    checks += 1

def run(root, *args):
    return subprocess.run(args, cwd=root, capture_output=True, text=True)

def streamed(path, chunk_size=4096):
    if chunk_size <= 0:
        raise ValueError('chunk size must be positive')
    digest = hashlib.sha256()
    with path.open('rb') as handle:
        for block in iter(lambda: handle.read(chunk_size), b''):
            digest.update(block)
    return digest.hexdigest()

with tempfile.TemporaryDirectory(prefix='pwp-sha256-') as directory:
    root = Path(directory)
    artifact = root / 'package.bin'
    original = b'Prep With Proof\x00fixture\xff\n'
    artifact.write_bytes(original)
    digest = streamed(artifact)
    output = run(root, 'sha256sum', '--', 'package.bin')
    check(output.returncode == 0, 'CLI hashing succeeds')
    check(output.stdout.split()[0] == digest, 'Python and GNU agree')
    manifest = root / 'SHA256SUMS'
    manifest.write_text(output.stdout)
    check(run(root, 'sha256sum', '--check', '--strict', 'SHA256SUMS').returncode == 0, 'valid manifest')
    artifact.write_bytes(original[:-1] + b'!')
    check(run(root, 'sha256sum', '--check', 'SHA256SUMS').returncode != 0, 'same-size changed byte rejected')
    check(len(artifact.read_bytes()) == len(original), 'size alone did not catch edit')
    artifact.write_bytes(original[:-1])
    check(run(root, 'sha256sum', '--check', 'SHA256SUMS').returncode != 0, 'truncation rejected')
    artifact.write_bytes(original)
    check(run(root, 'sha256sum', '--check', '--status', 'SHA256SUMS').returncode == 0, 'status success')
    artifact.unlink()
    check(run(root, 'sha256sum', '--check', 'SHA256SUMS').returncode != 0, 'missing file rejected')
    artifact.write_bytes(original)
    check(run(root, 'sha256sum', '--check', str(manifest)).returncode == 0, 'manifest from expected cwd')
    elsewhere = root / 'elsewhere'
    elsewhere.mkdir()
    check(run(elsewhere, 'sha256sum', '--check', str(manifest)).returncode != 0, 'relative entry is not based at manifest directory')
    manifest.write_text(output.stdout + 'not a checksum line\n')
    check(run(root, 'sha256sum', '--check', 'SHA256SUMS').returncode == 0, 'default can skip malformed line')
    check(run(root, 'sha256sum', '--check', '--strict', 'SHA256SUMS').returncode != 0, 'strict rejects malformed line')
    manifest.write_text('not a checksum line\n')
    check(run(root, 'sha256sum', '--check', 'SHA256SUMS').returncode != 0, 'no valid lines rejected')
    manifest.write_text(output.stdout + '0' * 64 + '  optional.bin\n')
    check(run(root, 'sha256sum', '--check', 'SHA256SUMS').returncode != 0, 'missing listed optional file fails')
    check(run(root, 'sha256sum', '--check', '--ignore-missing', 'SHA256SUMS').returncode == 0, 'ignore missing checks present subset')
    artifact.rename(root / 'renamed.bin')
    check(streamed(root / 'renamed.bin') == digest, 'rename does not change bytes digest')
    check(run(root, 'sha256sum', '--check', '--ignore-missing', 'SHA256SUMS').returncode != 0, 'all entries missing still fails')
    (root / 'renamed.bin').rename(artifact)
    spaced = root / 'a file.bin'
    spaced.write_bytes(original)
    spaced_output = run(root, 'sha256sum', '--', spaced.name)
    manifest.write_text(spaced_output.stdout)
    check(run(root, 'sha256sum', '--check', '--strict', 'SHA256SUMS').returncode == 0, 'space-containing filename round trip')
    lf, crlf = root / 'lf.txt', root / 'crlf.txt'
    lf.write_bytes(b'alpha\nbeta\n')
    crlf.write_bytes(b'alpha\r\nbeta\r\n')
    check(streamed(lf) != streamed(crlf), 'line-ending bytes differ')
    check(run(root, 'sha256sum', '--binary', '--', lf.name).stdout.split()[0] == run(root, 'sha256sum', '--text', '--', lf.name).stdout.split()[0], 'Linux text/binary flags do not normalize line endings')
    artifact.write_bytes(b'replaced by someone else\n')
    changed = run(root, 'sha256sum', '--', artifact.name)
    manifest.write_text(changed.stdout)
    check(run(root, 'sha256sum', '--check', '--strict', 'SHA256SUMS').returncode == 0, 'replacing file AND untrusted manifest passes consistency')
    empty = root / 'empty'
    empty.write_bytes(b'')
    check(streamed(empty) == 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'known empty vector')
    abc = root / 'abc'
    abc.write_bytes(b'abc')
    check(streamed(abc) == 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', 'known abc vector')
    for size in [0, 1, 63, 64, 65, 4095, 4096, 4097, 131073]:
        sample = root / 'sample.bin'
        sample.write_bytes(bytes(i % 256 for i in range(size)))
        expected = run(root, 'sha256sum', '--', sample.name).stdout.split()[0]
        for chunk in [1, 64, 4096]:
            check(streamed(sample, chunk) == expected, f'length {size}, chunk {chunk}')
print(f'PASS: {checks} checks; Python {sys.version.split()[0]}')
print(subprocess.run(['sha256sum', '--version'], capture_output=True, text=True, check=True).stdout.splitlines()[0])

Source: Python 3.12: hashlib updates and binary file hashing ↗

Read the experiment as evidence, not a security certification

The exact listing passed 50 checks with Python 3.12.14 and GNU coreutils 9.4 on Linux. Fixed cases cover good manifests, same-size edits, truncation, missing files, working-directory resolution, malformed lines, subset verification, a filename containing spaces, renaming, line endings and a forged matching artifact/manifest pair.

Two fixed known digests anchor the empty-byte and abc cases. Nine deterministic binary lengths exercise empty files, one byte, boundaries around 64 and 4,096 bytes, and a file of 131,073 bytes. Each is hashed with three chunk sizes and compared against GNU sha256sum. That tests streaming equivalence across read boundaries rather than assuming a whole-file read.

The forged-pair case is intentionally successful: after replacing the artifact and recomputing its unsigned manifest, the consistency checker reports success. No cryptographic attack is involved. The reference changed along with the data. This small fixture makes the trust boundary observable without claiming to simulate an actual compromised publisher.

We did not test a signed release, network transfer, concurrent writer, non-Linux text mode or adversarial filesystem. The Python and GNU results are cross-implementation comparisons plus fixed vectors, not an independent implementation of the SHA256 algorithm. The count describes the listed local checks and does not imply exhaustive validation.

Resolve a mismatch without erasing the evidence

When a check fails, record the artifact path, source release, expected digest, computed digest, file size and command exit status. Distinguish an unreadable file or wrong relative path from a successfully read file with different contents. Check whether the transfer is complete and whether a tool transformed the bytes after downloading.

If the reference itself is uncertain, obtain it again through the publisher’s verified release process. If the bytes still disagree, stop the consuming step and investigate the release or transfer. Do not replace the expected digest with your local result simply to make automation green. That changes the question being answered.

For repeatable jobs, gate the next action on the full verification result and preserve diagnostics. Explicitly choose whether missing entries are permitted and whether malformed lines must fail. A reliable checksum workflow is small: identify the release, establish trust in the reference, compare the exact bytes and stop on a result that does not meet the contract.

KEEP THESE THREE THINGS

Your next steps

  1. Verify the intended release against a reference whose origin you have established.
  2. Gate the next step on the command exit status and retain mismatch or read-error diagnostics.
  3. Hash original bytes; distinguish a changed file from a wrong path, transformed text or an untrusted manifest.

Sources & official resources

Linked resources checked on 2026-10-31. Technical behavior and provider guidance can change; check the documentation for the version you use.

About this guide and its recommendations

Published by Prep With Proof with AI-assisted drafting. Study routines and examples are editorial suggestions, not official exam instructions or claims of a learner's personal results. Cited provider facts are linked; ownership of recommended Prep With Proof products is disclosed. No competitor endorsement or guaranteed certification outcome is implied.

Suggest a correction →