Get a Pentest and security assessment of your IT network.

Cyber Security

Secure Hash Authentication

TL;DR

This guide explains how to build a secure authentication system using hash functions. It covers salting passwords, storing hashes securely, and verifying user credentials. We’ll focus on practical steps and common pitfalls.

1. Understanding the Basics

Hash functions take an input (like a password) and produce a fixed-size output (the hash). Crucially, it’s very difficult to reverse engineer the original input from the hash. This is what makes them useful for storing passwords.

  • Hashing: Converts data into a one-way representation.
  • Salting: Adding random data to each password before hashing. This prevents attackers using pre-computed tables of common password hashes (rainbow tables).

2. Generating Salts

Each user needs a unique salt. Here’s how you can generate them:

  1. Randomness: Use a cryptographically secure random number generator. Don’t use simple methods like rand() in many programming languages, as they aren’t strong enough.
  2. Length: Salts should be long enough to make brute-force attacks impractical (at least 16 bytes/128 bits).

Example using Python:

import os
salt = os.urandom(16)
print(salt)

3. Hashing Passwords with Salts

Combine the salt and password before hashing. Use a strong hashing algorithm like bcrypt, Argon2, or scrypt.

  1. Concatenation: Combine the salt and password (e.g., salt + password).
  2. Hashing Algorithm: Choose a robust algorithm designed for password storage. bcrypt is a good starting point.

Example using Python with bcrypt:

import bcrypt
password = b'mysecretpassword'
salt = os.urandom(16)
hashed_password = bcrypt.hashpw(password, salt)
print(hashed_password)

4. Storing Hashes Securely

How you store the hash is just as important as how you create it.

  • Database: Store both the salt and hashed_password in your database, associated with the user account. Do *not* store the password itself!
  • Encryption at Rest: Consider encrypting the entire database to protect against data breaches.

5. Verifying User Credentials

When a user logs in, you need to verify their password.

  1. Retrieve Salt: Get the salt associated with the user’s account from your database.
  2. Hash Input Password: Hash the password they entered using *the same* salt that was used during registration.
  3. Compare Hashes: Compare the newly generated hash with the stored hash. If they match, the password is correct.

Example using Python with bcrypt:

import bcrypt
stored_salt = b'your_stored_salt'
stored_hash = b'your_stored_hash'
user_password = b'mysecretpassword'
hashed_input_password = bcrypt.hashpw(user_password, stored_salt)
if hashed_input_password == stored_hash:
    print("Password verified!")
else:
    print("Incorrect password.")

6. Important Security Considerations

  • Algorithm Updates: Hashing algorithms can become vulnerable over time. Stay up-to-date and consider migrating to stronger algorithms when necessary.
  • Rate Limiting: Implement rate limiting on login attempts to prevent brute-force attacks.
  • Two-Factor Authentication (2FA): Add an extra layer of security with 2FA.
  • Regular Audits: Regularly review your authentication system for vulnerabilities.
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