RMRM Full Stack & AI Engineer · All guides · Roadmaps
Security · guide

Hashing vs Encryption

Hashing and encryption are both fundamental cryptographic techniques used to protect data, but they serve entirely different purposes. Understanding the distinction is critical for building secure systems, storing passwords correctly, and transmitting sensitive information safely.

What Is Hashing?

Hashing is a one-way transformation that converts any input data into a fixed-length string called a hash or digest. Common algorithms include SHA-256, SHA-3, and bcrypt. Because hashing is deterministic and irreversible, the same input always produces the same hash, but you cannot mathematically reconstruct the original input from the hash alone. It is used primarily for data integrity checks and secure password storage.

What Is Encryption?

Encryption is a two-way process that transforms plaintext into unreadable ciphertext using a key, and can be reversed with the correct key to recover the original data. It comes in two main forms: symmetric encryption (e.g., AES), where the same key encrypts and decrypts, and asymmetric encryption (e.g., RSA), which uses a public/private key pair. Encryption is designed for confidentiality — protecting data in transit or at rest so only authorized parties can read it.

Key Differences at a Glance

The defining difference is reversibility: encryption is reversible with a key, hashing is not reversible at all. Encryption preserves the ability to recover the original data, while hashing deliberately destroys it. Another difference is output size: encryption output scales with input size, whereas a hash always produces a fixed-length digest regardless of input length.

When to Use Each

Use hashing when you need to verify data without ever needing the original value back — for example, storing user passwords, verifying file integrity, or generating checksums. Use encryption when the original data must be retrievable by an authorized party — for example, storing credit card numbers, encrypting messages, or securing API tokens. Mixing these up, such as encrypting passwords instead of hashing them, is a common and serious security mistake.

Gotcha: Hashing Passwords Correctly

Not all hash functions are suitable for passwords. Fast general-purpose hashes like MD5 and SHA-256 can be brute-forced at billions of guesses per second using modern GPUs. Instead, always use a purpose-built, slow password-hashing algorithm such as bcrypt, scrypt, or Argon2, which are designed to be computationally expensive and include a built-in salt to prevent rainbow table attacks. Never store plaintext passwords or use raw MD5/SHA for password hashing in any production system.

Best Practice: Combine Both When Needed

Real-world systems often use hashing and encryption together. For example, TLS uses asymmetric encryption to exchange keys, symmetric encryption to protect the data stream, and hashing (via HMAC) to verify message integrity. Hashing ensures data has not been tampered with, while encryption ensures it cannot be read by unauthorized parties — they are complementary, not competing tools. Always choose algorithms that are currently considered cryptographically strong and avoid deprecated ones like MD5, SHA-1, or DES.

Go deeper with an AI tutor that teaches this in context — and quizzes you on it.
Open the app — free to start

© RM Full Stack & AI Engineer · All guides · Roadmaps · Open the app