Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python在clear文件中加密密码的最佳方法_Python_Encryption_Passwords - Fatal编程技术网

Python在clear文件中加密密码的最佳方法

Python在clear文件中加密密码的最佳方法,python,encryption,passwords,Python,Encryption,Passwords,我正在寻找一种方法,以获得用户的密码,但具有良好的安全性 目前,我在yaml文件中写入用户密码,如下所示: --- app: username: tata password: toto 使用python代码: url = http://application.com username = Cfg.get(['app', 'username']) password = Cfg.get(['app', 'password']) auth = Appli(url, username,

我正在寻找一种方法,以获得用户的密码,但具有良好的安全性

目前,我在
yaml
文件中写入用户密码,如下所示:

---
app:
  username: tata
  password: toto
使用python代码:

 url = http://application.com
 username = Cfg.get(['app', 'username'])
 password = Cfg.get(['app', 'password'])
 auth = Appli(url, username, password)

但是它不是很干净,我不想用
base64
函数加密到
base64

不太清楚您试图解决的问题

您可以使用任何现代算法对数据进行加密或解密。真正的问题是,如果需要通过编程方式访问加密数据,应该如何存储密钥。将加密数据+密钥存储在一起真的能解决您的安全问题吗


旁注:你可能会发现有用的

有很多类似蟒蛇的方法。 您还可以将密码仅存储在数据库级别的散列中,如
SHA1
md5
SHA256

当用户输入其密码时,系统应将其转换为哈希(您选择的哈希算法),然后检查输入的密码是否正确

import hashlib

url = http://application.com
username = Cfg.get(['app', 'username'])

password= Cfg.get(['app', 'password'])

#store pass_hash in the database
pass_hash= hashlib.sha256(password.encode()).hexdigest()

#authenticate using pass_hash not the actual textual password
auth = Appli(url, username, pass_hash)

在这种情况下,即使您的数据库暴露在外部,您的实际密码将是安全的,只有散列将是可见的。

如果您考虑BASE64“加密”,您不妨用纯文本编写它…查找适当的凭证管理(主要是语言不可知的,BTW),然后关注如何在Python中实现它。密码应该被腌制和散列(用慢散列函数),而不是加密的。考虑一下是的,确实,我正在寻找一种存储密钥的方法。salted和hash可能是一种很好的方法,我没有数据库来存储密码。如果您现在没有数据库,也可以,只要您存储并使用
pass\u hash
进行身份验证,而不是
密码
。现在,请确保将
pass\u散列
s和
username
s关联存储。