Archiviazione sicura delle password e recupero delle credenziali
Impari a eseguire correttamente l'hashing delle password, a difenderti dagli attacchi alle credenziali e a creare flussi sicuri per il reset delle password e il recupero degli account.
Archiviazione sicura delle password e recupero delle credenziali è una lezione Secure Coding & OWASP Top 10 for Backend gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Secure Coding & OWASP Top 10 for Backend, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Secure Coding & OWASP Top 10 for Backend include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Why Hashing Matters
Storing passwords in plaintext or with weak encoding means a single database breach exposes every user. Passwords must be stored as salted hashes using a slow, purpose-built algorithm.
This lesson covers correct hashing and secure recovery flows.
Hash, Do Not Encrypt
Passwords should be hashed, not encrypted. Encryption is reversible; if the key leaks, all passwords are exposed. Hashing is one-way: you verify by hashing the input and comparing.
- Never use reversible encryption for passwords
- Never use fast hashes like MD5 or SHA-1 alone
Salting
A salt is a unique random value added to each password before hashing. Salts ensure two users with the same password get different hashes, defeating precomputed rainbow tables.
Modern algorithms generate and store the salt for you.
Choosing an Algorithm
Use a deliberately slow, memory-hard algorithm: Argon2 (preferred), bcrypt, or scrypt. Their cost factor makes brute-force attacks expensive even with stolen hashes.
Hashing in Code
Here is a conceptual hashing and verification flow using a bcrypt-style API.
import bcrypt
def hash_password(plain):
return bcrypt.hashpw(plain.encode(), bcrypt.gensalt(rounds=12))
def verify(plain, stored_hash):
return bcrypt.checkpw(plain.encode(), stored_hash)
h = hash_password('s3cret')
print(verify('s3cret', h))Tuning the Cost Factor
The cost factor (rounds) controls how slow hashing is. Set it so a single hash takes around 100-300ms on your hardware: slow enough to deter attackers, fast enough for login. Re-tune it as hardware improves.
Defending Against Credential Stuffing
Attackers reuse leaked credentials across sites. Defend with rate limiting, account lockout with backoff, and checks against known-breached password lists.
- Throttle failed logins per account and per IP
- Reject passwords found in breach corpuses
- Alert users of logins from new devices
Constant-Time Comparison
Comparing secrets must take the same time regardless of how many characters match, or attackers can infer values via timing. Hashing libraries provide constant-time verify functions; use them instead of plain equality.
Secure Password Reset Tokens
Reset flows are a frequent weak point. Generate a high-entropy random token, store only its hash, set a short expiry, and invalidate it after one use.
import secrets, hashlib, time
def create_reset_token():
raw = secrets.token_urlsafe(32)
record = {
'token_hash': hashlib.sha256(raw.encode()).hexdigest(),
'expires_at': time.time() + 900,
'used': False,
}
return raw, record # email raw to user, store recordAvoiding Account Enumeration
Reset and login responses should not reveal whether an email exists. Return the same message regardless, and send the reset email only if the account exists. This prevents attackers from harvesting valid accounts.
Rehashing on Login
When you upgrade algorithms or cost factors, rehash a user's password on their next successful login using the new parameters. This migrates your store gradually without forcing resets.
Quick Check
Test your understanding of secure password storage.
Recap
You learned to hash with salt using Argon2 or bcrypt, tune the cost factor, defend against credential stuffing with rate limiting, compare in constant time, and build single-use expiring reset tokens that avoid account enumeration. Proper credential handling protects users even when the database is breached.
Impara Secure Coding & OWASP Top 10 for Backend con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Archiviazione sicura delle password e recupero delle credenziali» è gratuita?
Sì — il testo completo di «Archiviazione sicura delle password e recupero delle credenziali» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Secure Coding & OWASP Top 10 for Backend, passa a CoddyKit PRO. Il corso Secure Coding & OWASP Top 10 for Backend include 4 lezioni in totale.
Cosa imparerò in «Archiviazione sicura delle password e recupero delle credenziali»?
Impari a eseguire correttamente l'hashing delle password, a difenderti dagli attacchi alle credenziali e a creare flussi sicuri per il reset delle password e il recupero degli account. Eserciti Secure Coding & OWASP Top 10 for Backend con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Secure Coding & OWASP Top 10 for Backend?
Non è richiesta alcuna esperienza precedente. Secure Coding & OWASP Top 10 for Backend su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Archiviazione sicura delle password e recupero delle credenziali»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Secure Coding & OWASP Top 10 for Backend?
Sì. Ogni lezione Secure Coding & OWASP Top 10 for Backend include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Autenticazione a più fattori (MFA)
- OAuth 2.0 e OpenID Connect
- Sicurezza e best practice per i JWT
- Archiviazione sicura delle password e recupero delle credenziali