liveBook · Manning

Cryptographic hash

As explained in Real-World Cryptography

A hash function designed to provide security properties such as collision resistance, pre-image resistance, and second pre-image resistance.

A cryptographic hash turns an arbitrary-length file, message, or video into a fixed-length digest. Hashing the same input repeats the same result, while a tiny input change can produce a completely different one. Security depends on making it difficult to recover an input, replace an input with another matching the digest, or find any two inputs with the same digest. section 2.1 section 2.7

The main security properties are:

  • Pre-image resistance makes it infeasible to find an input that produced a given digest. section 2.7
  • Second pre-image resistance makes it infeasible to find a different input matching the digest of a known input. section 2.1 section 2.7
  • Collision resistance makes it infeasible to find two arbitrary inputs with the same digest. section 2.7

Checking a downloaded file

Hash the downloaded file with SHA-256, then compare the result with the digest published on the webpage. A match supports the claim that the file was not replaced. The published digest must itself be obtained securely; otherwise an attacker can replace both the file and its digest. section 2.1 section 2.4.4

$ openssl dgst -sha256 downloaded_file
f63e68ac0bf052ae923c03f5b12aedc6cca49874c1c9b0ccf3f39b662d1f487b

Run the OpenSSL command to hash a downloaded file and compare its hexadecimal digest with the expected value. section 2.1

Digest size matters: a very short output makes collisions easy, so practical cryptographic hashes use substantially larger outputs. CRC32 detects some accidental errors but lacks these security properties; MD5 and SHA-1 are deprecated for secure use because published collisions broke their collision resistance. section 2.3 section 2.5

SHA-2 pads and divides input into blocks, then repeatedly feeds each block and the previous intermediate value into a compression function. SHA-3 instead uses a sponge: it absorbs padded input through a permutation and squeezes out the required digest. SHA-2 is unsuitable for hashing secrets because its construction permits length-extension attacks. section 2.5.1 section 2.5.2

Hashes support integrity or identity checks in several systems:

  • Subresource integrity lets a browser hash an external JavaScript file before executing it. section 2.4.2
  • BitTorrent hashes individual chunks so peers can verify pieces before reassembling a file. section 2.4.3
  • Tor onion addresses can represent a hidden service’s public-key hash, helping authenticate the intended service. section 2.4.4

How Real-World Cryptography covers it

How deep it goes
Explains security properties, limitations, applications, and SHA-2 and SHA-3 constructions, but defers secret hashing and password hashing.
Examples
Shows OpenSSL file and message commands, subresource integrity, BitTorrent chunks, Tor onion addresses, and hexadecimal digests.
What it assumes
Assumes basic familiarity with files, messages, and command lines while teaching digest terminology and security properties from concrete examples.
What you can do after
Apply it via OpenSSL to verify downloaded files and recognize when a digest alone cannot authenticate an untrusted source.
How it connects
Connects hashes with integrity, authenticity, collision resistance, pre-image resistance, public keys, SHA-2, SHA-3, and message authentication codes.

Also explained in

Compare how 4 books explain cryptographic hash →

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.