Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Safe Handling of Passwords | Protecting Sensitive Data
Python Security Best Practices

bookSafe 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.

Note
Study More

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.

question mark

Why should passwords never be stored in plain text?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5.56

bookSafe Handling of Passwords

Swipe to show 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.

Note
Study More

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.

question mark

Why should passwords never be stored in plain text?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt