Skip to content

Beginner · Systems and cryptography · topic 13 of 43 · 30 min

Hashing: fingerprints for data

Explanation

A hash function turns any data into a short, fixed-length fingerprint. The same input always gives the same hash, a tiny change gives a completely different hash, and you cannot turn the hash back into the data. Defenders use hashes to check files have not changed, to identify known malware, and to store passwords safely (with a salt and a slow algorithm).

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.
python
import hashlib

print(hashlib.sha256(b"hello").hexdigest())
print(hashlib.sha256(b"Hello").hexdigest())
print(hashlib.sha256("namaste".encode()).hexdigest())
One capital letter changes everything. Shows 2cf24dba5f…9824, then 185f8db322…1969, then 8e9177ca98…d8f2. hexdigest() gives the hash as 64 hex characters.

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

AlgorithmOutput sizeStatus
MD5128 bits (32 hex characters)Broken for security: collisions can be made. Still seen as a file ID in old tools
SHA-1160 bits (40 hex)Broken for collisions; being retired
SHA-256 (SHA-2 family)256 bits (64 hex)Standard choice today
SHA-3VariousNewer standard, also safe

Use 1: checking a file has not changed

bash
$ sha256sum ubuntu-24.04-desktop-amd64.iso
<64 hex characters>  ubuntu-24.04-desktop-amd64.iso
Linux: compare the result with the hash published on the official download page.
powershell
PS> Get-FileHash .\setup.exe -Algorithm SHA256

Algorithm Hash                                                             Path
--------- ----                                                             ----
SHA256    <64 hex characters>                                              C:\Downloads\setup.exe
Windows PowerShell does the same with Get-FileHash.

File 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.
python
import hashlib, os

salt = os.urandom(16)
key = hashlib.pbkdf2_hmac("sha256", b"chai monsoon pakoda", salt, 600_000)
print(salt.hex(), key.hex())
PBKDF2 with a random salt and 600,000 iterations (the OWASP suggestion for PBKDF2-HMAC-SHA256). Store the salt and the result, never the password.

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

Data → fixed-size fingerprint

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)

Task 1 · Code · Python

Write sha256_hex(text) that returns the SHA-256 hash of the text (encoded as UTF-8) as a hex string, using hashlib.

Task 2 · Code · Python

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).

Task 3 · Code · Python

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

Question 1

1.How long is a SHA-256 hash written in hex?

Question 2

2.Why is a salt added before hashing passwords?

Question 3

3.Which is best for storing passwords?

Question 4

4.You change one letter in a document. Its SHA-256 hash…

Question 5

5.Why can attackers avoid hash-based malware detection?

Read more (free)

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.

From Storing passwords safely

Cracking a hash means…Related

Answer: Hashing guesses until one matches

You cannot reverse a hash; you guess and compare.

From Password hashing and cracking (your own hashes only)

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.

From How defenders detect malware

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.

From Hash maps and hash sets

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

  1. 2 more topics in “Systems and cryptography” before its exam: Symmetric and asymmetric encryption, Certificates and PKI.
  2. Module 3 exam: Systems and cryptography · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Identity and network defence. 43 topics left in the course before the final exam.

← Previous: Windows security basics