Cryptographic Hash Functions
Use a SHA-256 hash value to check that a file is unchanged, and decide whether a task needs hashing or encryption.
A fingerprint for any file
Rathi Softech emails Neha Kulkarni a link to MedLedger update 4.3, and next to the download link its website prints a strange 64-character code. Before installing the update on fourteen billing laptops, Neha runs a small tool on the downloaded file that produces its own 64-character code, and checks that the two codes match exactly. If even one character differed, she would know the file she has is not the file Rathi Softech published. That code is a hash value, and the tool that made it is a hash function. The whole idea is called hashing: turning any piece of data, from one word to a two-hour film, into a short fixed-length code that acts as its fingerprint.
Whatever goes in, one page or a thousand, the same-sized fingerprint comes out. And like a mixer-grinder, the process only runs one way.
Think of how a fingerprint works for people. It is tiny compared with the person, it is the same every time you take it, and two different people practically never share one. A hash value does the same for data. It does not contain the file and you cannot read the contract from it, but it identifies the contract so precisely that any change, even a comma, gives a different fingerprint.
The properties that make a hash useful
Not every way of shrinking data into a short code is good enough for security. A cryptographic hash function is one designed so that it is safe to rely on even when someone is actively trying to fool it. Six properties matter.
Integrity checks rely on the avalanche and no-collision properties: a changed file cannot keep its old fingerprint.
The third property, the avalanche effect, is what lets Neha trust her check. A small change in the input does not make a small change in the output; it scrambles the whole value, so a tampered file cannot quietly produce a "nearly matching" fingerprint. The fifth property, collision resistance, means nobody can craft a second, different file that happens to share the first file's fingerprint. A collision is exactly that: two different inputs with the same hash value. With a good hash function, finding one would take longer than the age of the universe, even with enormous computing power.
One way only, like a mixer-grinder
A one-way function is easy to run forwards and practically impossible to run backwards. Your mixer-grinder at home is a good picture. Put in coconut, green chillies, coriander and a little salt, and in thirty seconds you have chutney. Now try to get the whole coconut and the separate chillies back out of the chutney. No setting on the grinder does that.
A hash function is the same. Computing the fingerprint of a file takes a fraction of a second, but there is no "reverse" button that turns the fingerprint back into the file. The only way to find what produced a given hash value is to guess an input, hash it, and see whether it matches, then guess again. For a large file, or any long and unpredictable input, the number of possible guesses is so vast that this never succeeds. Short, predictable inputs such as common passwords are a different story, which is why passwords need extra protection on top of plain hashing.
SHA-256, the everyday standard
Many hash functions exist, and the one you will meet most often is SHA-256, short for Secure Hash Algorithm with a 256-bit output. A bit is the smallest unit of computer data, a single 0 or 1, so the output is 256 zeros and ones. To make that readable it is usually written in hexadecimal, a way of writing numbers using the digits 0 to 9 and the letters a to f, where each character stands for 4 bits. That is why every SHA-256 value is exactly 64 characters long.
Every input, whatever its size, is mixed through the same rounds and comes out as 256 bits, shown as 64 characters.
Older hash functions called MD5 and SHA-1 were once common. Researchers later found practical ways to create collisions for both, so they must not be used where security depends on the fingerprint, although you may still see them used to spot accidental damage to downloads. SHA-256 belongs to the SHA-2 family and is used for software downloads, digital signatures, website certificates and password systems.
You can see the avalanche effect for yourself. The cell below hashes two payment notes that differ in a single digit, ₹4,800 and ₹4,900, and counts how many of the 64 characters differ. You can press Run without knowing any Python, and you can change the text marked in the comment and run it again.
Expected output:
ce48fbd4a2320771d74e182cf953cb3ef6479032874c4ef9146805ffdb29d3ff
23d3994860d8131476e29ab5638709c0257c1b6f43dfb1555e16d4eb56cfe9b4
Characters that differ: 64 of 64
| Line | What it does |
|---|---|
import hashlib |
Brings in Python's ready-made toolbox of hash functions |
first = ..., second = ... |
The two texts to compare; they differ only in one digit |
hashlib.sha256(first.encode()).hexdigest() |
Turns the text into bits, runs SHA-256, and shows the result as 64 hex characters |
for i in range(64): |
Looks at each of the 64 positions in turn |
if a[i] != b[i]: |
Adds one to the count when the two fingerprints have a different character at that position |
One changed digit in the message changed every one of the 64 characters this time (usually around 60 change, practically never just a few), so a tampered invoice or update cannot hide behind a "nearly the same" hash.
The lab below does the same in your browser and lets you type freely. Start with the two payment notes, then try changing a capital letter, adding a space or swapping two words, and watch how much of the fingerprint moves each time.
Hashing is not encryption
Hashing and encryption are often confused because both turn readable data into something that looks like gibberish. The difference is whether you can get the original back. Encryption scrambles data with a key so that someone holding the right key can unscramble it: it is a locked box, built to be opened again. Hashing has no key and no way back: it is a fingerprint, built only to be compared.
Ask one question: will anyone ever need to read the original from this? If yes, encrypt. If you only need to check or compare, hash.
That question decides the right tool. Tulsi Health must read prescriptions again later, so prescriptions on a laptop are encrypted. It never needs to read a staff password again, only to check that a typed password is the same one, so passwords are hashed. A hash also has one important weakness as an integrity check. If an attacker can change both the file and the published fingerprint, for example by breaking into the website that shows it, the two will still match. A fingerprint is only as trustworthy as the place you got it from, which is why vendors also sign their updates and why Neha compares against a value from Rathi Softech's official site, not one sent in an email.
Sort each Tulsi Health task below by the tool it needs. For each card, ask whether anyone will need to read the original data again.