Python 从数据库存储和检索密码的最安全方法
希望将用户名和密码存储在数据库中,并且想知道最安全的方法是什么。我知道我必须在某处使用盐,但不确定如何安全地生成它或如何应用它来加密密码。一些示例 Python 代码将不胜感激。谢谢。
Looking to store usernames and passwords in a database, and am wondering what the safest way to do so is. I know I have to use a salt somewhere, but am not sure how to generate it securely or how to apply it to encrypt the password. Some sample Python code would be greatly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将密码+盐存储为哈希值和盐。看看 Django 是如何做到的:基本文档 和 源。
在数据库中,它们将
$$
存储在单个字符字段中。您还可以将这三个部分存储在不同的字段中。设置密码的函数:
get_hexdigest 只是一些哈希算法的薄包装。您可以使用 hashlib 来实现这一点。类似
hashlib.sha1('%s%s' % (salt, hash)).hexdigest()
以及检查密码的函数:
Store the password+salt as a hash and the salt. Take a look at how Django does it: basic docs and source.
In the db they store
<type of hash>$<salt>$<hash>
in a single char field. You can also store the three parts in separate fields.The function to set the password:
The get_hexdigest is just a thin wrapper around some hashing algorithms. You can use hashlib for that. Something like
hashlib.sha1('%s%s' % (salt, hash)).hexdigest()
And the function to check the password:
我认为最好使用专门用于散列密码的函数。我在这里解释了一些原因:https://stackoverflow.com/a/10948614/893857
如今,标准库在文档中有一个专门用于哈希密码的部分。它甚至提到您应该从加密安全的随机源(如 os.urandom())获取盐。
I think it is best to use a function dedicated to hashing passwords for this. I explain some reasons for this here: https://stackoverflow.com/a/10948614/893857
Nowadays the standard library has a dedicated section in the documentation for hashing password. It even mentions that you should get your salt from a cryptographically secure random source like
os.urandom()
.我在这里回答了这个问题:https://stackoverflow.com/a/18488878/1661689,@Koffie 也是如此。
我不知道如何充分强调所接受的答案是不安全的。它比纯文本更好,也比无盐哈希更好,但它仍然极易受到字典甚至暴力攻击。相反,请使用慢速 KDF,例如 bcrypt(或至少具有 10,000 次迭代的 PBKDF2)
I answered this here: https://stackoverflow.com/a/18488878/1661689, and so did @Koffie.
I don't know how to emphasize enough that the accepted answer is NOT secure. It is better than plain text, and better than an unsalted hash, but it is still extremely vulnerable to dictionary and even brute-force attacks. Instead, please use a SLOW KDF like bcrypt (or at least PBKDF2 with 10,000 iterations)
如果您对应用程序的两个端点有足够的控制,绝对最好的方法是使用 PAK-Z+。
(编辑:原始版本推荐 SRP,但 PAK-Z+ 有安全证明。)
If you have enough control over both endpoints of the application, the absolute best way is using PAK-Z+.
(Edited: the original version recommended SRP but PAK-Z+ has a proof of security.)
对于 Flask 应用程序或任何 Python 应用程序,您可以使用 werkzeug WSGI Web 应用程序库,它为您提供了使用盐和不同类型的算法生成和解码密码的选项,格式如下: method$salt$hash
默认盐长度是 16,使用的算法是 sha256
例如。
您可以在此处阅读有关 werkzeug 安全性的更多信息: https:// werkzeug.palletsprojects.com/en/2.0.x/utils/#module-werkzeug.security
For flask application or any python application you can use the werkzeug WSGI web application library, which provides you the option to generate and decode the password with salt and different types of algorithms in a format like this : method$salt$hash
The default salt length is 16 and algo used is sha256
Eg.
you can read more about werkzeug security here : https://werkzeug.palletsprojects.com/en/2.0.x/utils/#module-werkzeug.security
这是一个更简单的方法(取自effbot),假设长度大于8的密码不会有问题*:
用于生成密码:
*:长度大于8的密码从右往下剥离为8个字符长的
Here is a simpler way (taken from effbot), provided passwords with a length greater than 8 will not be a problem*:
for generate the password :
*: A password with a length greater than 8 is stripped from the right down to 8 chars long