Description
Fast general-purpose hashes such as MD5, SHA-1 and SHA-256 make passwords inexpensive to guess if the stored hashes leak.
Potential impact
A weak or reused password can expose the account and the data it can access.
Remediation
Use a vetted password-hashing library with Argon2id. Depending on your requirements, scrypt, bcrypt or PBKDF2 with a sufficient iteration count are alternatives. Use a fresh random salt per password and benchmark the cost on your production hardware.
Examples
Password hashing
Before
MD5:
python
# Unsafe password hashing with MD5
import hashlib
password = b'mypassword'
hashed_password = hashlib.md5(password).hexdigest()
SHA-1:
python
# Unsafe password hashing with SHA-1
import hashlib
password = "mypassword"
hashed_password = hashlib.sha1(password.encode()).hexdigest()
After
Bcrypt:
python
# Safe password hashing with bcrypt
import bcrypt
def hash_password(password):
salt = bcrypt.gensalt(rounds=12)
hashed = bcrypt.hashpw(password.encode(), salt)
return hashed
password = "mysecretpassword"
hashed_password = hash_password(password)
Argon2:
python
# Safe password hashing with Argon2
from argon2 import PasswordHasher
def hash_password(password):
ph = PasswordHasher()
return ph.hash(password)
password = "mysecretpassword"
hashed_password = hash_password(password)
General data integrity hashing, not password hashing
python
# Use SHA-256 for general data integrity, not for passwords
import hashlib
document = b'document contents'
document_digest = hashlib.sha256(document).hexdigest()
The bcrypt example generates a salt with cost factor 12. PasswordHasher uses Argon2id by default. The final SHA-256 example computes a document digest for an integrity comparison.