DevToolbox
All guidesDevToolbox guide

Hash Functions Explained: SHA-256, SHA-1 and Beyond

Understand cryptographic hash functions, the SHA-2 family, collision resistance, rainbow tables, and why password storage demands specialized algorithms.

What Is a Cryptographic Hash Function?

A cryptographic hash function takes an arbitrary-length message and processes it into a fixed-length string of bits (a digest). Hash functions serve as the digital fingerprint of data in modern computer science, forming the foundational bedrock for digital signatures, message authentication codes (HMAC), Git commit trees, and cryptographic blockchains.

Key Properties of Secure Hash Functions

  1. Deterministic: Feeding the exact same message will always yield the exact same digest.
  2. Pre-image Resistance (One-Way): Given a digest H, it should be computationally infeasible to find an input m such that hash(m) = H.
  3. Second Pre-image Resistance: Given an input m1, it is infeasible to find a distinct input m2 such that hash(m1) = hash(m2).
  4. Collision Resistance: It must be infeasible to locate any two arbitrary inputs that map to the identical digest.
  5. Avalanche Effect: Changing even a single bit in the input message completely changes the resulting hash output (typically flipping ~50% of digest bits).

The Evolution of the SHA Family

The Secure Hash Algorithms are a family of cryptographic hash functions published by the National Institute of Standards and Technology (NIST):

  • SHA-1 (160-bit): Published in 1995. In 2017, researchers at CWI Amsterdam and Google announced the SHAttered attack, demonstrating practical collision generation. SHA-1 is deprecated for all cryptographic signing.
  • SHA-2 Family (224, 256, 384, 512 bits): Published in 2001. SHA-256 and SHA-512 remain unbroken and are universally trusted across TLS certificates, HTTPS encryption, Bitcoin mining, and secure code signing.
  • SHA-3 (Keccak): Released in 2015 based on the sponge construction. SHA-3 provides an alternative cryptographic internal structure completely different from the Merkle-Damgård construction used by SHA-2.

Why Plain Hashes Fail for Password Storage

A frequent beginner security flaw is hashing user passwords with fast algorithms like MD5 or SHA-256. Modern consumer graphics cards (GPUs) can compute hundreds of billions of SHA-256 hashes per second. Using precomputed rainbow tables or dictionary attacks, attackers can crack leaked SHA-256 passwords in seconds.

To safely hash passwords, modern engineering standards mandate adaptive, memory-hard key derivation functions:

  • Argon2id: Winner of the Password Hashing Competition; the current gold standard recommended by OWASP.
  • bcrypt: Battle-tested, built-in blowfish-based algorithm with adjustable work factors.
  • PBKDF2: NIST-standardized key derivation function using repeated iterations with salt.

Frequently Asked Questions About Hashes

Can you reverse a hash to find the original text?

No. A cryptographic hash is an irreversible mathematical one-way operation. Because information is compressed down to a fixed-size digest (e.g., 256 bits for a gigabyte of data), the pigeonhole principle guarantees infinite inputs map to identical digest sizes. Instead of "reversing" the algorithm, attackers compare hashes against massive precomputed databases called rainbow tables or execute brute-force dictionary attacks.

What is a salt and why is it mandatory for passwords?

A cryptographic salt is a unique, cryptographically random string appended to each user's password prior to hashing. Salting ensures that even if two users choose the identical password (e.g., "password123"), their resulting stored hashes will be completely different. This completely neutralizes rainbow tables and forces attackers to crack each password individually.

What is an HMAC (Hash-Based Message Authentication Code)?

An HMAC (RFC 2104) is a mechanism for message authentication using cryptographic hash functions combined with a shared secret key. While a plain hash verifies data integrity (ensuring a file was not corrupted during transit), an HMAC verifies both integrity and authenticity (guaranteeing that the message was authored by someone in possession of the private secret key).

HMAC vs. Digital Signatures

HMAC relies on symmetric cryptography: both the sender and receiver share the exact same secret key. If the key is leaked, any party can forge messages. Digital signatures (such as RSA or ECDSA) utilize asymmetric public/private key cryptography, where only the author possesses the signing private key, while anyone with the public key can verify authenticity without gaining the ability to forge signatures.

Try the related tool

SHA Hash Generator

More guides