Blockchain technology · · 7 min read
What is a hash function? The digital fingerprint of crypto
A hash function is a mathematical operation that takes data of any length and turns it into a short, fixed-length output called a hash or digest. The same data always produces the same hash, yet working backwards from the hash to the original data is practically impossible, which is why a hash works like a digital fingerprint.
In Bitcoin, this one tool links blocks together, creates the mining puzzle, and produces transaction IDs and addresses. This article explains what a hash function does, which properties make it secure, and the misunderstandings that come up most often.
What a hash function does
Take SHA-256, the function Bitcoin uses. The 256 in its name means the output is 256 bits long. That output is usually written as a 64-character hexadecimal string made of the digits 0–9 and the letters a–f.
- The SHA-256 hash of the input "abc" begins with ba7816bf8f01cfea.
- The hash of "Abc", with only the first letter capitalized, begins with 06d90109c8cce34e.
The inputs differ by a single letter, yet the outputs look nothing alike: 131 of the 256 bits have changed, roughly half. This behavior is called the avalanche effect. Because of it, you cannot look at an output and guess what the input was "close to".
The size of the input does not change the size of the output either. A three-letter word, a book hundreds of pages long and a file several gigabytes in size all produce a 64-character hash. That makes it possible to compare large pieces of data through a small summary that stands in for them.
Properties of a secure hash function
Not every hash function is fit for security work. A cryptographic hash function is expected to have these properties:
| Property | What it means |
|---|---|
| Deterministic | The same input always gives the same output |
| Fast | Computing the hash takes little time |
| One-way (preimage resistance) | Recovering the input from the hash is practically impossible |
| Second-preimage resistance | Given one input, finding a different input with the same hash is practically impossible |
| Collision resistance | Finding any two different inputs with the same hash is practically impossible |
| Avalanche effect | A small change in the input changes the output completely |
"Impossible" here is meant in a practical, not a mathematical, sense. The output has a fixed length while the possible inputs are unlimited, so different inputs with the same hash must exist. The question is how much work it takes to find them.
Security in numbers
A 256-bit output has 2 to the power of 256 possible values, a number with 78 digits. Since there is no known shortcut for finding an input that produces a given hash, the only way is to keep guessing, and the number of guesses required is on that scale.
Finding a collision is easier, because you do not need a specific result, only any pair that matches. This resembles the birthday paradox: in a group of 23 people, the chance that two share a birthday is above 50%, because any matching pair will do. By the same logic, finding a SHA-256 collision takes roughly 2 to the power of 128 attempts.
Imagine, hypothetically, a computer that can try a billion billion (10 to the power of 18) hashes per second. It would need about 10 trillion years to get through 2 to the power of 128 attempts, hundreds of times the known age of the universe.
This resilience is not a permanent promise, though. Practical ways to produce collisions were eventually found for MD5 and SHA-1, both once widely used, and they were abandoned for security-sensitive work. Trust in a cryptographic hash rests on it surviving years of open scrutiny.
Where hashing is used in crypto
Hash functions do a job at almost every layer of a blockchain:
- Linking blocks: Each block carries the hash of the block before it. If a single character changes in an old block, that block's hash changes and the link to the next block breaks.
- Mining: In proof of work, miners keep changing a number in the block header and hashing it again until the result falls below a target set by the network. Since the outcome cannot be predicted, this is a race of computing power.
- Transaction IDs: In Bitcoin, each transaction's ID (TXID) is a hash of the transaction data. It is the long string you paste into a block explorer to look up a transaction.
- Merkle root: The hashes of a block's transactions are paired up and hashed again, over and over, until a single value remains. As a result, you can prove that a transaction is included in a block of 1,024 transactions with just 10 hashes, without downloading all of them.
- Addresses: Classic Bitcoin addresses are derived by hashing the public key (the shareable key derived from the private key) first with SHA-256 and then with RIPEMD-160. On Ethereum, the address is the last 20 bytes of the Keccak-256 hash of the public key.
- File verification: Wallet software often publishes the hash of its installer. If the hash you compute on your own computer does not match the published value exactly, the file may have been altered or corrupted.
Bitcoin applies SHA-256 twice in a row for block headers and transaction IDs. Keccak-256, used by Ethereum, is the algorithm the SHA-3 standard was based on, but it is not identical to the standardized version, so the two produce different results for the same input.
Common misunderstandings
- Hashing is not encryption. Encrypted data can be decrypted with the right key. A hash has no key to unlock it; the operation only goes one way.
- One-way does not mean unguessable. If the input is short or predictable, an attacker can hash likely inputs one by one and compare the results. That is why the hash of a weak password is not safe. Well-designed systems add a random value (a salt) to each password before hashing and deliberately use slow methods.
- A hash does not hide anything; it only summarizes. Publishing a file's hash does not protect its contents. It only lets you check whether the file has changed.
- A matching hash does not prove the source is trustworthy. A fake site can publish both a tampered file and the hash that matches it. Make sure the value you compare against comes from an official channel; some projects also sign their hash lists with a digital signature.
A hypothetical example: Elif downloads a wallet installer and compares only the first and last four characters of the hash she computes with the published value. That is not enough. Getting a hash whose first and last four characters match takes 16 to the power of 8 attempts on average, about 4.3 billion, which an ordinary computer can do in a reasonable amount of time. A safe comparison requires all 64 characters to match.
Key takeaways
- A hash function turns data of any size into a fixed-length fingerprint; the same input always gives the same output.
- A secure hash is one-way, resistant to collisions, and produces a completely different result after a tiny change.
- SHA-256 draws its strength from the sheer number of possible outputs, while MD5 and SHA-1 show that this strength is not permanent.
- In cryptocurrencies, hashing links blocks, makes mining possible, and produces transaction IDs and addresses.
- Hashing is not encryption: it does not protect predictable inputs, and a comparison only counts if every character matches.