Cyberlium

Python › Module 4 › Lesson 3

BeginnerModule 4Lesson 3/4

Lab — File Integrity Hash Check

Hash a file with hashlib and compare digests to detect unexpected changes

25 min+21 XP3 quiz
Module progress3 of 4

Opening

Lab: SHA-256 fingerprints YOUR files in cyberlium-lab. If the digest changes, the bytes changed — this is integrity, not cracking.

A hash is a fixed-length fingerprint of bytes. hashlib.sha256() updates with chunks of a file you open in binary, then hexdigest() prints 64 hex characters. Same bytes, same digest, every time, on every honest machine. Change one character in the file, hash again, and the digest looks unrelated — avalanche. Defenders use that to notice unexpected edits: a lab script that grew a backdoor, a download that does not match a vendor’s published SHA-256, a notes file that another account touched. You will hash two files, or the same file before and after an edit, inside $HOME/cyberlium-lab. This is not password cracking. You will not hash password lists. You will not download rainbow tables. You will not try to invert SHA-256. You will not point the script at /etc/shadow. You will not “check” a classmate’s files. Integrity of YOUR bytes, chmod 600 on the notes that store the hex (hex of a lab file is not a password, but the folder still holds other secrets). Next is the topic final quiz, then Topic 8 Cryptography Basics — hashes will show up again as building blocks, still not as a cracker.

1. What hashlib is doing: bytes in, 256-bit digest out, compare as strings

open(path, "rb") is required. Text mode would rewrite newlines on some platforms and change the digest. Read in chunks (8192 bytes is a teaching size) so a large ISO does not need to fit in RAM: h.update(chunk) in a loop until the empty bytes sentinel. You do not hash the filename. You do not hash a path string and call it file integrity. You hash the contents. hexdigest() is the usual printable form; digest() is raw bytes if you were storing packed — you will print hex. Comparison is string equality of two hexes you recorded, or hashing file A and file B and printing MATCH / DIFFER. Do not implement your own SHA-256. Do not use md5 for this lab as a “shortcut” — MD5 is still seen in old checksums, but the teaching primitive here is SHA-256.

A matching digest means the bytes you hashed now equal the bytes you hashed then (or equal the other file). It does not mean the file is safe, signed, or from a vendor. Malware can be hashed too; the digest would just be stable. Vendor comparison only works if you fetched the published hash from a channel you already trust (the vendor’s site you typed), not from a forum next to a cracked download. This lab does not require a vendor file. It requires a file you wrote, an edit, and two hexes that differ. If they do not differ after a real edit, you hashed the wrong path or you did not save the edit.

2. Integrity versus cracking: no rainbow tables, no shadow files, no password lists

Password cracking tries guesses through a hash function to match a stolen password hash. Rainbow tables are precomputed guesses. That is a different problem, a different ethics line, and out of this course. hashlib in this lab is the same library a cracker might call, which is why the target file is the control: sample_auth.log, integrity-a.txt, a script you wrote — YOUR files. /etc/shadow, dumped SAM files, “rockyou,” and online hash databases are not lab inputs. If a tutorial tells you to crack NTLM with Python this week, that tutorial is not this lesson. Topic 8 will talk about hashes as crypto primitives without turning you into a cracking shop.

Also out: hashing other people’s documents to “see if they leaked,” scraping paste sites for hashes to attack, and shipping a recursive hasher over a directory you do not own. You may hash every file in cyberlium-lab because you created that tree. chmod 600 the notes that list paths and digests so a shared account does not silently copy your inventory. Empty notes fail. Notes that contain password guesses or shadow lines fail ethics even if SHA-256 ran.

3. Wrong vs right: cracking kits vs before/after SHA-256 on YOUR lab files

Worked failure — same hashlib.sha256, opposite input. Right never includes rainbow tables or live password hashes.

  • Wrong

    Hash /etc/shadow or a dumped password database. Download rainbow tables. Loop secrets.choice into SHA-256 to attack a login. Compare against an online crack service. Call the lab “complete” only if you recovered a plaintext. Hash a coworker’s disk. This course forbids all of that.

  • Right

    SHA-256 two files you own, or one file before and after an edit, in $HOME/cyberlium-lab. Record both hexes and MATCH/DIFFER in hash-lab-notes.txt. chmod 600. Explain avalanche in your own words. Next: Quiz — Python for Security Final, then Cryptography Basics (Topic 8).

4. Hands-on: hash_file.py, edit, hash again, lock the notes

Create integrity-demo.txt, hash it, append a line, hash it again. Optionally hash sample_auth.log as a second file so you see two different digests for two different byte strings. Fill the notes. Do not add a password list to make it “more security.”

Command guide

hash_file.py — SHA-256 of YOUR bytes, chunked, hexdigest

INTEGRITY lab on YOUR files in cyberlium-lab. NOT password cracking. NOT rainbow tables. NOT /etc/shadow.

Command — copy this

mkdir -p "$HOME/cyberlium-lab"
cd "$HOME/cyberlium-lab"

Command — copy this

cat > hash_file.py << 'PY'
import hashlib
import sys

def sha256_file(path):
    h = hashlib.sha256()
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(8192), b""):
            h.update(chunk)
    return h.hexdigest()

path = sys.argv[1] if len(sys.argv) > 1 else "integrity-demo.txt"
print(path)
print(sha256_file(path))
PY

Command — copy this

printf '%s
' 'cyberlium integrity demo — before edit' > integrity-demo.txt
python3 hash_file.py integrity-demo.txt

Record digest_before, then:

Command — copy this

printf '%s
' 'cyberlium integrity demo — AFTER edit' >> integrity-demo.txt
python3 hash_file.py integrity-demo.txt

Record digest_after — should DIFFER.

Optional second file (reuse fiction; still not prod):

Command — copy this

python3 hash_file.py sample_auth.log 2>/dev/null || true

Command guide

hash-lab-notes.txt then chmod 600 — hexes of YOUR files only

Command — copy this

NOTES="$HOME/cyberlium-lab/hash-lab-notes.txt"
{
  echo "=== FILE INTEGRITY HASH LAB ==="
  echo "primitive: hashlib.sha256 on rb chunks — NOT a cracker"
  echo "file: $HOME/cyberlium-lab/integrity-demo.txt"
  echo "digest_before:"
  echo "digest_after:"
  echo "compare: DIFFER (expected after edit) / MATCH"
  echo "optional_second_file_digest:"
  echo "avalanche: one-byte change -> unrelated hex"
  echo "ethics: my files only; no shadow; no rainbow tables; no password lists"
} > "$NOTES"

Command — copy this

chmod 600 "$HOME/cyberlium-lab/hash_file.py" \
          "$HOME/cyberlium-lab/integrity-demo.txt" \
          "$NOTES"

Windows without chmod: WSL/Git Bash, or restrict files in your profile.

NEVER: python hash_file.py /etc/shadow NEVER: curl a rainbow table NEVER: hash password guesses against a stolen dump

Mission: before/after SHA-256 in hash-lab-notes.txt (mode 600)

Hash a file you own in $HOME/cyberlium-lab with hashlib.sha256. Change the file (or hash a second file). Confirm the digests differ when bytes differ. Write both hexes, MATCH/DIFFER, and the ethics line (integrity not cracking) to hash-lab-notes.txt. chmod 600 the script, demo file, and notes. Do not hash password dumps. Do not use rainbow tables.

Stuck? Ask Cyberlium AI Mentor

If “hashing is how you crack passwords” still feels like this lab, ask for a hint — not a table. Try: "Hint only: why open rb and update chunks, why one-byte edits avalanche SHA-256, and why /etc/shadow and rainbow tables are out of scope for integrity-demo.txt?" You still fill the notes. No cracking, no stolen dumps.

You used hashlib as a defender: fingerprint YOUR files, notice change, lock the notes. Matching hex is equality of bytes, not a safety certificate. Cracking kits stay out. Next — Quiz — Python for Security Final — twelve APPLY items across the whole topic (the subtitle still says 10-question), then Cryptography Basics (Topic 8).

Knowledge Check

1

APPLY: hash_file.py prints a 64-character hex for integrity-demo.txt. You append one line and hash again. What happened, and what is this lab not?

Multiple choice

Knowledge Check

2

APPLY: True or False: Changing one byte usually changes the SHA-256 digest drastically, and that property means you should download rainbow tables to “test” integrity-demo.txt.

True or False

Knowledge Check

3

APPLY: Two files in cyberlium-lab have different SHA-256 hexes. A classmate wants to hash /etc/shadow “for a third data point.” Correct pair?

Multiple choice

← Previous

Answer all 3 knowledge checks to continue. (0/3 answered)