Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 41 ยท Cryptography Foundations

Cryptographic Hash Functions

Learn what a cryptographic hash function does, with SHA-256 in Python: determinism, the avalanche effect, preimage and collision resistance, bytes vs text.

A cryptographic hash function turns any input, from a single letter to a whole movie, into a short, fixed-size fingerprint called a digest. It is the most important building block in a blockchain: it links blocks, summarizes transactions, powers mining and produces addresses. Fortunately, you do not need advanced mathematics to use it. You need to know what properties it promises and what those promises mean in practice.

Python ships with the algorithms you need in the hashlib module. In this lesson you will use SHA-256, the function used by Bitcoin, to see each property with your own eyes.

The first digest

SHA-256 always returns 256 bits, usually shown as 64 hexadecimal characters. The input must be bytes, so text is encoded first:

Python Your first SHA-256 digest

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Whether the input is empty or ten million bytes, the digest has the same size. A digest is not a compressed copy of the data. It is only a fingerprint, and you cannot get the data back from it.

The properties that matter

A good cryptographic hash function has these properties:

  1. Deterministic. The same input always gives the same digest. Everyone on Earth who hashes hello with SHA-256 gets the same 64 characters.
  2. Fast to compute. Hashing a block takes microseconds.
  3. Avalanche effect. A tiny change to the input (one bit) changes about half of the output bits, so the new digest looks unrelated.
  4. Preimage resistance (one-way). Given a digest, it is infeasible to find any input that produces it.
  5. Second-preimage resistance. Given one input, it is infeasible to find a different input with the same digest.
  6. Collision resistance. It is infeasible to find any two different inputs with the same digest.

Properties 4 to 6 say "infeasible", not "impossible". Since there are infinitely many inputs but only 2^256 outputs, collisions must exist; the point is that nobody can find one with any realistic amount of computing.

Determinism and the avalanche effect

Python Avalanche effect

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Changing one letter flips roughly half of the 256 bits (about 128, give or take a few). There is no pattern you could use to guess "how close" an input is to another. This is why a hash can reveal that data changed but never how.

Preimage resistance: why you can only guess

If you want an input whose digest begins with a particular pattern, there is no shortcut. You must try inputs one after another. This brute-force search is exactly what mining does (Proof of Work and Mining).

Python Searching for a prefix by brute force

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each hex character has 16 possibilities, so a two-character prefix has a chance of 1 in 256 per attempt. Asking for six zeros would take about 16.7 million tries on average. The only way to find such an input is luck and effort, and that asymmetry (hard to find, instant to check) is the foundation of proof of work.

Hex digests, bytes and text

hexdigest() shows the digest as hex text, but the digest is really 32 bytes. Both views are useful:

Python Digest as bytes and as hex

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two traps catch beginners:

  • Hash the bytes, not the idea. "รฉ" can be encoded in different ways. Always choose one encoding (UTF-8) and one exact byte layout. The same applies to numbers and objects: 5, "5" and 5.0 are different bytes.
  • Hash structured data in a fixed order. Two dictionaries with the same items in a different order produce different JSON text and therefore different digests, unless you sort the keys. Blockchains define an exact serialization for this reason (Designing a Block).
Python Serialization changes the hash

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Which hash functions are in use?

Function Output Notes
SHA-256 256 bits Bitcoin block hashing; very widely used
SHA-3 / Keccak-256 256 bits Ethereum uses a Keccak variant for addresses and storage
RIPEMD-160 160 bits Combined with SHA-256 in Bitcoin address creation
MD5, SHA-1 128, 160 bits Broken for collision resistance; never use for security

hashlib provides sha3_256 too (the final NIST standard), but Ethereum uses the original Keccak padding, so their outputs differ. For learning, SHA-256 is enough.

Never use plain hashes to store passwords. They are too fast, so attackers can try billions per second. Use a slow, salted password hashing function instead. That is a different job from the one hashes do in a blockchain.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How long is a SHA-256 digest, whatever the input size?

    SHA-256 always produces 256 bits. In hexadecimal each character stores 4 bits, so that is 64 characters.
  2. 2. You change one character of a message and hash it again. What do you expect?

    This is the avalanche effect. Every output bit depends on every input bit, so tiny changes spread everywhere.
  3. 3. Why is mining basically a brute-force search?

    Preimage resistance means you cannot work backwards from a desired digest. You can only try candidate inputs, and checking each one is instant.
  4. 4. Why can the same dictionary produce different digests in two programs?

    Hash functions hash bytes. If serialization differs (key order, spacing, encoding), the bytes differ, so the digests differ. Fix a canonical format.
  5. 5. Which hash function should you avoid for security purposes?

    Practical collision attacks on MD5 (and SHA-1) are known, so they cannot be trusted where collision resistance matters.

Summary

  • A cryptographic hash function maps any input to a fixed-size digest; SHA-256 gives 256 bits (64 hex characters).
  • It is deterministic, fast, one-way and collision resistant, and a one-bit input change flips about half the output bits.
  • Finding an input with a chosen digest pattern requires brute force; checking an answer is instant. This powers proof of work.
  • Hash functions hash bytes, so fix an encoding and a canonical serialization.
  • Avoid MD5 and SHA-1, and never use a plain fast hash for passwords.

Next: Hash Pointers and Tamper Evidence, where you link records by their digests.