Introduction

Welcome to the lesson on secure password storage in our course! In this lesson, we will explore the critical role of key derivation functions (KDFs) in protecting passwords. Building on our previous discussions about cryptographic failures, we'll focus on how improper password storage can lead to vulnerabilities.

Let's dive in and learn how to safeguard passwords effectively! 🔐

Password Storage and Security Risks

Password storage is a crucial aspect of web application security. When users create accounts, their passwords must be stored securely to prevent unauthorized access. When passwords are stored without proper hashing techniques, several critical vulnerabilities emerge:

  1. Plain Text Storage: Storing passwords in plain text means anyone with database access can immediately see all user passwords.

  2. Simple Hashing Without Salt: Using basic hash functions like MD5 or SHA-256 without salt creates predictable outputs for identical inputs.

  3. Fast Hashing Algorithms: Algorithms designed for speed (like SHA-256) allow attackers to attempt millions of password combinations per second.

  4. Lack of Salt: Without unique salts, identical passwords produce identical hashes, enabling efficient attacks against multiple accounts simultaneously.

Key derivation functions (KDFs) are essential tools that transform passwords into secure hashes, making it difficult for attackers to reverse-engineer the original passwords.

Now that we understand the importance of proper password storage and the risks of inadequate protection, let's examine some concrete examples of vulnerable implementations.

Insecure Password Storage

Suppose you have an API key that you need to hash before storing in your database. A common but insecure approach would be to use a fast cryptographic hash function like SHA-256, which is designed for speed rather than password storage.

When verifying an API key with this approach, we would simply generate a new hash and compare it to the stored hash:

This verification method highlights the vulnerability - identical inputs always produce identical outputs, making the system predictable and easier to attack.

Let's explore how attackers can exploit these weaknesses.

Exploiting the Vulnerability

Without proper salting and slow hashing algorithms, attackers can employ several effective techniques:

  1. Rainbow Table Attacks: Pre-computed tables allow attackers to instantly look up common password hashes. The hash for "password" would be immediately identified from these tables.

  2. Brute Force Attacks: Using specialized hardware, attackers can test billions of password combinations per second against unsalted SHA-256 hashes.

  3. Identical Hash Detection: Without salts, identical passwords produce identical hashes. If two users have the same password, they'll have the same hash, allowing attackers to compromise multiple accounts once one password is cracked.

In a real-world breach of a database with unsalted SHA-256 hashes, an attacker could crack most common passwords in minutes to hours, rather than the years it would take with proper KDFs. Fortunately, there are robust solutions available to prevent these attacks.

Secure Example: Password Storage with KDFs

To protect against these vulnerabilities, we should use proper key derivation functions like bcrypt, Argon2, or PBKDF2. These functions automatically incorporate salting and work factor adjustments to make password cracking computationally expensive.

BCrypt is an industry-standard key derivation function specifically designed for password hashing. It automatically handles salt generation and includes a work factor parameter that allows you to adjust the computational cost as hardware becomes more powerful.

This implementation uses bcrypt with automatic salting and an adjustable cost factor. The salt ensures that even identical passwords produce different hashes, while the work factor makes brute force attacks prohibitively expensive.

For verification, bcrypt provides a built-in comparison function:

The verifyApiKey function securely compares the provided API key with the stored hash without revealing any information that could help attackers.

Note that while KDFs significantly increase the security of password storage, they cannot protect against fundamentally weak passwords - proper password selection by users remains crucial for overall system security.

Conclusion and Next Steps

In this lesson, we've explored the critical importance of using key derivation functions (KDFs) for secure password storage. We've seen how the absence of proper salting and slow hashing algorithms can lead to catastrophic security breaches, allowing attackers to quickly compromise user accounts. By implementing solutions like bcrypt, you can significantly enhance the security of your applications and protect sensitive user data from unauthorized access.

As you move on to the practice exercises, you'll have the opportunity to apply these concepts and enhance your skills in web application security. Keep up the great work, and continue exploring the fascinating world of cryptography! 🎉

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal