Copied to Clipboard
Encrypting a message (e.g., for sharing):
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher = Fernet(key)
plaintext = b"Sensitive meeting notes"
encrypted = cipher.encrypt(plaintext) # store or send this
# Later, with the same key:
decrypted = cipher.decrypt(encrypted) # recovers original
The first snippet demonstrates irreversible hashing for password storage—you never need the original password again, only to verify it. The second shows reversible encryption for data you must retrieve later.
Key Takeaway
Always pick the tool based on whether you need to recover the original data.
If you never need the original (passwords, integrity checks), use hashing (with a salt). If you must get the original back (messages, files), use encryption (and protect the key). Mix them up, and you’ll either leak secrets or lock yourself out.
For deeper diving, check out the OWASP guide on Password Storage Cheat Sheet.