What a checksum proves
A cryptographic hash such as SHA-256 maps any input to a fixed-length output of 256 bits, written as 64 hexadecimal digits. The mapping is designed so that two different files almost never produce the same output, and so that you cannot build a file with a chosen output. A matching digest is therefore strong evidence that the bytes are the same. It says nothing about whether the bytes are good, only that they are the bytes whose digest you recorded.
That distinction matters. A checksum stored next to a file on the same disk can be replaced along with the file. A checksum from a trusted source, such as a vendor publishing it on a separate page, is useful because the attacker would need to change both places.
Computing and reading a digest
sha256sum reads each file and prints its digest followed by the name. The format has two spaces between the digest and the name, and a dash in place of the name means standard input. Use printf rather than echo when you need exact bytes, because echo may or may not add a newline depending on the shell.
echo hello > msg.txt
sha256sum msg.txt
printf 'hello\n' | sha256sumThe first and last lines print the same digest, because echo hello writes the bytes hello followed by one newline, and the printf form writes the same bytes. The digest is a property of the bytes, not of how you produced them.
Saving and verifying a manifest
Store the digest in a file and check it later with -c. The check reads each named file, computes its digest, and prints OK or FAILED for each line. The command exits with a nonzero status if any file fails, which makes it usable in scripts.
sha256sum msg.txt > msg.sha256
sha256sum -c msg.sha256The second command prints msg.txt: OK. The file name in the manifest is the one used at check time, so run the check from the same directory where you created the manifest, or use paths that resolve the same way.
Detecting a change
To see the check fail, change the file and check again. Appending a single line is enough. The digest changes, and sha256sum -c reports a mismatch with the word FAILED and a nonzero status.
echo tampered >> msg.txt
sha256sum -c msg.sha256 || echo "integrity check failed"The || runs the message only when the check fails. The pattern of a check followed by an action on failure is the basis of most backup and deployment scripts. Keep it in mind for the module on exit codes that comes later.
Do not use sha256sum -c with a manifest you received from an untrusted copy of the same file and then assume the data is safe. The manifest and the file could both have been replaced. Compare against a digest that came through a separate, trusted channel.
Digesting a whole tree
A manifest for a directory is a list of digests, one per file. Shell globbing produces the file list, and sha256sum takes the list as arguments. Redirecting the output into a file gives you a manifest you can verify later.
mkdir -p bundle
printf 'x\n' > bundle/x
printf 'y\n' > bundle/y
sha256sum bundle/* > bundle.sha256
sha256sum -c bundle.sha256The manifest now has one line for each file in bundle. Verifying it prints one OK line per file. If a file were missing, the check would report it, which is how a manifest protects a backup from silent gaps.
Checksums are not signatures
A digest proves that two sets of bytes are identical. It does not prove who produced them. A signature adds a secret key that only the publisher holds, and verification checks the signature against a published public key. Projects that distribute software usually publish both a digest and a signature, and the signature is the stronger guarantee. A digest alone is enough to catch accidental damage, such as a truncated download, and to confirm that a copy matches its original.
The distinction tells you what to trust. A checksum on the same page as a download protects against transfer errors, but it cannot protect against a page that was changed along with the checksum. A signature made with a key you already trust protects against both, provided the key itself arrived by a reliable route.
Partial damage and what it looks like
Damaged data is not always a whole file that changed. A disk that returns bad blocks can leave a file of the right length with a few wrong bytes in the middle, and a copy that stopped early leaves a file shorter than expected. A digest catches both cases, because it covers every byte. A size check catches only the second, which is why size checks are a weak substitute for a digest.
When one line of a manifest fails, do not assume the others are fine. Run the check on the whole manifest, read every line of the output, and compare the failing file with a known good copy. The check tells you which file failed. Only a comparison tells you how it failed. Keep the damaged copy until you have decided what went wrong, because checking the same damaged file again tells you nothing new.
Hands-on: fingerprint and detect
Create a file, record its digest, change it, and capture the failure in a file. The point of the exercise is the workflow: record a fingerprint when the data is known good, and check the fingerprint whenever the data is used again.
echo original > record.txt
sha256sum record.txt > record.sha256
echo changed >> record.txt
sha256sum -c record.sha256 > verify.txt 2>&1 || true
cat verify.txtThe output in verify.txt contains the word FAILED. The redirection with || true keeps the command from ending early, so the report is saved even though the check failed on purpose.