Results.

SCEPTRA.TECH

Check a Download SHA-256 With Python Without Uploading the File Anywhere

Original illustration separating local SHA-256 comparison from publisher trust

A large download finishing successfully is not the same as checking its contents. If a publisher supplies a SHA-256 checksum for the exact file you downloaded, you can calculate the file's digest locally and compare the two. You do not need to upload the file to a checksum website.

I would keep two questions separate: does this file match the expected digest, and why should I trust that expected digest? The short Python script below addresses the first question. It does not answer the second.

Get the right expected value first

Find the publisher's verification instructions for your download. Match the release, operating system, architecture and filename. A checksum for a different build is not useful even when the product name looks right.

Use the algorithm named by the publisher. This example calculates SHA-256, not SHA-1, SHA-512 or MD5. Do not compare values from different algorithms or assume a shorter value is an acceptable abbreviation.

Read the file as bytes, in chunks

Save this code as sha256_file.py. It uses Python's standard library and opens the target file for reading, not writing:

from pathlib import Path
import hashlib
import sys

path = Path(sys.argv[1])
digest = hashlib.sha256()
with path.open('rb') as stream:
    for chunk in iter(lambda: stream.read(1024 * 1024), b''):
        digest.update(chunk)
print(digest.hexdigest())

Run it from a terminal, supplying the completed download's path. For example, if your Python 3 command is python:

python sha256_file.py "downloads/example.zip"

Replace the example path with your actual file. Relative paths are resolved from the terminal's current working directory. Your installation may use python3 instead; use the command that launches Python 3 on your machine.

The rb mode reads raw bytes without text decoding or newline conversion. The loop feeds at most one MiB per read into the same hash object. Python documents that repeated update() calls hash the concatenation of those chunks. The script does not need to load the entire download into memory.

Compare the complete digest

The output is the hexadecimal digest of the file's bytes. Compare the full value with the publisher's SHA-256 value for that exact artifact. Do not accidentally include a filename or a checksum-list marker in the expected value.

I checked this script with a three-byte file containing abc, a version with an added newline, and a file larger than two MiB. The chunked result matched a direct SHA-256 calculation, and adding the newline changed the digest. Those are small local checks, not a verification of any particular software download.

If the values differ, do not run or install the file while explaining away the mismatch. Recheck the file selection and release details, confirm that the download is complete, and follow the publisher's recovery instructions. Avoid changing or editing the file just to make a comparison pass.

A match is not an authenticity certificate

A malicious distributor could provide a modified file and a matching digest together. A checksum from that same untrusted source does not establish who produced the file. A matching digest is also not a malware scan or a guarantee that software is safe to run.

Some publishers authenticate their checksum lists with signatures. Ubuntu, for example, documents verifying the signing key and signed checksum list before checking its installation image. If your publisher provides that workflow, follow it. This Python script does not verify signatures, keys or publisher identity.

Finally, hash a stable, completed file rather than one another program is still writing. Keep the computed value, the exact filename and the trusted verification reference together. That makes your conclusion precise: this file matched this expected SHA-256 value, under these checks.

Sources

Comments