Skip to content
Guest
Cybersecurity · Course Syllabus Show lessons
Auth & Access Control · 8 min read · Intermediate

Password Hashing, Salting & Constant-Time Checks

Store passwords with Argon2id/bcrypt/PBKDF2 and compare secret tokens without timing leaks.

What you'll learn

  • Generate a unique cryptographically random salt per user (`secrets.token_hex`)
  • Use Argon2id, bcrypt, or high-iteration PBKDF2 for password storage
  • Compare secrets and HMAC signatures with `hmac.compare_digest`

Overview

Store passwords with Argon2id/bcrypt/PBKDF2 and compare secret tokens without timing leaks.

Code Example

import hashlib, hmac, secrets

salt = secrets.token_hex(16)
dk = hashlib.pbkdf2_hmac("sha256", password.encode(), salt.encode(), 210_000).hex()

# Always verify hashes/tokens in constant time
is_valid = hmac.compare_digest(computed_hash, stored_hash)

Key Takeaways

  • Generate a unique cryptographically random salt per user (secrets.token_hex)
  • Use Argon2id, bcrypt, or high-iteration PBKDF2 for password storage
  • Compare secrets and HMAC signatures with hmac.compare_digest

Quick Check

Check Your Understanding

Score: 0 / 1

1. Why is `hmac.compare_digest(a, b)` preferred over `a == b` when comparing API keys or webhook signatures?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind