DevLab
Crypto

Hash Functions Explained: MD5, SHA-256, and When to Use Each

Learn what hash functions do, why cryptographic hashes are one-way, and which algorithm to use for passwords, checksums, and data integrity.

What is a Hash Function?

A hash function takes any input and produces a fixed-size output called a digest. The same input always produces the same output, but you cannot reverse the process. This one-way property, combined with the avalanche effect (a tiny input change produces a completely different hash), makes hash functions the backbone of passwords, checksums, and digital signatures.

The Three Core Properties

  • Deterministic: SHA-256("hello") always equals the same 64-character hex string
  • Avalanche effect: SHA-256("hello") and SHA-256("Hello") share zero recognizable bits
  • Collision resistant: Computationally infeasible to find two different inputs producing the same hash

MD5 — Fast But Broken for Security

MD5 produces a 128-bit (32 hex character) hash. Researchers demonstrated practical collision attacks — two different files with the same MD5 hash. You can forge a file with the same MD5 as a trusted one.

Acceptable: Non-security checksums (verifying a download was not corrupted), cache keys, database partition keys.

Never use for: Passwords, digital signatures, or any security-critical verification.

SHA-256 — The Current Standard

SHA-256 produces a 256-bit (64 hex character) hash. No practical collision attacks exist. Used in TLS certificates, Bitcoin mining, Git commit hashes, and HMAC API signatures. SHA-256 is fast — good for checksums, bad for passwords (a GPU can compute billions per second).

For Passwords: Use Slow Hash Functions

Password hashing needs a deliberately slow algorithm that GPUs cannot parallelize efficiently:

  • bcrypt — most widely supported; tunable cost factor
  • scrypt — memory-hard; harder to attack with specialized hardware
  • Argon2 — winner of the Password Hashing Competition; the modern best choice

Never use MD5, SHA-1, or SHA-256 directly for passwords, even with a salt.

Practical Uses

  • File integrity: SHA-256 checksums confirm a file was not corrupted or tampered with
  • API signatures: HMAC-SHA256 signs API requests to prevent tampering in transit
  • Content addressing: Git uses SHA-1 (migrating to SHA-256) to address every file and commit
  • Deduplication: Storage systems hash files to find duplicates without comparing byte-by-byte

Frequently Asked Questions

What is a hash function?

A hash function takes an input of any size and produces a fixed-length output (the hash or digest). The same input always produces the same hash, but even a tiny change in the input produces a completely different output. Hash functions are one-way — you cannot reverse a hash back to its original input.

What is the difference between MD5 and SHA-256?

MD5 produces a 128-bit hash and is considered cryptographically broken — collisions can be generated in seconds. SHA-256 produces a 256-bit hash and remains secure for cryptographic use. Use SHA-256 or SHA-3 for anything security-related. MD5 is only acceptable for non-security checksums.

Can two different inputs produce the same hash?

Yes, this is called a collision. All hash functions can theoretically collide because they map infinite inputs to a finite output space. Secure hash functions like SHA-256 make collisions computationally infeasible to find deliberately, while broken ones like MD5 allow collisions to be generated quickly.

Should I use hashing or encryption for passwords?

Use hashing, not encryption. Encryption is reversible (anyone with the key can decrypt), but hashing is one-way. For passwords specifically, use a dedicated password hashing algorithm like bcrypt, scrypt, or Argon2 — never raw SHA-256 or MD5, because they are too fast and vulnerable to brute-force attacks.

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines