要安全地存储用户名和密码组合以在 cron 作业执行的 Python 脚本中使用,请考虑以下事项options:
密钥环库与 Windows 上的 CryptProtectData API 以及其他平台上的相关 API 无缝集成。这样可以使用用户的登录凭据对数据进行加密。其简单用法包括:
import keyring
# Define a unique namespace for your application
service_id = 'IM_YOUR_APP!'
# Set the password for a given username
keyring.set_password(service_id, 'dustin', 'my secret password')
# Retrieve the password
password = keyring.get_password(service_id, 'dustin')
要单独存储用户名,请滥用 set_password 函数:
import keyring
MAGIC_USERNAME_KEY = 'im_the_magic_username_key'
# Username to store
username = 'dustin'
# Store the password and username in the keyring
keyring.set_password(service_id, username, "password")
keyring.set_password(service_id, MAGIC_USERNAME_KEY, username)
# Retrieve username and password
username = keyring.get_password(service_id, MAGIC_USERNAME_KEY)
password = keyring.get_password(service_id, username)
由于存储在密钥环中的项目是使用用户凭据加密的,因此在同一用户帐户下运行的其他应用程序可以访问密码。
为了增强安全性,请考虑在将密码存储到密钥环之前对其进行混淆或加密。这增加了额外的保护层,防止通过自动密码检索意外泄露。但是,任何有权访问脚本源代码的人仍然有可能解密密码。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3