Hashing: a fingerprint is not security
What a hash is, how to compute md5 and sha256 with the shell and Python, what a fingerprint can prove, and why md5 is not protection.
What a hash is, how to compute md5 and sha256 with the shell and Python, what a fingerprint can prove, and why md5 is not protection.
In plain words
A hash is a fingerprint for data. Give it a word or a whole file and it gives back a short code. The same input always gives the same code, and changing one letter gives a completely different one. You cannot rebuild the file from the code.
Try it yourself
Try it in the terminal. -n stops echo from adding a line break, which would change the fingerprint:
echo -n hello | md5sum
echo -n hellp | md5sum
echo -n hello | sha256sum
The first line should read 5d41402abc4b2a76b9719d911017c592. Same thing in Python, and for a whole file:
python3 -c 'import hashlib; print(hashlib.md5(b"hello").hexdigest())'
md5sum /etc/os-release
Change one letter in app.py, save, and run md5sum again.
A step further
A fingerprint proves that a file arrived unchanged: compare the code you were given with the one you computed. That is its job.
It is not security. md5 is old and broken: people can make two different files with the same md5, so it must never protect anything. For checks use sha256. Passwords are never stored as a plain hash of any kind: they need a slow, salted method (bcrypt, argon2).
So an md5 field in an API answer is a fingerprint for the client to check, not protection.
How it works in real projects
Software downloads publish a sha256 next to the file. Git itself names every commit by a hash. Password systems use slow salted hashes so a stolen database is hard to use.