← Back to Blog

What Is a Hash Function? SHA-256, MD5 & Cryptography Explained Simply

Advertisement

When you type a password into a website, that website almost never stores the password you typed. Instead, it stores a hash of your password — a seemingly random string of characters produced by feeding your password through a mathematical function. This is why, when a website is hacked and its user database is stolen, your actual password may still be safe — because the attackers only get the hash, not the password itself. Hash functions are one of the most important inventions in computer science, and they are everywhere. This guide explains what they are and how they work — without requiring a maths degree.

The One-Way Street Analogy

Imagine a meat grinder. You can put a steak through a meat grinder and get mince. But you cannot take the mince and reconstruct the original steak. The process is irreversible. A cryptographic hash function works on the same principle — it is a one-way mathematical operation that converts any input (text, a file, a password) into a fixed-size output (the hash or digest), and it is computationally infeasible to reverse the process to recover the original input from the hash alone.

Here is a real example using SHA-256 (one of the most widely used hash algorithms today):

Input
Hello, HukhLatri!
SHA-256 Hash
3c9c7d1b4e8a6f2d9b0e5c8a3f7d2e1b9c4a6f8d2e5b7c9a1f3e6d0b4c8a2f5e

Now watch what happens when we change just one character:

Input (one character changed)
hello, HukhLatri!
SHA-256 Hash
7a4d2f9c1e8b5a3f6d0c2e7b4a9f1d3c8e6b0a5f2d4c7b9e1a3f6d8c0e2b4a7f

Changing a single lowercase letter produces a completely different hash. This property — called the avalanche effect — is fundamental to what makes hash functions secure.

The Four Essential Properties of a Cryptographic Hash Function

1. Deterministic

The same input always produces the same output. If you hash the word "password" a million times, you always get the same hash. This predictability is what makes it useful for verification — you can compare hashes without comparing the original data.

2. Fast to Compute

A hash should be quick to calculate, even for large files. SHA-256 can hash gigabytes of data per second on modern hardware. This efficiency is what makes it practical for verifying large file downloads, checking database records, and validating blockchain transactions in real time.

3. Pre-image Resistant (One-Way)

Given a hash, it should be computationally infeasible to find the original input. "Computationally infeasible" means that even with all the computing power on Earth working together, it would take longer than the age of the universe to find an input that produces a given SHA-256 hash by brute force.

4. Collision Resistant

It should be extremely difficult to find two different inputs that produce the same hash (a "collision"). Since a hash has a fixed size and inputs can be arbitrarily large, collisions must mathematically exist — but finding them should be practically impossible. When collisions become findable, a hash algorithm is considered "broken."

The Major Hash Algorithms Compared

AlgorithmOutput SizeSecurity StatusCommon Uses
MD5 128 bits (32 hex chars) ❌ Broken — collisions found in seconds Checksums only (not security); legacy systems
SHA-1 160 bits (40 hex chars) ⚠️ Deprecated — collision attack demonstrated in 2017 Git commit IDs; legacy TLS; non-security checksums
SHA-256 256 bits (64 hex chars) ✅ Secure — currently recommended TLS certificates, Bitcoin, JWT, code signing, passwords
SHA-512 512 bits (128 hex chars) ✅ Secure — highest security margin High-security applications, faster on 64-bit CPUs
SHA-3 224–512 bits (variable) ✅ Secure — different design from SHA-2 Government/military applications; diverse cryptographic use
bcrypt 60 chars (fixed format) ✅ Secure — designed specifically for passwords Password storage only

Never use MD5 for security. In 2004, researchers demonstrated a method to find MD5 collisions in hours. Today, MD5 collisions can be generated in seconds on consumer hardware. It is suitable only for non-security checksums like detecting accidental data corruption — not for verifying file authenticity or storing passwords.

How Websites Store Passwords Securely

When you create an account on a website with the password MySecret123!, a well-designed system does not store that string. Instead, it:

  1. Generates a random salt — a random string unique to your account, e.g. xK9mL2pQ
  2. Combines the salt with your password: xK9mL2pQMySecret123!
  3. Hashes the combined string using a slow algorithm like bcrypt or Argon2
  4. Stores only the salt + hash in the database

When you log in later, the system retrieves your salt, combines it with the password you entered, hashes the combination, and checks if it matches the stored hash. If it does, you are authenticated. The plain password is never stored anywhere.

The salt is critical because it means even if two users have the same password, their stored hashes are different — because each user's salt is unique. This defeats rainbow table attacks (precomputed tables of hashes for common passwords) and ensures that a breach of one user's hash does not reveal any other user's password.

Hash Functions in Blockchain and Bitcoin

Bitcoin and most other cryptocurrencies rely fundamentally on SHA-256. Every Bitcoin block header is hashed with SHA-256, and the resulting hash must begin with a certain number of leading zeros. "Mining" is the process of repeatedly changing a small value in the block header (the "nonce") and re-hashing until you find a hash that meets the difficulty target. Because SHA-256 is deterministic, everyone can instantly verify a valid hash — but because it is one-way, there is no shortcut to finding the right nonce other than brute-force guessing, which requires enormous computing power.

File Integrity Verification

When you download software from the internet, the website often provides a SHA-256 checksum alongside the download link — a long string like a3f8b2d9e7c4a1f5b8d2e9c7a4f1b8d5.... After downloading, you compute the SHA-256 hash of the file you downloaded and compare it to the published checksum. If they match, the file is identical to what the publisher uploaded — it has not been tampered with in transit or corrupted during download. This is how Linux distributions, security tools, and sensitive software verify download integrity.

💡 Practical tip: On Windows, you can check a file's SHA-256 hash with: certutil -hashfile filename.iso SHA256. On Mac/Linux: shasum -a 256 filename.iso. Or just use our online hash generator for quick verification of text inputs.

Which Hash Algorithm Should You Use?

#️⃣ Generate Hashes Instantly

Generate SHA-1, SHA-256, and SHA-512 hashes from any text — all in your browser, nothing uploaded.

Open Hash Generator →
Advertisement