A Blockchain Primer › Module 2 › Lesson 2.3

Lesson 2.3: Giving Data a Digital Fingerprint — The Properties and Uses of Hash Functions

  • #1. The Core Question
  • #2. An Everyday Analogy
  • #3. Core Concepts Explained
  • #4. Diagrams
  • #5. Key Takeaways
  • #6. Quiz

The last lesson wrote ownership into transaction history. But that history keeps growing, and nodes need a fast way to tell whether two sets of data are the same and whether anything has been changed. This lesson introduces a basic tool for that: the hash, data's digital fingerprint.

1. The Core Question

A dataset holds tens of thousands of transactions. How can you quickly tell whether the data on both sides is exactly the same? If a single punctuation mark changed, how do you spot it right away? And can you build a puzzle that can only be solved by burning computing power?

This lesson answers three questions: How does a hash turn any data into a fixed-length fingerprint? What properties do these fingerprints need to have, reliably? And what can you use them for?

The short version: A hash squeezes any data into a fixed-length fingerprint. That lets you identify data, compare it, and detect changes very cheaply, and it even lets you create computational puzzles.

2. An Everyday Analogy

Human Fingerprints and Data Fingerprints

A person's fingerprint can't tell you their life story, but it can tell you whether this is the same person. A data fingerprint works the same way. It doesn't rebuild what the data says. It only answers one question: is this the same data?

Say you hand a contract to the other party, and both of you compute the fingerprint of the contract file. If the fingerprints match, the two files were identical at the moment you computed them. If they don't, the files differ in at least one spot.

The Shipping Seal

A seal on a shipping box can't stop someone from opening the box, but it lets you see at a glance whether the box has been resealed.

A hash value isn't a lock either. It can't stop anyone from changing data. It's more like a "content seal": change the data and the fingerprint changes, so whoever checks it notices right away.

The Coat-Check Tag

A coat-check tag points to where your coat is hanging. A hash reference points to where the data is stored, and it can also check whether it's still the same coat.

The Combination Lock and Trial and Error

If you don't know the code to a combination lock, all you can do is try one setting after another. A hash puzzle works the same way. There's no shortcut. You keep changing an adjustable number and recomputing, over and over, until the result meets some condition. It isn't about knowing the answer. It's about burning computing power on attempt after attempt.

3. Core Concepts Explained

3.1 What a Hash Function Is

A hash function is a small program that takes in data of any length and puts out a number of fixed length. That output is called the hash value, or hash number.

Two key points:

  • The input can be any length: a sentence, a file, or a whole run of transaction records all work.
  • The output length is fixed: no matter how big the input is, the result is a string of digits of the same length.

Hash values are usually written in hexadecimal, using 0–9 and A–F. Different hash functions produce hash values of different lengths.

3.2 The Five Properties of a Cryptographic Hash Function

Not every hash function can serve as a digital fingerprint. A cryptographic hash function usually needs five properties.

First, it can process any data quickly. It can't accept only some special format, and it can't be slow. Otherwise the system would bog down under large volumes of transaction data.

Second, it's deterministic. The same input always gives the same output. As long as the input is identical, the hash value should be identical no matter which computer computes it or when. That's what lets two nodes compare data by comparing hash values.

Third, it's pseudo-random. Change the input even slightly, and the output changes in a way you can't predict. Change one punctuation mark in a sentence, and the hash value may look completely different.

Fourth, it's one-way. Going from input to output is easy. Going backward from output to input is practically impossible. A hash value doesn't give away what the original data says, just as seeing a fingerprint doesn't let you rebuild the whole person.

Fifth, it's collision-resistant. When two different inputs produce the same hash value, that's called a hash collision. Collision resistance means a pair of inputs like that is very hard to find. It doesn't promise that collisions never happen. It makes the odds of finding one in practice so low you can ignore them.

Collision resistance is what makes it possible for hash values to work as digital fingerprints. If it were easy to find two different datasets with the same fingerprint, you couldn't use fingerprints to identify data.

3.3 Five Ways of Using Hashes

A hash function takes one piece of data at a time. In practice, though, we often need a fingerprint for a whole group of data. There are five common patterns.

Independent hashing. Each piece of data gets its own hash value, and none of them affects the others. This fits cases where each item needs to be identified on its own.

Repeated hashing. Feed a hash value back in as input and hash it again, adding another layer on top of an existing fingerprint.

Combined hashing. First join several pieces of data into one in some way, then hash the result as a whole. The upside is that you get just one fingerprint. The downsides are that you can't see each piece's own fingerprint, and the way you join them affects the result.

Sequential hashing. Combine the hash value of the data you already have with the new data that just arrived, then compute a new hash. This fits cases where new data keeps coming in and the overall fingerprint has to stay up to date.

Hierarchical hashing. First hash each piece of data separately, then combine the hashes in pairs and keep hashing upward until you end up with a single fingerprint at the top. The structure looks like an upside-down tree and is usually called a Merkle tree.

Hierarchical hashing has one important feature: if you know the hash values of certain pieces of data, you can keep computing the fingerprints above them even without the original data. That sets things up for later, when we use hashes to organize large numbers of transactions and to check whether a particular transaction is included in a block.

