Hash Functions

박서경박서경
3 min read

🔐 Hash Functions Explained: The Backbone of Modern Cryptography

Cryptographic hash functions are the unsung heroes of security. They're not just for password storage—they power digital signatures, secure messaging, software integrity, blockchain, intrusion detection, and much more.


🧠 What Is a Hash Function?

A hash function is a deterministic algorithm that takes any-length input and produces a fixed-length output, typically 256 or 512 bits.

Input (any size) → Hash()Output (fixed size)

Properties of good hash functions:

  • Fast

  • Deterministic

  • Avalanche effect (small change → drastic output change)

  • Irreversible (preimage resistance)

  • Collision-resistant


⚙️ Real-World Use Cases of Hash Functions

DomainUse
Digital SignaturesSign the hash of a document instead of the full document for speed and integrity
Password StorageStore Hash(password) with salt, not the raw password
Blockchain / BitcoinUse SHA-256 for mining and block integrity
TLS/SSL, SSH, IPSecEnsure message integrity in encrypted communications
Software UpdatesVerify update integrity with signed hash
Version Control (e.g., Git)Identify commits and files by hash (SHA-1 or SHA-256)
ForensicsProve files haven't changed using hashes
DeduplicationCloud storage systems use hashes to detect identical files
Digital CertificatesCertificates contain hashes for data verification
Proof-of-Work (e.g., Bitcoin)Find a nonce such that `Hash(data

🛡️ Key Security Properties

1️⃣ Preimage Resistance

Given H, it’s hard to find M such that Hash(M) = H
🔒 Used for:

  • Secure password verification

  • Proof-of-knowledge schemes

  • Commitment schemes

2️⃣ Second Preimage Resistance

Given M₁, it’s hard to find M₂ ≠ M₁ such that Hash(M₁) = Hash(M₂)
🔒 Used for:

  • Digital signature integrity (forged messages must not have same hash)

3️⃣ Collision Resistance

Find M₁ ≠ M₂ with Hash(M₁) = Hash(M₂) → very hard
🔒 Used for:

  • Signing software

  • Preventing file forgery

  • Blockchain consistency

⏱ Collision attack complexity: ~2ⁿ⁄² for n-bit hash (e.g., 128-bit security needs 256-bit hash)


⚔️ How Can Hash Functions Go Wrong?

🚨 Length-Extension Attack

Affects Merkle–Damgård constructions (e.g., SHA-1, SHA-2)

  • Attacker knows Hash(M) and can compute Hash(M || pad || X)
    💡 Solution: Use HMAC, or SHA-3 / BLAKE2 which are safe

🏗️ Hash Function Families

🔷 SHA-1 (Don’t use ❌)

  • 160-bit output

  • Broken in 2005; real collision in 2017 (SHAttered.io)

🔷 SHA-2 (SHA-224, SHA-256, SHA-384, SHA-512)

  • Still widely used

  • Strong security

  • Used in TLS, Bitcoin, certificates

🔷 SHA-3 (Keccak)

  • Sponge construction

  • No length-extension issue

  • Includes SHA3-256 and SHAKE128/256 (XOFs)

🔷 BLAKE2 (⚡ Faster and Safer)

  • Faster than SHA-2

  • Resistant to length-extension

  • Supports keyed hashing

  • Used in Argon2 (password hashing), IPFS, libsodium


🔬 Internal Designs

DesignExplanation
🔁 Merkle–DamgårdProcesses message blocks iteratively using a compression function
🧽 Sponge (e.g., SHA-3)Absorbs message into a state, permutes, then squeezes output
🔐 Davies–MeyerCompression based on block cipher + XOR

🧪 Attacks Recap

AttackAffectsSolution
Length ExtensionSHA-1, SHA-2Use HMAC or SHA-3
Collision AttackMD5, SHA-1Use SHA-2/3, BLAKE2
Preimage AttackRareSHA-256 still secure (2²⁵⁶ complexity)
Proof-of-Storage bypassMD / SHAUse `Hash(C

✅ What Should You Use?

Use CaseRecommended Hash
General purposeSHA-256 or BLAKE2
HMAC-based authHMAC-SHA-256
Secure key derivationBLAKE2b or Argon2
New protocolsSHA-3, SHAKE128/256
High-speed hashingBLAKE2bp (multi-core)

📌 Summary

  • Hash functions are everywhere: from secure messaging to cryptocurrency.

  • Understand the security properties and use the right hash.

  • Prefer SHA-2, BLAKE2, or SHA-3 in modern systems.

  • Don’t use MD5 or SHA-1 for security.

0
Subscribe to my newsletter

Read articles from 박서경 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

박서경
박서경