Blog | G5 Cyber Security

Stronger Passwords: Custom Hashing

TL;DR

Standard password hashing (like bcrypt or Argon2) is good, but adding a unique salt *per user* and stretching the hash multiple times makes cracking passwords much harder. This guide shows how to do it.

1. Understand Password Hashing Basics

Password hashing turns your plain text password into an unreadable string of characters. It’s a one-way process – you can’t get the original password back from the hash. Crucially, it should be slow to compute.

2. Why Custom Hashing?

While bcrypt and Argon2 are excellent, a custom approach gives you finer control over security parameters and can potentially offer increased resistance against future attacks if implemented carefully.

3. Implementing a Custom Hashing Procedure

  1. Generate a Unique Salt per User: Each user needs their own random salt. Store this salt securely alongside the hashed password (e.g., in your database).
  2. import os
    salt = os.urandom(16) # Generate 16 bytes of random data
    print(salt)
  3. Choose a Strong Hashing Algorithm: SHA-256 is a good starting point, but consider newer algorithms if available in your programming language/framework.
  4. Multiple Stretching Rounds: Repeat the hashing process several times (e.g., 10,000 or more). The higher the number of rounds, the slower and more secure it becomes, but also increases processing time.
  5. import hashlib
    def hash_password(password, salt, rounds=10000):
      hashed = password.encode('utf-8') # Encode to bytes
      for _ in range(rounds):
        hashed = hashlib.sha256(salt + hashed).digest()
      return hashed
  6. Combine Salt and Hash for Storage: Store both the salt *and* the final hash in your database. Never store passwords in plain text!
  7. Verification Process: When a user logs in, retrieve their salt from the database. Re-hash the entered password using that same salt and compare it to the stored hash.
  8. def verify_password(entered_password, stored_salt, stored_hash):
      hashed = hash_password(entered_password, stored_salt)
      return hashed == stored_hash

4. Important Considerations

Exit mobile version