Bcrypt: The Secret Weapon for Securing Passwords

Krishanu Ghosh
5 min readJul 9, 2024

--

Greetings!!!

I recently had the opportunity to implement the bcrypt algorithm in an open-source, cloud-agnostic resource manager. My goal was to find a more secure hashing algorithm than MD5 or SHA-256. After extensive research, I discovered bcrypt. This journey provided me with valuable insights into the workings of bcrypt, its advantages over traditional hashing algorithms, and why it’s a superior choice for password security.

I’m excited to share these insights with you and explain why bcrypt was the ideal solution for enhancing security in my project. So let’s dive into the details of bcrypt and explore its benefits together.

What is Bcrypt?

Bcrypt is a password hashing algorithm. But what does that mean? Think of hashing like scrambling a word so much that it turns into a completely different string of characters. Unlike encryption, which can be reversed with the right key, hashing is a one-way street. Once hashed, the original password can’t be recovered, making it a secure way to store passwords.

How is Bcrypt Different from Usual Hashing Algorithms?

Now, you might be wondering, “Aren’t all hashing algorithms the same?” Well, not quite. Here’s how bcrypt is different from typical hashing algorithms like MD5 or SHA-256:

  1. Salted Hashing: While many hashing algorithms don’t automatically include a salt, bcrypt always does. This means even identical passwords will have different hashes, providing an extra layer of security.
  2. Adaptive Work Factor: Bcrypt allows you to adjust its complexity. This means you can increase the number of hashing rounds, making it more secure as computing power grows. Other algorithms don’t offer this adaptability.
  3. Built-in Slow Hashing: Bcrypt is intentionally slow to thwart brute force attacks. In contrast, algorithms like MD5 and SHA-256 are designed for speed, which is great for quick computations but terrible for password security.

How Does Bcrypt Work?

Bcrypt uses an algorithm called the Blowfish cipher to turn your password into a hashed value. Here’s a simplified breakdown of what happens when you use bcrypt:

  1. Salting: Bcrypt starts by adding a unique, random string (called a salt) to your password. This ensures that even if two users have the same password, their hashed passwords will be different.
  2. Hashing: It then hashes the salted password. This process involves multiple rounds of computation, which makes it slower to produce the hash. While “slow” might sound bad, it’s actually a key feature for security.
  3. Storage: The resulting hash, along with the salt, is stored in the database.

Now you might ask “If identical passwords have different hashes due to salting, how does bcrypt verify that the entered password is correct?“

Great question! Understanding how password verification works with bcrypt is essential to see its full power in action. Here’s an explanation:

How Password Verification Works with Bcrypt

When you create an account and set a password, bcrypt performs the following steps:

  1. Generate a Salt: Bcrypt generates a unique salt for your password.
  2. Hash the Password with the Salt: The password combined with the salt is hashed.
  3. Store the Hash and the Salt: The resulting hash, along with the salt, is stored in the database.

Now, when you log in, bcrypt follows a similar process to verify your password:

  1. Retrieve the Stored Hash and Salt: When you attempt to log in, the system retrieves the stored hash and the salt associated with your account from the database.
  2. Hash the Entered Password with the Retrieved Salt: Bcrypt takes the password you entered during login, combines it with the retrieved salt, and hashes this combination again.
  3. Compare the Hashes: The newly generated hash is then compared with the stored hash. If they match, the password is correct; otherwise, it’s incorrect.

Why This Works

  • Unique Salt for Each Password: The salt ensures that even if two users have the same password, their hashes will be different. This makes it extremely difficult for attackers to use precomputed hash tables (rainbow tables) to crack passwords.
  • Rehashing with the Same Salt: When verifying a password, bcrypt uses the same salt stored in the database, ensuring the hash generated during login matches the stored hash if the entered password is correct.

Example Walkthrough

Let’s go through a simplified example:

  1. Account Creation:
  • Password: mypassword
  • Salt: randomsalt123
  • Hashed Password: hash(mypassword + randomsalt123) = a1b2c3d4

2. The database stores:

  • Hashed Password: a1b2c3d4
  • Salt: randomsalt123

3. Login Attempt:

  • Entered Password: mypassword
  • Retrieve Stored Salt: randomsalt123
  • Hash Entered Password: hash(mypassword + randomsalt123)

4. If the resulting hash is a1b2c3d4, the entered password is correct.

Why is Bcrypt Better?

So, why all the fuss about bcrypt? Here’s why it stands out:

  1. Protection Against Rainbow Tables: A rainbow table is a precomputed table of hashes used to crack passwords. Salting ensures that each hash is unique, making rainbow tables ineffective.
  2. Slow Hashing: Bcrypt’s deliberate slowness is a huge plus. Fast algorithms are great for efficiency but terrible for security because they make it easier for hackers to perform brute force attacks (trying all possible passwords). Bcrypt’s slowness makes these attacks impractical.
  3. Adaptive Hashing: As computing power increases, bcrypt can be adjusted to remain secure by increasing the number of hashing rounds. This adaptability ensures it stays robust over time.

Why Should You Care?

Okay, some of you might be thinking, “I’m not a developer. Why should I care about bcrypt?” Here’s why:

  • Trustworthy Services: When you sign up for an online service, knowing they use bcrypt means they take your security seriously. If a company doesn’t mention their password storage methods, be cautious.
  • Password Managers: Many password managers use bcrypt to securely store your passwords. This means even if someone hacks into your password manager, they won’t get your actual passwords.
  • Future-Proof Security: Understanding tools like bcrypt helps you make better choices about the platforms you use. It’s a sign of a service that’s thinking ahead about security.

What I think

In summary, bcrypt isn’t just a tool; it’s a mindset towards better security. By choosing bcrypt, I feel more equipped to protect sensitive data and more confident in the integrity of the systems I develop. For anyone serious about security, bcrypt is undoubtedly the way to go.

In a world where data breaches are becoming all too common, bcrypt is a powerful ally in keeping your information secure. By understanding how it works and why it’s important, you’re taking a step toward better digital hygiene. So next time you create a password, remember the superhero working behind the scenes to keep it safe — bcrypt!

--

--