Checksums and differences
On this page
Two copies of a note look similar, but one has been edited. You need evidence that their bytes differ and a readable explanation of the edit. By the end, you can use a checksum for the first question and diff for the second.
Before you start
Section titled “Before you start”Finish Creating and inspecting archives and its reset. Use ~/lab from the practice directory setup. Reserve the absent directory ~/lab/b15-compare.
Keep an original, then compare the bytes
Section titled “Keep an original, then compare the bytes”Prepare two copies of a short note:
mkdir ~/lab/b15-comparecd ~/lab/b15-compareIn your editor, create before.txt with these contents and a final newline:
Project notesstatus=draftSave and exit, then copy it and compute both digests:
cp before.txt after.txtsha256sum before.txt after.txtA checksum, here a SHA-256 digest, is a fixed-length value computed from a file’s bytes. Sha256sum prints the complete digest followed by the filename. The two filenames differ, but the bytes match, so their digests match. The pathname is not part of the file’s digest.
Edit one line and hash again
Section titled “Edit one line and hash again”Open after.txt in your editor. Change only the second line and keep its final newline:
Project notesstatus=readyThe recording uses printf to write these same versions. Compare them again:
sha256sum before.txt after.txtdiff before.txt after.txtecho $?Predict: will the digest identify the changed line number?
Run it, then compare with your guess
learner@rootvolt:~/lab$ mkdir ~/lab/b15-comparelearner@rootvolt:~/lab$ cd ~/lab/b15-comparelearner@rootvolt:~/lab/b15-compare$ printf 'Project notes\nstatus=draft\n' > before.txtlearner@rootvolt:~/lab/b15-compare$ cp before.txt after.txtlearner@rootvolt:~/lab/b15-compare$ sha256sum before.txt after.txt684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 before.txt
684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 after.txtlearner@rootvolt:~/lab/b15-compare$ printf '%s\n' 'Project notes' status=ready > after.txtlearner@rootvolt:~/lab/b15-compare$ sha256sum before.txt after.txt684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 before.txt
f23b781fbbc1873bb58a8f90752522272a62f449df2ff5c80e1e1c417270dff9 after.txtlearner@rootvolt:~/lab/b15-compare$ diff before.txt after.txt2c2
< status=draft
---
> status=readylearner@rootvolt:~/lab/b15-compare$ echo $?1learner@rootvolt:~/lab/b15-compare$ rm before.txt after.txtlearner@rootvolt:~/lab/b15-compare$ cd ~/lablearner@rootvolt:~/lab$ rmdir ~/lab/b15-comparelearner@rootvolt:~/lab$ test ! -e ~/lab/b15-compare && printf 'Reset complete\n'Reset completelearner@rootvolt:~/lab$ Steps (31)
- Type
mkdir ~/lab/b15-compare - Press Enter — Reserve a directory for the two text versions
- Type
cd ~/lab/b15-compare - Press Enter — Work where both versions can be named explicitly
- Type
printf 'Project notes\nstatus=draft\n' > before.txt - Press Enter — Write the original two-line note
- Type
cp before.txt after.txt - Press Enter — Keep an unchanged original for comparison
- Type
sha256sum before.txt after.txt - Press Enter — Compute both complete digests before editing
- Output:
684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 before.txt 684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 after.txt - Type
printf '%s\n' 'Project notes' status=ready > after.txt - Press Enter — Replace only the status line in the edited copy
- Type
sha256sum before.txt after.txt - Press Enter — Compare the unchanged original with the edited copy
- Output:
684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 before.txt f23b781fbbc1873bb58a8f90752522272a62f449df2ff5c80e1e1c417270dff9 after.txt - Type
diff before.txt after.txt - Press Enter — Read which text line changed
- Output:
2c2 < status=draft --- > status=ready - Type
echo $? - Press Enter — A difference is status 1 rather than a command failure
- Output:
1 - Type
rm before.txt after.txt - Press Enter — Remove both disposable versions
- Type
cd ~/lab - Press Enter — Leave the comparison directory
- Type
rmdir ~/lab/b15-compare - Press Enter — Remove the empty comparison directory
- Type
test ! -e ~/lab/b15-compare && printf 'Reset complete\n' - Press Enter — Confirm neither version remains
- Output:
Reset complete
No. The changed digest shows that the bytes differ, without locating the edit. Diff supplies the line-by-line explanation. The digest and changed diff lines are highlighted in the replay.
The full captured values are repeated here so wrapped checksum rows remain readable:
| Version | SHA-256 digest |
|---|---|
before.txt, unchanged |
684b86c798665be311d3e36704af4b87aaa694d8049914be148952e6627dedb4 |
after.txt, edited |
f23b781fbbc1873bb58a8f90752522272a62f449df2ff5c80e1e1c417270dff9 |
Compare every character when checking digests. A matching beginning is not a matching digest. With exactly these bytes, including newlines, your values match the recording.
Every input byte matters: a changed space, newline style, or final newline changes the digest in practical checks. A different digest proves the files differ. Matching SHA-256 digests give strong evidence of identical bytes, though a finite digest cannot mathematically rule out every collision. For a direct byte-by-byte comparison of two local files, use cmp.
Read the text edit
Section titled “Read the text edit”In diff’s normal format, 2c2 says line 2 in the first file changes to line 2 in the second. The line beginning < comes from before.txt; the line beginning > comes from after.txt. The separator is not part of either file. Reversing the argument order reverses which version those markers describe.
The changed lines are status=draft and status=ready. Diff returned status 1 because it found differences. Status 0 means no differences; status 2 indicates trouble comparing the inputs. Echo must immediately follow diff to read its status rather than another command’s status.
Diff is useful for readable text. For an archive or an image, a digest or cmp can test byte identity without pretending there is a useful line-by-line edit.
Security Angle
Section titled “Security Angle”A checksum is useful only in relation to an expected value. If an attacker can replace both a download and the checksum beside it, a match does not prove who published it. Use a trusted source for the expected digest and verify signatures when the publisher provides them. MD5 appears in older checksum lists, but it is unsuitable for checking authenticity against deliberate tampering; prefer SHA-256 for these exercises.
Reset this lesson
Section titled “Reset this lesson”The replay ends with:
rm before.txt after.txtcd ~/labrmdir ~/lab/b15-comparetest ! -e ~/lab/b15-compare && printf 'Reset complete\n'Mistakes to recognize
Section titled “Mistakes to recognize”Treating a checksum as a description of the edit. Its characters do not encode a line number. Use diff for readable text changes.
Expecting matching appearance to mean matching bytes. Spaces and newline conventions can differ without an obvious visual change. Compare the complete digests.
Try it yourself
Section titled “Try it yourself”Goal: Decide whether renaming after.txt without changing its contents should change its SHA-256 digest. Explain which part of sha256sum’s displayed row changes.
Hint
The command reads file contents, then labels the result with a name.
Solution
The digest stays the same. The printed pathname changes. A renamed copy has the same bytes; editing its contents is a different operation.
- SHA-256 summarizes bytes, including invisible text differences.
- A different digest proves a difference without locating it.
- Diff identifies text edits and uses status 1 for differences.
Next, Putting the workflow together prepares and checks a project handoff.
Further reading
Section titled “Further reading”- On Ubuntu:
man sha256sum,man diff, andman cmp. - GNU Coreutils SHA-2 utilities.
- GNU Diffutils manual.