3.4 Use One: Comparing Data Quickly

The most direct way to compare two datasets is byte by byte. That's fine for small data, but it takes a long time for large data.

Hash values squeeze data of any length into a fixed length, so comparing two hash values tells you quickly whether the data matches:

  • Different hash values mean the two datasets are different.
  • Identical hash values mean the two datasets can be considered the same.

"Can be considered" matters here. Collision resistance isn't an absolute mathematical guarantee. It just makes collisions practically impossible to find.

3.5 Use Two: Detecting Whether Data Has Been Altered

At some point in time, compute the hash value of a set of data and save it. Later, if you want to check whether the data has changed, compute the hash value again and compare it with the old one.

  • If the two values match, the data hasn't been changed during that time.
  • If the two values differ, the data has changed.

This also works when sending data. The sender computes the hash value first, the receiver computes it again when the data arrives, and comparing the two shows whether anything was changed along the way. It relies on two properties. Collision resistance lets "same fingerprint" stand for "same data," and pseudo-randomness makes even a tiny change produce an obvious change in the fingerprint.

3.6 Use Three: Hash References

An ordinary reference only records where the data is. A hash reference adds the data's hash value, so it tells you two things at once:

  • Where the data is stored.
  • What the data should contain.

If someone changes the data, the location may still be there, but the hash won't match. The reference becomes invalid, a "broken reference." That way, users never end up with tampered data without knowing it. Hash references fit data that's supposed to stay the same once it's created.

3.7 Use Four: Change-Sensitive Storage

Link data together with hash references, and you get two common structures.

Chain structure. Each piece of data contains a hash reference that points to the piece before it. When new data arrives, it's combined with the previous reference to create a new reference. From the last reference (the head of the chain), you can trace the entire chain backward.

Tree structure. Each of several pieces of data first gets its own hash reference. Then the references combine in pairs, layer by layer, until there's a single root reference at the top. Any change to data at the bottom ripples up along its path to the references above it, and in the end it changes the root reference.

Two structures here are easy to mix up:

  • Merkle tree: mainly used to compute hashes layer by layer. It doesn't store the original data. Even without all the original data, if you know the right hash values, you can keep computing upward.
  • Merkle Patricia Trie: a specific kind of tree that stores data and also uses hash references to keep anyone from quietly changing it.

Both use hash references, but one is mainly about computing fingerprints, and the other is about storing data and protecting it.

3.8 Use Five: Creating Hash Puzzles

Some situations call for an operation that uses up real computing resources. The hash puzzle is built for exactly that.

It has four parts:

  • A piece of data that has to stay the same.
  • A number you can adjust freely, called the nonce.
  • A hash function.
  • A restriction on the hash result, usually called the difficulty.

Here's how it works. Combine the data with the nonce and hash them, then check whether the result meets the restriction. If it doesn't, change the nonce and try again, until you find a value that works.

Difficulty is usually expressed as how many leading zeros the hash value has to have. The more zeros required, the more attempts it takes on average, and the more computing power and time it burns.

The only way to solve a hash puzzle is trial and error, because the hash function is one-way. You can't work backward from the result you want to the input you'd need. But once someone finds the answer, it's easy for others to check: hash the data and the nonce one more time and see whether the result meets the condition.

In blockchain, this mechanism is usually called proof of work. In some specific systems, solving the puzzle and earning the reward is also called mining. This lesson covers only the basic structure of the hash puzzle. The specific consensus process comes later.

3.9 Why Blockchain Uses Hashes So Heavily

Blockchain mainly puts hashes to work on three things:

  • Storing transaction data in a way that makes changes show up.
  • Using hash values as digital fingerprints for transaction data.
  • Using hash puzzles to make changing history computationally expensive.

All three come up again and again in later lessons on block structure, tamper resistance, and consensus mechanisms.

4. Diagrams

Figure 2-3-1: A Hash Function as a Digital Fingerprint Machine

Any input goes into the hash function, and a fixed-length digital fingerprint comes outAny input goes into the hash function, and a fixed-length digital fingerprint comes out
Figure 2-3-1 Any input goes into the hash function, and a fixed-length digital fingerprint comes out
Click to enlarge

Figure 2-3-2: The Five Hashing Patterns

Independent, repeated, combined, sequential, and hierarchical hashingIndependent, repeated, combined, sequential, and hierarchical hashing
Figure 2-3-2 Independent, repeated, combined, sequential, and hierarchical hashing
Click to enlarge

Figure 2-3-3: The Hash Puzzle

Keep adjusting the nonce until the hash result meets the difficulty requirementKeep adjusting the nonce until the hash result meets the difficulty requirement
Figure 2-3-3 Keep adjusting the nonce until the hash result meets the difficulty requirement
Click to enlarge

5. Key Takeaways

  • A hash function turns data of any length into a hash value of fixed length.
  • A cryptographic hash function has to process any data quickly and be deterministic, pseudo-random, one-way, and collision-resistant.
  • Determinism means different nodes get the same fingerprint for the same data. Pseudo-randomness means small changes to the input cause big changes to the output. One-wayness means you can't turn the fingerprint back into the original.
  • The five patterns are independent, repeated, combined, sequential, and hierarchical. Hierarchical hashing is the Merkle tree.
  • Hash values make it possible to compare data quickly, detect changes, and build references that are sensitive to change.
  • Chain and tree structures link data with hash references, and changes at the bottom ripple upward.
  • Hash puzzles create computational cost through the nonce and the difficulty. Only trial and error can solve them, but checking a solution is easy. In blockchain this is usually called proof of work.
  • A hash value isn't a lock and can't prevent changes. What it does is make changes visible.

6. Quiz

Part 1: Single Choice (1–4)

Question 1

What is the most essential feature of a hash function's output?

A. The output gets longer as the input gets longer
B. No matter how long the input is, the output is a fixed length
C. The output has to be in Chinese
D. You can rebuild the input directly from the output

Show answer

Answer: B

Explanation: What defines a hash function is that the input can be any length and the output is a fixed length. A has it exactly backward, C contradicts what a hash value looks like, and D breaks the one-way property.

Question 2

What's the main benefit of comparing two large files by their hash values?

A. You can see every line inside the files
B. You don't need to compare byte by byte; comparing fixed-length fingerprints is enough
C. Hash values can repair files automatically
D. Hash values make files smaller

Show answer

Answer: B

Explanation: Hash values squeeze data of any length into a fixed length, so comparing fingerprints is enough. A, C, and D aren't things hashes do.

Question 3

What's the key difference between a hash reference and an ordinary reference?

A. A hash reference doesn't need to point to any data
B. A hash reference contains both the location and a fingerprint of the contents, so it can check whether the data changed
C. A hash reference can only point to images
D. An ordinary reference always prevents tampering

Show answer

Answer: B

Explanation: A hash reference points to a location and also carries a fingerprint of the contents, so if the data changes, the reference becomes invalid. A and C describe it wrong, and an ordinary reference on its own can't prevent tampering.

Question 4

Which hashing pattern does a Merkle tree belong to?

A. Independent hashing
B. Repeated hashing
C. Hierarchical hashing
D. Combined hashing

Show answer

Answer: C

Explanation: A Merkle tree builds its tree shape by combining hashes layer by layer. That's hierarchical hashing.

Part 2: Multiple Choice (5–7)

Question 5

Which properties should a cryptographic hash function usually have?

A. Processing any data quickly
B. Determinism
C. Pseudo-randomness and one-wayness
D. Collision resistance

Show answer

Answer: A, B, C, D

Explanation: All five properties are important features of a cryptographic hash function.

Question 6

Which statements about how hash values are used in real systems are correct?

A. Comparing data quickly
B. Detecting whether data has been changed
C. Serving as references that are sensitive to change
D. Creating hash puzzles that take computing resources to solve

Show answer

Answer: A, B, C, D

Explanation: Comparing data, detecting changes, change-sensitive references, and hash puzzles are all real uses of hash values.

Question 7

Which statements about hash puzzles are correct?

A. The only way to find the nonce is repeated trial and error
B. The higher the difficulty, the more attempts it takes on average
C. Once someone finds the answer, it's fairly easy for others to check it
D. You can compute the nonce directly by running the function in reverse

Show answer

Answer: A, B, C

Explanation: Only trial and error can solve a hash puzzle, the difficulty affects how many attempts it takes, and checking the answer is easy once it's found. D is wrong because hashing is one-way, so you can't get the nonce by working backward.

Part 3: True or False (8–9)

Question 8

True or false: a hash value can stop other people from changing data, so it works like a lock.

Show answer

Answer: False

Explanation: A hash value can't prevent changes. It only exposes them. It's more like a seal or a fingerprint than a lock.

Question 9

True or false: a Merkle tree and a Merkle Patricia Trie are the same structure, and both store the original data.

Show answer

Answer: False

Explanation: A Merkle tree mainly computes hashes layer by layer and doesn't store the original data. A Merkle Patricia Trie stores data and also protects it with hash references.

Part 4: Scenario Analysis (10)

Question 10

A team wants to pack ten thousand transactions into one overall fingerprint, so that later they can quickly check whether a particular transaction is in the batch without rereading all ten thousand every time. They also want to publish a set of important files and prove those files haven't been changed since.

Answer the following:

  1. Which hashing pattern is the best fit for packing these ten thousand transactions? Why?
  2. If one transaction gets changed, what happens to the top-level fingerprint?
  3. For the important files, is it enough to save the files themselves? What else do you need?
Show answer

Answer:

  1. Hierarchical hashing, meaning the Merkle tree. It organizes the ten thousand transactions into layers and keeps the hashes in between, so checking one transaction only takes the hash values along part of the path. You don't have to reprocess all the data.
  2. That transaction's hash changes, which affects its branch and the nodes above it, and in the end changes the top-level fingerprint. That exposes the change to the data.
  3. Saving the files alone isn't enough, because someone could quietly swap a file without anyone easily noticing. You also need to save the files' hash values as a "content fingerprint" you can refer back to, and keep them somewhere an attacker can't freely change, such as a public record or a protected system.