Beginner · Systems and cryptography · topic 13 of 43 · 30 min
Hashing: fingerprints for data
Explanation
Why hashes are everywhere in security
Imagine you download a 2 GB installer. How do you know nobody changed it on the way? Comparing 2 GB byte by byte is slow, and you would need the original. Instead, the publisher shows a short fingerprint of the file, and you compute the same fingerprint on your copy. If they match, the file is the same. That fingerprint is a hash.
What a hash function does
A cryptographic hash function takes input of any size and gives output of a fixed size, called the hash or digest. A good one has these properties:
- Deterministic: the same input always gives the same hash.
- Avalanche effect: changing one letter changes the hash completely.
- One-way: you cannot work backwards from the hash to the input.
- Collision-resistant: it is practically impossible to find two different inputs with the same hash.
import hashlib
print(hashlib.sha256(b"hello").hexdigest())
print(hashlib.sha256(b"Hello").hexdigest())
print(hashlib.sha256("namaste".encode()).hexdigest())Hashing is NOT encryption. Encryption is two-way: with the key you get the data back. A hash is one-way: there is no key and no way back.
Common algorithms
| Algorithm | Output size | Status |
|---|---|---|
| MD5 | 128 bits (32 hex characters) | Broken for security: collisions can be made. Still seen as a file ID in old tools |
| SHA-1 | 160 bits (40 hex) | Broken for collisions; being retired |
| SHA-256 (SHA-2 family) | 256 bits (64 hex) | Standard choice today |
| SHA-3 | Various | Newer standard, also safe |
Use 1: checking a file has not changed
$ sha256sum ubuntu-24.04-desktop-amd64.iso
<64 hex characters> ubuntu-24.04-desktop-amd64.isoPS> Get-FileHash .\setup.exe -Algorithm SHA256
Algorithm Hash Path
--------- ---- ----
SHA256 <64 hex characters> C:\Downloads\setup.exeFile integrity monitoring (FIM) tools hash important system files and alert when a hash changes unexpectedly.
Use 2: identifying malware
Threat reports list the SHA-256 hashes of known malicious files. Security tools compare the hashes of files on your computers against these lists. This catches known files fast, but an attacker who changes one byte gets a new hash, so hashes are only one layer of detection.
Use 3: storing passwords
Websites should never store your actual password. They store a hash, and when you log in they hash what you typed and compare. But a plain fast hash is not enough: attackers who steal the database can try billions of guesses per second, and two people with the same password would have the same hash.
- Salt: a random value, different for each user, added before hashing. Same passwords then give different hashes, and pre-computed tables of hashes (rainbow tables) become useless.
- Slow algorithms: bcrypt, scrypt, Argon2id or PBKDF2 with many iterations are designed to be slow, so each guess costs the attacker much more time.
import hashlib, os
salt = os.urandom(16)
key = hashlib.pbkdf2_hmac("sha256", b"chai monsoon pakoda", salt, 600_000)
print(salt.hex(), key.hex())Common mistakes
- Using MD5 or plain SHA-256 to store passwords. Use Argon2id, bcrypt, scrypt or PBKDF2 with a salt.
- Calling hashing "encryption" in a report. It confuses everyone.
- Comparing a hash from the same untrusted website you downloaded the file from. Get the hash from the official source.
- Forgetting to encode text to bytes in Python: hashlib needs bytes, so use "text".encode().
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Hashing: fingerprints for data · 1/7
A hash is a short fingerprint of data. Same input, same hash. Tiny change, totally different hash.
Prefer a video by a teacher? Find free videos on “hashing explained sha256 salt password hashing for beginners” on YouTube ↗
In real life: Checking a government utility download
A tax practitioner downloads an offline utility for filing returns. Before running it, she computes its SHA-256 hash with Get-FileHash and compares it with the value on the official download page. They match, so the file was not changed on the way. A colleague who downloaded "the same" utility from a forwarded link gets a different hash: that copy was tampered with and is deleted.
Practice (0/3 done)
Write sha256_hex(text) that returns the SHA-256 hash of the text (encoded as UTF-8) as a hex string, using hashlib.
File integrity check. baseline and current are dicts of file path → SHA-256 hex. Write changed_files(baseline, current) that returns a dict with three sorted lists: "modified" (in both, hash differs), "added" (only in current) and "removed" (only in baseline).
Write hash_password(password, salt) returning hashlib.pbkdf2_hmac("sha256", password bytes, salt, 1000).hex() (1000 iterations only to keep this exercise fast; real systems use far more). Then write check_password(password, salt, stored) that returns True if it matches, comparing with hmac.compare_digest.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Symmetric and asymmetric encryptionSystems and cryptography · same module
- Storing passwords safelyBackend Development · Authentication and authorization
- Password hashing and cracking (your own hashes only)Ethical Hacking and Penetration Testing (legal labs) · Deeper lab skills: passwords, privilege, Metasploit, wireless
- How defenders detect malwareMalware and web security
- Hash maps and hash setsData Structures and Algorithms (DSA) for interviews · Hashing, two pointers and sliding window
- Project: password strength checkerPython Programming · Projects: put it all together
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
How long is a SHA-256 hash written in hex?
Answer: 64 characters
256 bits = 32 bytes = 64 hex characters, whatever the input size.
Why is a salt added before hashing passwords?
Answer: So identical passwords get different hashes and pre-computed tables fail
A unique random salt per user defeats rainbow tables and hides reused passwords.
Which is best for storing passwords?
Answer: Argon2id or bcrypt
Password hashing must be slow and salted; Argon2id and bcrypt are designed for that.
You change one letter in a document. Its SHA-256 hash…
Answer: Changes completely
The avalanche effect: any change gives a completely different hash.
Why can attackers avoid hash-based malware detection?
Answer: Changing even one byte of the file gives a new hash
Hash matching only catches exact known files, so it is one layer among several.
Why is plain SHA-256 a bad password hash?Related
Answer: It is too fast, so attackers can try billions of guesses per second
Password hashes must be slow. Fast hashes let attackers test huge password lists quickly.
Cracking a hash means…Related
Answer: Hashing guesses until one matches
You cannot reverse a hash; you guess and compare.
What is a weakness of hash-based detection?Related
Answer: Any small change to the file gives a new hash
Hashes match exact files only.
In a hash table, what is a collision?Related
Answer: Two different keys that land in the same bucket
More possible keys than buckets means some keys must share a bucket; chaining or open addressing handles it.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Systems and cryptography” before its exam: Symmetric and asymmetric encryption, Certificates and PKI.
- Module 3 exam: Systems and cryptography · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Identity and network defence. 43 topics left in the course before the final exam.