Best practices for data protection and encryption
We covered attacks accessing data in unauthorized ways in Chapter 8. Data protection and encryption are essential for securing sensitive information transmitted via APIs. In Python, using libraries such as cryptography
to encrypt data at rest and in transit is crucial. For instance, encrypting sensitive information such as passwords and personal data before storing it in the database can prevent unauthorized access. Observe the following code that applies the cryptography
library to make use of Fernet tokens and keys:
# The wrong way: Storing sensitive data without encryption user_data = {'ssn': '123-45-6789'} database.store(user_data) # The correct way: Encrypting sensitive data before storing from cryptography.fernet import Fernet key = Fernet.generate_key() cipher_suite = Fernet(key) encrypted_ssn = cipher_suite.encrypt(b'123-45-6789') user_data = {'ssn': encrypted_ssn} database.store(user_data...