DEV Community

Cover image for How to store password in Database
ZeeshanAli-0704
ZeeshanAli-0704

Posted on

How to store password in Database

Storing passwords securely in a database is critical for maintaining the integrity and security of a system. Here are key methods and best practices for password storage in terms of system design:

1. Use Strong Hashing Algorithms

  • Hashing: Store the hashed version of the password instead of the plaintext password. Use strong, cryptographic hashing algorithms like:
    • bcrypt: Designed specifically for hashing passwords and includes a salt automatically.
    • Argon2: The winner of the Password Hashing Competition (PHC), designed to resist GPU cracking.
    • PBKDF2: Uses a pseudorandom function (such as HMAC) and is configurable to be slow.

2. Salting Passwords

  • Salting: Add a unique, random salt to each password before hashing to prevent rainbow table attacks.

    • Ensure each password is concatenated with a unique, random string (salt) before hashing. Store the salt in the database alongside the hashed password.

3. Pepper

  • Peppering: Add a static secret value (pepper) to all passwords before hashing. The pepper should be kept secret and stored separately from the database, often in application code or environment variables.

Key Outcomes

  • Choose hashing algorithms that are intentionally slow (like bcrypt, Argon2, or PBKDF2) to make brute-force attacks more difficult.

  • Use techniques like PBKDF2, bcrypt, or Argon2, which internally apply the hashing function multiple times to increase computation time.

  • Store salts in the same database table as the hashed passwords. Salts do not need to be secret but should be unique per password.

  • Ensure your database and application environment are secure:
    Use HTTPS for data transmission.
    Apply database access controls and encryption.
    Regularly update and patch systems to fix vulnerabilities.

  • Conduct regular security audits and code reviews to ensure password storage and handling follow best practices.

Conclusion

Implementing these methods ensures that even if an attacker gains access to the database, the passwords remain protected. Proper hashing, salting, and environment security practices are fundamental to secure password storage in system design.

More Details:

Get all articles related to system design
Hastag: SystemDesignWithZeeshanAli

Git: https://github.com/ZeeshanAli-0704/SystemDesignWithZeeshanAli

Top comments (0)