๐Ÿ”‘ Cryptography

Cryptographic hash functions: what they are and what they do

What a cryptographic hash is, which properties it needs, why MD5 and SHA-1 are obsolete and how SHA-256 protects files, signatures and evidence.

A cryptographic hash function is one of the most used and least visible tools in computing. It encrypts nothing and has no keys, yet without it there would be no digital signatures, download checks, blockchains or reliable forensic acquisitions. Let us look at what it is, which properties it must have and which algorithms to use today.

A fingerprint for data

A hash function takes input of any length, from a single word to an entire disk, and returns a fixed-length value called a hash, digest or fingerprint. With SHA-256, for example, the output is always 256 bits, usually written as 64 hexadecimal characters.

The SHA-256 hash of the string "abc" is:

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

Change a single letter, or add a space, and the result changes completely, bearing no resemblance to the previous one. This behaviour is called the avalanche effect.

The three security properties

Not every function that summarises data is suitable for cryptographic use. A checksum such as CRC is meant to detect accidental transmission errors, but it does not resist someone who deliberately wants to manipulate the data. A cryptographic hash function must guarantee three properties:

  1. Preimage resistance: given a hash, it must be infeasible to find any message that produces it. The function is "one-way".
  2. Second-preimage resistance: given a message, it must be infeasible to find a different one with the same hash.
  3. Collision resistance: it must be infeasible to find any two messages with the same hash.

Collisions necessarily exist, because the possible inputs are infinite and the outputs are not. Security lies in the fact that finding one requires an unreachable amount of computation.

Obsolete and current algorithms

Algorithm Output length Status
MD5 128 bits Practical collisions, do not use for security
SHA-1 160 bits Practical collision demonstrated in 2017, obsolete
SHA-256 / SHA-512 (SHA-2 family) 256 / 512 bits Secure and extremely widespread
SHA-3 224-512 bits Secure, internal structure different from SHA-2
BLAKE2 / BLAKE3 variable Secure and very fast

For MD5, collisions can now be generated quickly on an ordinary computer. They have even been used in real attacks to create fake certificates. For SHA-1, in 2017 a group of researchers produced two different PDF files with the same hash, showing that the attack was no longer merely theoretical.

SHA-3 was not created because SHA-2 was broken, but to provide an alternative based on a different construction principle, in case weaknesses are discovered in the SHA-2 family in the future.

What they are used for in practice

Checking file integrity

Many software websites publish the SHA-256 hash of their downloadable files. By computing the hash of the file you received and comparing it with the published one, you verify that it has not been damaged or altered. Be careful: the check only makes sense if the reference hash comes from a trustworthy source, separate from the one that might have been tampered with.

Digital signatures

A digital signature is not applied to the entire document but to its hash. This is far more efficient and, thanks to collision resistance, signing the fingerprint is equivalent to signing the document. If a hash algorithm becomes vulnerable to collisions, signatures based on it lose their value too: that is why SHA-1 was abandoned in certificates.

Digital evidence

In my digital forensics work, the hash is the tool that ties a copy to its original: you compute the fingerprint of the acquired media and record it; anyone can later recompute it and check that the copy has not changed by a single bit.

Message authentication codes

Combining a hash with a secret key produces an HMAC, which lets those who share the key verify both the integrity and the origin of a message. It is used in network protocols, APIs and authentication tokens.

Blockchains and data structures

In blockchains, each block contains the hash of the previous one: modifying a block changes its hash and breaks the chain. Merkle trees, built on nested hashes, make it possible to verify a single element of a large set without downloading all of it.

A special case: passwords

General-purpose hashes such as SHA-256 are designed to be fast. For storing passwords this is a weakness: an attacker who steals the database can try billions of combinations per second. Passwords call for deliberately slow, salted functions such as Argon2, scrypt or bcrypt. A plain SHA-256, even with a salt, is not enough.

In short

  • A hash function produces a fixed-length fingerprint that changes completely with the slightest change to the data.
  • It must resist preimage, second-preimage and collision attacks.
  • MD5 and SHA-1 are obsolete; today SHA-2, SHA-3 or BLAKE2/BLAKE3 are used.
  • Hashes underpin file checks, digital signatures, forensic evidence, HMAC and blockchains.
  • Passwords require dedicated slow functions, not fast hashes.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also