Get a Pentest and security assessment of your IT network.

Cyber Security

Secure Username Storage: Encryption Guide

TL;DR

Don’t store usernames in plain text! Use a strong hashing algorithm (like bcrypt or Argon2) with a unique salt for each user. This makes it very difficult to recover the original username even if your database is compromised.

How to Encrypt Usernames

  1. Understand Why Encryption Isn’t Enough: Simply encrypting usernames (like using AES) isn’t ideal. If someone gets the encryption key, they can decrypt all usernames. Hashing is better because it’s a one-way process – you can’t easily get back the original username from the hash.
  2. Choose a Strong Hashing Algorithm:
    • bcrypt: A widely used and well-tested algorithm. It automatically includes salting, making it easier to implement securely.
    • Argon2: More modern than bcrypt and offers better resistance against certain attacks (like GPU cracking). It’s a bit more complex to set up but is considered very secure.
    • Avoid older algorithms like MD5 or SHA1: These are known to be vulnerable.
  3. Generate Salts: A salt is random data added to each username before hashing. This prevents attackers from using pre-computed tables of common password/username hashes (rainbow tables).
    • Each user must have a unique salt.
    • Salts should be long enough – at least 16 bytes is recommended.
  4. Hash the Username with the Salt: Combine the username and its corresponding salt before hashing.
    # Example using Python and bcrypt
    import bcrypt
    
    salt = bcrypt.gensalt()
    hashed_username = bcrypt.hashpw("myusername".encode('utf-8'), salt)
    print(hashed_username)
    
  5. Store the Salt and Hash: You need to store both the generated salt and the resulting hash in your database.
    • Don’t store the original username!
    • Typically, you’ll have two columns in your user table: one for the hash and one for the salt.
  6. Verify Usernames During Login: When a user tries to log in:
    1. Retrieve the salt associated with that username from the database.
    2. Hash the entered username using the retrieved salt.
    3. Compare the resulting hash with the stored hash.
      # Example using Python and bcrypt
      import bcrypt
      
      stored_hash = "..."
      stored_salt = "..."
      attempted_username = "myusername"
      
      hashed_attempt = bcrypt.hashpw(attempted_username.encode('utf-8'), stored_salt)
      
      if hashed_attempt == stored_hash:
        print("Username verified!")
      else:
        print("Incorrect username.")
      
  7. Database Considerations:
    • Use parameterized queries to prevent SQL injection attacks. This is crucial when dealing with user input.
    • Ensure your database connection is secure (e.g., using TLS/SSL).

Important Security Notes

  • Keep Salts Secret: While not as critical as the encryption key, salts should be treated as sensitive data.
  • Regularly Review and Update: Stay informed about new security vulnerabilities and best practices in hashing algorithms.
  • Consider a Password Manager Library: For complex applications, using a well-maintained password manager library (which handles salting, hashing, and other security features) is highly recommended.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation