In Python, storing sensitive information like usernames and passwords requires careful consideration. When scripting tasks such as periodically retrieving data from third-party services, you need a reliable and secure method to store credentials without compromising data privacy.
One option is utilizing the Python keyring library, which integrates with operating system encryption mechanisms. In Windows, keyring employs the CryptProtectData API, ensuring data encryption with the user's logon credentials.
To use keyring, simply establish a service namespace for your application and store the credentials as key-value pairs:
import keyring
service_id = 'IM_YOUR_APP!'
keyring.set_password(service_id, 'dustin', 'my secret password')
Since keyring securely encrypts the information with the user's credentials, unauthorized parties would require access to the user's operating system to recover the password. However, you can enhance security by encrypting or obfuscating the password within your application before storing it on the keyring. While this doesn't prevent determined attackers from accessing the password, it increases the difficulty and reduces the likelihood of inadvertent disclosure.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3