liveBook · Manning

Hash function

As explained in Real-World Cryptography

A hash function maps input data to a fixed-size output and may provide cryptographic properties such as one-wayness and collision resistance.

A hash function turns data of any length—a file, message, or video—into a fixed-size digest. Hashing the same input always produces the same digest, while even a tiny input change should produce a very different result. This makes a digest useful as a compact fingerprint for checking data. section 2.1

Figure 2.2 A hash function takes an arbitrary-length input (a file, a message, a video, and so on) and produces a fixed-length output (for example, 256 bits for SHA-256). Hashing the same input produces the same digest or hash.

Use the diagram to see arbitrary-length inputs mapped to fixed-length outputs, including SHA-256's 256-bit digest. section 2.1

Cryptographic hash functions are expected to provide three related protections:

  • Pre-image resistance makes recovering an input from its digest infeasible. It does not hide tiny or predictable inputs: an attacker can hash every plausible candidate and compare the results. section 2.2
  • Second pre-image resistance makes it infeasible to find a different input matching the digest of a given input. section 2.2
  • Collision resistance makes it infeasible to produce any two different inputs with the same digest. Larger digest sizes make collisions harder; practical cryptographic hashing generally uses at least 256 output bits. section 2.2 section 2.3

Verify a downloaded file

Download the file, compute its SHA-256 digest, and compare it with the digest published through a trusted channel. A match supports the file's integrity and authenticity; an attacker who changes the file should cause the digest to change. The comparison fails as an authentication method if the attacker can replace the published digest too. section 2.1 section 2.4.4

$ openssl dgst -sha256 downloaded_file
f63e68ac0bf052ae923c03f5b12aedc6cca49874c1c9b0ccf3f39b662d1f487b

The command-line example shows SHA-256 being computed for a downloaded file. section 2.1

Hash functions are rarely used alone. They support commitments, chunk-by-chunk integrity checks in BitTorrent, public-key identification in Tor addresses, and signatures over transaction digests. In each case, the digest helps only when the digest or the surrounding protocol is protected from substitution. section 2.4 section 2.4.3 section 2.4.4 section 2.5.4

Choose a standardized cryptographic function rather than a similarly named checksum. CRC32 detects some errors but supplies none of these security properties; MD5 and SHA-1 are considered broken because published collisions undermine collision resistance. SHA-2 remains widely adopted, while SHA-3 is recommended and avoids SHA-2's length-extension weakness. Serialize structured objects unambiguously before hashing; TupleHash is designed to help hash tuples without ambiguous concatenation. Passwords require slower, purpose-built password hashing, with Argon2 identified as the state-of-the-art choice. section 2.5 section 2.5.2 section 2.5.4 section 2.7

How Real-World Cryptography covers it

How deep it goes
Explains the mapping, three security properties, limitations, applications, algorithm choices, and serialization hazards without implementation-level internals.
Examples
Demonstrates file checks, OpenSSL commands, BitTorrent chunks, Tor addresses, commitments, and tampered cryptocurrency transactions.
What you can do after
Apply hashing via OpenSSL, compare trusted digests, distinguish cryptographic hashes from checksums, and avoid predictable or ambiguous inputs.
How it connects
Connects hashing with integrity, authenticity, commitments, digital signatures, SHA-2, SHA-3, TupleHash, and password hashing.

Also explained in

Compare how 16 books explain hash function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Real-World Cryptography. The section links point to the parts of the book it draws on.