DEV Community

Cover image for Hashing Passwords: Why MD5 and SHA Are Outdated, and Why You Should Use Scrypt or Bcrypt
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Hashing Passwords: Why MD5 and SHA Are Outdated, and Why You Should Use Scrypt or Bcrypt

When it comes to securing user passwords, developers have a responsibility to ensure that sensitive data is stored safely.

Over the years, the tools and techniques for password storage have evolved, and some older methods like MD5 and SHA are no longer considered secure.

In this blog, we’ll explore why MD5 and SHA are outdated, and why modern algorithms like Scrypt and Bcrypt are the go-to choices for password hashing.

Why Hashing is Important

Before diving into the specifics of hashing algorithms, let’s understand why hashing is crucial for password storage:

  • One-Way Function: Hashing is irreversible. Once a password is hashed, it cannot be converted back to its original form.
  • Protection Against Data Breaches: Even if attackers gain access to your database, they cannot easily retrieve the original passwords from the hashes.
  • Password Verification: When a user logs in, their input is hashed and compared to the stored hash. If they match, the password is correct.

Image description

The Problem with SHA and MD5

SHA (Secure Hash Algorithm)

  • What it is: SHA is a family of cryptographic hash functions, including SHA-1, SHA-256, and SHA-512.
  • Why it’s insecure for passwords:
    • Designed for Speed: Like SHA algorithms is designed to be fast, which makes them unsuitable for password hashing. Attackers can use GPUs or specialized hardware to crack SHA hashes efficiently.
    • No Built-in Salting: SHA does not include salting by default, making it vulnerable to rainbow table attacks.
    • Not Password-Specific: SHA was designed for data integrity checks, not for securely storing passwords.

MD5 (Message Digest Algorithm 5)

  • What it is: MD5 is a widely used cryptographic hash function that produces a 128-bit hash value.
  • Why it’s insecure:
    • Vulnerable to Collisions: MD5 is prone to hash collisions, where two different inputs produce the same hash. This makes it easy for attackers to reverse-engineer passwords.
    • Fast Computation: MD5 is designed to be fast, which is a disadvantage for password hashing. Attackers can use brute force or rainbow tables to crack MD5 hashes quickly.
    • Outdated: MD5 has been considered broken since the early 2000s and is no longer recommended for any security-related purposes.

Image description

Modern Password Hashing: Scrypt and Bcrypt

Bcrypt

  • What it is: Bcrypt is a password-hashing function based on the Blowfish cipher. It was designed specifically for password storage.
  • Why it’s secure:
    • Slow Hashing: Bcrypt is intentionally slow, making brute-force attacks computationally expensive.
    • Built-in Salting: Bcrypt automatically generates and appends a unique salt to each password, preventing rainbow table attacks.
    • Configurable Work Factor: The work factor (or cost factor) can be increased to make the hashing process slower as hardware improves.
    • Widely Adopted: Bcrypt has been around since 1999 and is trusted by the security community.

Example of Bcrypt in Action (Node.js):

const bcrypt = require('bcrypt');
const saltRounds = 10;

// Hashing a password
bcrypt.hash('myPassword', saltRounds, function(err, hash) {
  console.log('Hashed Password:', hash);
});

// Verifying a password
bcrypt.compare('myPassword', hash, function(err, result) {
  console.log('Password Match:', result);
});
Enter fullscreen mode Exit fullscreen mode

Scrypt

  • What it is: Scrypt is a password-based key derivation function designed to be memory-intensive, making it resistant to hardware-based attacks.
  • Why it’s secure:
    • Memory-Hard: Scrypt requires a significant amount of memory to compute, which makes it difficult for attackers to use GPUs or ASICs.
    • Configurable Parameters: Scrypt allows you to tune parameters like CPU/memory cost, block size, and parallelism to balance security and performance.
    • Cryptocurrency Adoption: Scrypt is used in cryptocurrencies like Litecoin, which has led to extensive scrutiny and validation of its security.

Example of Scrypt in Action (Node.js):

const scrypt = require('scrypt-js');

const password = Buffer.from('myPassword');
const salt = Buffer.from('uniqueSalt');
const N = 16384; // CPU/memory cost
const r = 8;     // Block size
const p = 1;     // Parallelism
const dkLen = 64; // Output length

scrypt.scrypt(password, salt, N, r, p, dkLen, function(err, derivedKey) {
  console.log('Hashed Password:', derivedKey.toString('hex'));
});
Enter fullscreen mode Exit fullscreen mode

Comparison: Scrypt vs. Bcrypt

Feature Scrypt Bcrypt
Speed Slower due to memory-hard design Slower due to iterative hashing
Memory Usage High Low
Resistance to GPUs Excellent Good
Ease of Use Requires parameter tuning Simple to implement
Adoption Gaining popularity Widely adopted

Best Practices for Password Hashing

  1. Use Modern Algorithms: Always use Scrypt, Bcrypt, or Argon2 for password hashing.
  2. Add a Unique Salt: Ensure each password is hashed with a unique salt to prevent rainbow table attacks.
  3. Increase Work Factors: Adjust the work factor (e.g., Bcrypt’s cost factor or Scrypt’s parameters) to keep up with advancements in hardware.
  4. Avoid Fast Hashes: Never use MD5, SHA-1, or SHA-256 for password storage.
  5. Use a Pepper: Add a secret key (pepper) to the hashing process for an extra layer of security.

Image description

Conclusion

When it comes to password security, using the right hashing algorithm is critical.

While MD5 and SHA are outdated and insecure, modern algorithms like Scrypt and Bcrypt provide robust protection against brute-force and rainbow table attacks.

By following best practices and staying informed about the latest developments in cryptography, you can ensure that your users’ passwords remain safe.

If you’re starting a new project or updating an existing one, make the switch to Scrypt or Bcrypt today.

Your users (and your security team) will thank you!

Further Reading:

Let me know your thoughts in the comments! What hashing algorithm do you use in your projects? 🚀

I’ve been working on a super-convenient tool called LiveAPI.

LiveAPI helps you get all your backend APIs documented in a few minutes

With LiveAPI, you can quickly generate interactive API documentation that allows users to execute APIs directly from the browser.

Image description

If you’re tired of manually creating docs for your APIs, this tool might just make your life easier.

Top comments (0)