Blog | G5 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.

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.

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

Exit mobile version