A corrupted log file. Thousands of lines of noise. One hidden security sequence to recover. This is the setup for a CTF challenge that taught me more about how hashing actually works than any textbook.
The Setup
You're given a file of terminal logs. Your job is to extract a hidden security sequence by following five strict rules:
The Trap That Got Me
My first attempt used .strip() before hashing — completely standard practice when reading files. It seemed harmless. It broke everything.
MD5('LOGDATA-X4-TERMINAL\n') and MD5('LOGDATA-X4-TERMINAL') produce entirely different hashes.
That missing newline changes every hash, which changes which lines are classified as "mined" vs "hollow", which changes every character extracted. The entire sequence was wrong, and the error was invisible until I understood what hashing actually is: a contract. Both parties must agree byte-for-byte on what is being hashed.
The Solution
import hashlib
def decode_ledger(file_path):
sequence = []
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
if line.startswith("SYSTEM") or line.startswith("#"):
continue
# Hash the line WITH its newline — do not strip
hash_hex = hashlib.md5(line.encode('utf-8')).hexdigest()
last_char = hash_hex[-1]
if last_char.isdigit():
if len(line) > 6:
sequence.append(line[6])
final_flag = "".join(sequence)
print(f"--- RECOVERY COMPLETE ---")
print(f"SECURITY SEQUENCE: {final_flag}")
except FileNotFoundError:
print("Error: ledger_data.txt not found.")
if __name__ == "__main__":
decode_ledger('ledger_data.txt')Three things to notice:
Why This Matters Beyond CTFs
MD5 hashing underpins a lot of real systems — file integrity verification, password storage, log auditing. A single byte difference produces a completely different hash. That property is the point. It means you can detect any modification, no matter how small.
The takeaway I bring to production code: whenever you're hashing something, be explicit about exactly what bytes you're hashing. Document it. The next engineer (or you, six months later) will thank you.