Safe Handling of Passwords
When handling passwords in Python applications, you face significant risks if you do not adopt secure storage principles. Storing passwords improperly can expose your users and systems to attacks such as credential theft, unauthorized access, and data breaches. Secure storage means never keeping the actual password in a retrievable format, and instead, applying cryptographic techniques to make it infeasible for attackers to recover the original password, even if they gain access to your storage.
# Insecure: storing a password in plain text
user_password = "supersecret123"
with open("passwords.txt", "w") as file:
file.write(user_password)
Storing passwords in plain text, as shown above, is highly insecure. Anyone who gains access to the storage medium—such as a file, database, or backup—can immediately read the password. This exposes all user accounts to compromise if the data is leaked or stolen, and is a common cause of large-scale security breaches.
# Secure: storing a password hash using hashlib
import hashlib
password = "supersecret123"
# Create a hash of the password
password_hash = hashlib.sha256(password.encode()).hexdigest()
with open("password_hashes.txt", "w") as file:
file.write(password_hash)
This secure pattern protects passwords by storing only the hash value, not the actual password. When a user logs in, your application hashes the entered password and compares it to the stored hash. Even if attackers access the hashes, they cannot retrieve the original passwords directly, making it much harder to compromise accounts.
Hashing is better than encryption for passwords because hashes are one-way functions—they cannot be reversed to reveal the original password. Encryption can be decrypted if the key is exposed, but hashes are designed to be irreversible, which is why they are preferred for storing passwords securely.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 5.56
Safe Handling of Passwords
Glissez pour afficher le menu
When handling passwords in Python applications, you face significant risks if you do not adopt secure storage principles. Storing passwords improperly can expose your users and systems to attacks such as credential theft, unauthorized access, and data breaches. Secure storage means never keeping the actual password in a retrievable format, and instead, applying cryptographic techniques to make it infeasible for attackers to recover the original password, even if they gain access to your storage.
# Insecure: storing a password in plain text
user_password = "supersecret123"
with open("passwords.txt", "w") as file:
file.write(user_password)
Storing passwords in plain text, as shown above, is highly insecure. Anyone who gains access to the storage medium—such as a file, database, or backup—can immediately read the password. This exposes all user accounts to compromise if the data is leaked or stolen, and is a common cause of large-scale security breaches.
# Secure: storing a password hash using hashlib
import hashlib
password = "supersecret123"
# Create a hash of the password
password_hash = hashlib.sha256(password.encode()).hexdigest()
with open("password_hashes.txt", "w") as file:
file.write(password_hash)
This secure pattern protects passwords by storing only the hash value, not the actual password. When a user logs in, your application hashes the entered password and compares it to the stored hash. Even if attackers access the hashes, they cannot retrieve the original passwords directly, making it much harder to compromise accounts.
Hashing is better than encryption for passwords because hashes are one-way functions—they cannot be reversed to reveal the original password. Encryption can be decrypted if the key is exposed, but hashes are designed to be irreversible, which is why they are preferred for storing passwords securely.
Merci pour vos commentaires !