No history yet

Introduction to Hash Functions

What Is a Hash Function?

Think of a hash function as a machine that creates a unique fingerprint for any piece of digital data. You can feed it a tiny text file or an entire movie. No matter the size of the input, the machine processes it and spits out a short, fixed-length string of characters. This output is called a hash.

The core job of a hash function is to map data of any size to a value of a fixed size. It's a one-way process. You can easily generate a hash from your data, but you can't go backward and reconstruct the original data from the hash.

This process isn't random. A good hash function follows a strict set of rules that make it reliable and useful. Let's look at the most important properties.

Key Properties

Three characteristics define a hash function and make it powerful.

Determinism: The same input will always produce the same output. If you hash the word "hello" today, you'll get the exact same hash for "hello" tomorrow. This makes hashes predictable and verifiable.

Fixed-Size Output: Whether you hash a single letter or a thousand-page book, the resulting hash will have the same length. This makes it easy to store and compare hashes, as you always know what size to expect.

Avalanche Effect: A tiny change to the input, like changing a single letter, results in a completely different and unpredictable hash. The change avalanches through the output, altering it dramatically.

Let's see these properties in action. The table below uses a common hash function called SHA-256. Notice how different inputs produce outputs that are all the same length. Also, see how changing just the capitalization of one letter creates a wildly different hash.

InputSHA-256 Hash Output
hello2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
hello2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Hello185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
The quick brown fox jumps over the lazy dog.ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c

The first two rows demonstrate determinism. The third row shows the avalanche effect when compared to the first. And finally, the last row illustrates that even a much longer input still produces an output of the same fixed length.

A Simple Example

To understand how a hash function works under the hood, we can create a very simple one. This example is not secure for real-world use, but it illustrates the basic mechanics.

Our function will take a word, convert each letter to its numerical position in the alphabet (A=1, B=2, etc.), sum these numbers, and then find the remainder when dividing by 10. The remainder will be our hash.

Let's try it with the word "CAT":

  1. C is the 3rd letter, A is the 1st, T is the 20th.
  2. Sum the numbers: $3 + 1 + 20 = 24$.
  3. Find the remainder when divided by 10: $24 \pmod{10} = 4$.
  4. The hash for "CAT" is 4.

What about the word "ACT"? It has the same letters, so it will also have a sum of 24 and a hash of 4. This is called a collision, where two different inputs produce the same hash. While collisions are possible in all hash functions, good ones are designed to make them extremely rare.

Our simple function isn't very good because collisions are too easy to find. It also doesn't show a strong avalanche effect. Real-world hash functions use complex mathematical operations to avoid these problems.

Time to check your understanding.

Quiz Questions 1/5

What is the primary purpose of a hash function?

Quiz Questions 2/5

If the input "Hello World" produces a specific hash, what will the input "hello world" produce?

Hash functions are a fundamental building block in computer science, used for everything from storing passwords securely to ensuring data integrity.