Blog | G5 Cyber Security

Secure Server Passwords: Best Practices

TL;DR

Don’t store passwords directly on your server, even encrypted! Use a strong password hashing function (like bcrypt or Argon2) with salts. Store the hashes and salts securely. Consider using a dedicated secrets management system for extra protection.

Why You Shouldn’t Store Passwords Directly

Even if you encrypt passwords, it’s risky. Encryption can be broken, keys can be stolen, or vulnerabilities discovered. Hashing is designed to be one-way – you can’t get the original password back from the hash.

Step-by-Step Guide

  1. Choose a Strong Password Hashing Function:
  • Generate Salts:
  • A salt is a random string added to each password before hashing. This prevents attackers from using pre-computed tables of common passwords (rainbow tables). Each user should have a unique salt.

    # Example Python with bcrypt
    salt = os.urandom(16)
    pwd_hash = bcrypt.hashpw(password.encode('utf-8'), salt)
    
  • Hash the Passwords:
  • Use your chosen hashing function to create a hash of each password, combined with its unique salt.

    # Example PHP with Argon2
    $hash = password_hash($password, PASSWORD_ARGON2ID);
    
  • Store the Hashes and Salts:
  • Password Verification:
  • When a user logs in:

    1. Retrieve the salt for that user from the database.
    2. Hash the entered password using the retrieved salt.
    3. Compare the resulting hash with the stored hash. If they match, the password is correct.
    # Example Python with bcrypt
    hashed_password = bcrypt.hashpw(entered_password.encode('utf-8'), retrieved_salt)
    if hashed_password == stored_hash:
      # Password matches!
    
  • Secrets Management Systems (Recommended):
  • Regularly Review and Update:
  • Important Considerations

    Exit mobile version