How Databases Keep Passwords Secure: Hashing, Salting, and Beyond

Listen to this Post

In this article, Neo Kim explains the mechanisms databases use to store passwords securely. Here’s a breakdown of the process:

  1. Hashing: The server transforms the password using a hash function to create a unique fingerprint. This fingerprint is stored in the database instead of the actual password.
  2. One-Way Function: Hash functions are one-way, meaning the password cannot be reverse-engineered from the fingerprint.
  3. Rainbow Table Attacks: To counter rainbow table attacks (pre-computed tables of hash values), a unique salt is added to the password before hashing. This salt is stored alongside the fingerprint in the database.
  4. Brute-Force Protection: To slow down brute-force attacks, the same hash function is applied multiple times (key stretching).

Practice-Verified Commands and Code:

  • Generating a Salt and Hash in Python:
    import hashlib
    import os</li>
    </ul>
    
    def generate_salt():
    return os.urandom(16) # 16 bytes salt
    
    def hash_password(password, salt):
    return hashlib.pbkdf2_hmac('sha256', password.encode(), salt, 100000)
    
    password = "securepassword123"
    salt = generate_salt()
    hashed_password = hash_password(password, salt)
    print(f"Salt: {salt.hex()}")
    print(f"Hashed Password: {hashed_password.hex()}")
    
    • Using bcrypt in Node.js:
      const bcrypt = require('bcrypt');
      const saltRounds = 10;</li>
      </ul>
      
      const password = "securepassword123";
      bcrypt.hash(password, saltRounds, function(err, hash) {
      console.log(<code>Hashed Password: ${hash}</code>);
      });
      
      • Linux Command to Generate a Random Salt:
        openssl rand -hex 16
        

      What Undercode Say:

      Password security is a cornerstone of modern system design, and understanding how databases protect sensitive information is crucial. Hashing ensures that passwords are not stored in plaintext, while salting adds an extra layer of security against rainbow table attacks. Techniques like key stretching (e.g., bcrypt, PBKDF2) further slow down brute-force attempts, making it computationally expensive for attackers to crack passwords.

      For developers, implementing these practices is straightforward with libraries like bcrypt, Argon2, or PBKDF2. Always ensure that salts are unique and securely stored alongside hashed passwords. Additionally, consider using cryptographic peppers (secret keys stored outside the database) for enhanced security.

      In Linux, tools like `openssl` can generate secure salts, while scripting languages like Python and Node.js provide robust libraries for hashing and salting. As quantum computing advances, staying updated with post-quantum cryptographic algorithms will be essential.

      For further reading on secure password storage, check out these resources:
      OWASP Password Storage Cheat Sheet
      Bcrypt Documentation
      Argon2: The Winner of the Password Hashing Competition

      By following these best practices, you can ensure that your systems remain resilient against evolving security threats.

      References:

      initially reported by: https://www.linkedin.com/posts/nk-systemdesign-one_give-me-2-minutes-and-ill-teach-you-how-activity-7302245396058128386-2B3N – Hackers Feeds
      Extra Hub:
      Undercode AIFeatured Image