sourcecodestack Team
Tools, guides & how-tos
Cryptographic hash functions are one of the foundational building blocks of modern digital security. They’re used in password storage, file integrity verification, digital signatures, blockchain technology, and dozens of other applications. Yet despite being everywhere in software development and security, hash functions are often misunderstood — or worse, misused in ways that create serious security vulnerabilities.
This guide explains how hash functions work, the key differences between MD5, SHA-1, SHA-256, and SHA-512, and how to use them correctly for different purposes.
A cryptographic hash function is a mathematical algorithm that takes an input of any size and produces a fixed-size output called a hash, digest, or checksum.
Input: "Hello, World!"
SHA-256 Output: dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986d
Input: "Hello, World" (no exclamation mark)
SHA-256 Output: 4ae7c3b6ac0beff671efa8cf57386151c06e58ca53a78d83f36107316cec125f
Notice that removing a single character produces a completely different hash. This is one of the defining properties of good hash functions.
The same input always produces the same output. If you hash the string “password” with SHA-256 today and again in five years, you’ll get the identical hash. This is fundamental — it’s what makes hashes useful for verification.
Given a hash output, it is computationally infeasible to work backward to find the original input. This is not encryption — you cannot “decrypt” a hash. There is no key, no reversal algorithm. The only way to find an input that produces a given hash is to try inputs until one matches.
You know: dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986d
You want: The original text
Method: Try every possible input until you find a match
Time: Effectively impossible for a strong hash with a good input
A small change in the input produces a dramatically different output. Changing one bit of the input changes approximately half of the output bits. This makes it impossible to make “small” changes to data that produce predictable changes in the hash.
"The quick brown fox" → a9701d10e20f4a234dfc08b4...
"The quick brown Fox" → 9a09a4... (completely different)
A collision occurs when two different inputs produce the same hash output. A good hash function makes finding collisions computationally infeasible.
Note: Because hash functions map infinite possible inputs to a finite output space, collisions must mathematically exist. The question is whether they can be found in reasonable time. For broken algorithms like MD5, they can be found quickly.
This is the most common misconception about hash functions:
| Property | Hash Function | Encryption |
|---|---|---|
| Reversible? | No | Yes (with key) |
| Fixed output size? | Yes | No (typically same as input) |
| Key required? | No | Yes |
| Primary use | Verification, integrity | Confidentiality |
| Example | SHA-256 | AES-256 |
Encryption converts data into ciphertext that can be decrypted back to the original with the correct key. Hashing is a one-way transformation that cannot be reversed.
Storing passwords as encrypted values is a security mistake — if the encryption key is compromised, all passwords are instantly revealed. Storing passwords as hashes is correct — even if the hash database is stolen, the passwords cannot be directly recovered.
| Property | MD5 | SHA-1 | SHA-256 | SHA-512 |
|---|---|---|---|---|
| Output size | 128 bits (32 hex chars) | 160 bits (40 hex chars) | 256 bits (64 hex chars) | 512 bits (128 hex chars) |
| Speed | Very fast | Fast | Medium | Slower |
| Collision resistance | Broken (2004) | Broken (2017) | Strong | Very strong |
| Pre-image resistance | Weak | Moderate | Strong | Very strong |
| Security status | Cryptographically broken | Cryptographically broken | Secure | Secure |
| Safe for passwords? | No | No | No (use bcrypt) | No (use bcrypt) |
| Safe for file integrity? | Not recommended | Not recommended | Yes | Yes |
| Safe for digital signatures? | No | No | Yes | Yes |
| Used in TLS/SSL? | No (deprecated) | No (deprecated) | Yes | Yes |
Developed in 1991 by Ronald Rivest. For years, MD5 was the standard hashing algorithm for everything from file verification to password storage.
In 2004, researchers demonstrated MD5 collision attacks — the ability to produce two different files with the same MD5 hash. By 2008, researchers created a rogue SSL certificate using MD5 collisions. MD5 is now considered cryptographically broken for any security-sensitive application.
MD5 is still acceptable for:
MD5 is not acceptable for:
Developed by the NSA in 1995. SHA-1 produces a 160-bit hash and was widely used until 2017, when Google’s Project Zero demonstrated the first practical SHA-1 collision (the “SHAttered” attack), producing two PDF files with identical SHA-1 hashes.
Major browsers removed SHA-1 support for TLS certificates in 2017. Git, which historically used SHA-1 to identify commits, is transitioning to SHA-256.
Part of the SHA-2 family, designed by the NSA in 2001. SHA-256 is currently the workhorse of cryptographic hashing — used in TLS certificates, Bitcoin mining, code signing, and file integrity verification.
SHA-256 produces a 256-bit (32-byte) hash, represented as 64 hexadecimal characters. No collision attacks are known against SHA-256.
Also part of SHA-2, SHA-512 produces a 512-bit (64-byte) hash. It offers slightly higher security margins than SHA-256 and can be faster than SHA-256 on 64-bit processors due to architectural differences in how the algorithm processes data.
This is perhaps the most consequential use of hashing. When a user creates a password, the system hashes it and stores the hash — never the plaintext password. On login, the entered password is hashed and compared to the stored hash.
The right way to hash passwords:
Regular hash functions (even SHA-256) are not suitable for password storage because they’re designed to be fast — which is a liability for passwords. An attacker with a stolen hash database and a GPU can try billions of SHA-256 hashes per second.
Password hashing requires purposely slow algorithms:
| Algorithm | Design | Recommended? |
|---|---|---|
| MD5 | Fast general hash | Never |
| SHA-256 | Fast general hash | No |
| bcrypt | Password-specific, slow | Yes |
| scrypt | Password-specific, memory-hard | Yes |
| Argon2 | Password-specific, state-of-the-art | Yes (best choice) |
# Wrong: Using SHA-256 for passwords
import hashlib
hashed = hashlib.sha256(b"mypassword").hexdigest() # INSECURE
# Right: Using bcrypt for passwords
import bcrypt
hashed = bcrypt.hashpw(b"mypassword", bcrypt.gensalt(rounds=12)) # SECURE
Password salting adds a unique random value to each password before hashing, ensuring that two users with the same password have different stored hashes — defeating precomputed rainbow table attacks.
Pro Tip: Use your language’s established password hashing library. Do not implement your own password hashing scheme. PHP has
password_hash(), Python hasbcrypt, Node.js hasbcryptjs. These libraries handle salting automatically and are maintained by security experts.
When you download software, many providers publish a checksum alongside the download — the SHA-256 hash of the legitimate file. After downloading, you compute the hash of your downloaded file and compare it to the published value.
If they match, you have high confidence that:
# Verify a downloaded file on Linux/macOS
sha256sum downloaded-file.iso
# Compare output to the published checksum
# Windows PowerShell
Get-FileHash downloaded-file.iso -Algorithm SHA256
Digital signatures don’t hash the document itself — they hash the document and then encrypt the hash with a private key. Anyone with the corresponding public key can decrypt the hash and verify it matches the document’s actual hash.
Signing: Hash(document) → Encrypt with private key → Signature
Verifying: Decrypt signature with public key → Compare to Hash(document)
This approach is efficient because asymmetric encryption (RSA, ECC) is slow — hashing the document first means you only encrypt a small, fixed-size hash rather than the entire document.
Storage systems use hashes to detect duplicate files. Instead of doing a byte-by-byte comparison of files (slow and storage-intensive), the system computes a hash of each file and compares hashes. Equal hashes indicate likely equal files.
Git uses this principle — every commit, tree, and blob in a Git repository is identified by its SHA hash. This is why Git can detect when files are identical across different directories or branches.
Non-cryptographic hashing powers the hash maps and dictionaries at the core of every programming language. These use much simpler, faster algorithms (MurmurHash, FNV, xxHash) optimized for performance rather than security.
A hash collision occurs when two different inputs produce the same hash output. Because hash functions map infinite inputs to finite outputs, collisions must exist mathematically — the question is how hard they are to find.
Collision attack: Find any two inputs X and Y such that Hash(X) = Hash(Y). This broke MD5 and SHA-1.
Pre-image attack: Given a hash H, find any input X such that Hash(X) = H. This is harder than a collision attack and has not succeeded against SHA-256.
Second pre-image attack: Given an input X, find a different input Y such that Hash(X) = Hash(Y). Even harder than pre-image attacks.
Collisions are mathematically more likely than intuition suggests, due to the birthday paradox: in a group of just 23 people, there’s a 50% chance two people share a birthday, despite there being 365 possible birthdays.
For a hash function with an n-bit output, collisions can be expected after approximately 2^(n/2) hashes — so MD5’s 128-bit output provides only 2^64 collision resistance, not 2^128.
This is one of the most practical applications of hashing for everyday users. Here’s the complete process:
The software provider’s download page will typically list checksums:
ubuntu-22.04-desktop-amd64.iso
SHA-256: a4acfda10b18da50e2ec50ccaf17c1b8b1b647a9b8f1a5c6e2a1f82f4c5b8d9e
Download the file through your browser or download manager.
Linux/macOS:
sha256sum ubuntu-22.04-desktop-amd64.iso
Windows (PowerShell):
Get-FileHash ubuntu-22.04-desktop-amd64.iso -Algorithm SHA256 | Select-Object Hash
Or use our online Hash Generator for quick file verification without command line.
Compare your computed hash character-by-character with the published hash. A single character difference means the files don’t match.
Pro Tip: Many developers write a small script to automate this comparison. But simply eyeballing the first 8 characters and last 8 characters is a pragmatic approach — an attacker would need to produce a file with the exact same hash, which is currently impossible for SHA-256.
When using an online hash generator, privacy is an important consideration:
Our Hash Generator processes all hashing operations entirely in your browser using the Web Crypto API. Your input data never leaves your device and is never transmitted to our servers. This makes it safe for most use cases, but for highly sensitive data, a command-line tool remains the gold standard.
Our tool supports:
SHA-3 (Keccak) was selected by NIST as a new standard in 2012 — not because SHA-2 is broken, but as a backup in case SHA-2 is ever compromised. SHA-3 uses a completely different internal structure (sponge construction) compared to SHA-2’s Merkle-Damgard construction, meaning a breakthrough against SHA-2 would not necessarily affect SHA-3.
SHA-3 is not yet widely deployed but is supported in most modern cryptographic libraries.
| Use Case | Recommended Algorithm |
|---|---|
| Password storage | Argon2id > bcrypt > scrypt |
| File integrity verification | SHA-256 |
| Digital signatures | SHA-256 or SHA-512 |
| TLS/SSL certificates | SHA-256 |
| HMAC authentication | SHA-256 or SHA-512 |
| Non-security checksum | SHA-256 (or MD5 for legacy) |
| Git commits | SHA-256 (transitioning from SHA-1) |
| Blockchain/Bitcoin | SHA-256 |
| Code signing | SHA-256 or SHA-512 |
Hash functions are elegant in their simplicity — a fixed-size fingerprint for data of any size — and powerful in their applications. Understanding the difference between MD5, SHA-1, SHA-256, and purpose-built password hashing algorithms is essential knowledge for any developer working with authentication, file handling, or data integrity.
The key takeaways:
Use our Hash Generator to quickly compute and verify hashes directly in your browser, with full support for SHA-256, SHA-512, and legacy MD5/SHA-1 for compatibility purposes.
sourcecodestack Team
We build free, privacy-first browser tools and write practical guides on how to use them. Everything runs on your device — no uploads, no sign-ups.
Encryption is one of the most powerful tools available for protecting information, and it is more accessible t…
MD5 and SHA 256 both turn any input into a fixed length fingerprint — but treating them as interchangeable is …
Two accounts. Same $10,000. Same 8% rate. Same 30 years. One ends with $24,000 of interest; the other ends wit…