Blog | G5 Cyber Security

Password Hash Match?

TL;DR

No, a password should never be the same as its hash. A hash is a one-way function – you can’t get the original password back from the hash. If they match, your system is seriously compromised.

Why Passwords and Hashes Must Be Different

Hashing is used to store passwords securely. Instead of saving your actual password (which would be a huge security risk if stolen), systems save a hash of the password. Here’s why they need to stay separate:

How Hashing Works (Simplified)

Imagine a blender. You put in your password (the fruit), and it makes a smoothie (the hash). You can’t un-blend the smoothie back into the original fruit.

Checking Passwords – How It Should Work

  1. User Enters Password: The user types their password.
  2. Hash the Entered Password: The system takes the entered password and runs it through the same hash function used to store passwords.
  3. Compare Hashes: The newly created hash is compared to the stored hash in the database.
  4. Match? Access Granted! If the hashes match, the user is authenticated.

Here’s a simple Python example (using bcrypt – a good hashing algorithm):

import bcrypt

password = "mysecretpassword".encode('utf-8')
hashed_password = bcrypt.hashpw(password, bcrypt.gensalt())

# Later, when checking the password:
entered_password = "mysecretpassword".encode('utf-8')
if bcrypt.checkpw(entered_password, hashed_password):
    print("Password matches!")
else:
    print("Incorrect password.")

Notice we’re not comparing the entered password to hashed_password directly; we’re using bcrypt.checkpw() which handles the hashing and comparison safely.

What if a Password *Is* The Same as Its Hash?

  1. Immediate Investigation: This is a critical security breach.
  2. Compromised System: Your system has been compromised, likely due to poor coding practices or a vulnerability in your hashing implementation.
  3. Reset All Passwords: Force all users to reset their passwords immediately.
  4. Review Code: Thoroughly review the code responsible for password storage and authentication. Look for direct comparisons of passwords to hashes.
  5. Security Audit: Consider a professional security audit to identify other potential vulnerabilities.

Preventing This Issue

Exit mobile version