Skip to main content

Type a word from a question or an article title.

1 · Basics

Hash functions in plain words - a data fingerprint that cannot be forged

What a hash function is, its properties, why a blockchain needs it, how keccak256 differs from SHA-256 and where you meet hashes every day.

3 min readUpdated:
In this article

A hash function is one of the "bricks" the whole blockchain stands on. It turns any data - a word, a letter, a gigabyte file - into a short string of fixed length. That string is called a hash, or a "fingerprint" of the data.

An example you can check

Take the keccak256 function Ethereum uses and apply it to two nearly identical words:

text
keccak256("hello") = 0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8
keccak256("Hello") = 0x06b3dfaec148fb1bb2b066f10ec285e7c9bf402ab32aa78a5d38e34566810cd2

The difference is one capital letter, and the results have nothing in common. The hash length is always the same: 32 bytes, that is 64 hexadecimal characters (plus 0x in front). Whether the input is "hello" or the whole of "War and Peace" does not matter.

Four properties of a good hash

  1. Determinism. The same data always gives the same hash. You and your counterparty, independently, get the same result.
  2. The avalanche effect. The slightest change of input changes the hash beyond recognition. So a change of even one comma in a contract is immediately visible.
  3. One-wayness. You cannot recover the original data from the hash. The only way is to try variants until the hash matches.
  4. Collision resistance. It is practically impossible to find two different data sets with the same hash, and impossible to "fit" a document to a desired hash.

Where hashes work in a blockchain

  • Linking blocks. Each block contains the hash of the previous one (how a blockchain works). Changing an old block means changing its hash and breaking the whole chain after it.
  • Addresses. An Ethereum address is the last 20 bytes of the hash of the public key (details).
  • Identifiers. A function number in the ABI and an event identifier are the first bytes of the hash of their signature.
  • The Merkle tree. Hashes combine into one "root" value that lets you check whether an element belongs to a set (Merkle trees).
  • Deal terms. We record in the contract the hash of the document, not the document itself (hash commitment).

SHA-256 and keccak256

There are many hash functions. In blockchains two are most common:

  • SHA-256 - used in Bitcoin.
  • keccak256 - used in Ethereum.

They are similar in properties but give different results for the same input. Confusing keccak256 with the standardised SHA-3 is a common mistake: they share an ancestor but differ in details, so the hashes do not match. If you check a hash "by hand" in an online calculator, make sure keccak256 is selected.

Everyday examples

  • Downloading a file. The download page states the file's hash. You compute the hash yourself and compare: a match means the file was not swapped.
  • Passwords. Sites store not the password but its hash (with an addition called a "salt"): a leaked database does not reveal passwords directly.
  • Git. A commit number is a hash of its contents.
  • Signing documents. What is signed is not the whole document but its hash: faster and more reliable (signatures).

Limits and subtleties

Short predictable data is vulnerable to brute force. An attacker computes the hash of "123456" in a second and compares it with a list. For short or guessable texts a random string, a salt, is added.

A hash does not prove data is truthful. It proves only that it is the same.

Normalisation matters. The "same" text with different line breaks gives different hashes. That is why deal terms are brought to a canonical form before hashing.

A hash protects data from swapping, but what proves that the action was performed by the owner? The answer is in the article on public-key cryptography.

Mentioned in

Ready to try it?

Create your first deal in a couple of minutes. The money is locked in a smart contract and paid out by rules you can see in advance.