Python 3.9 中 hashlib 中的每个哈希函数都添加了参数 usedforsecurity
。
Changed in version 3.9: All hashlib constructors take a keyword-only argument usedforsecurity with default value True. A false value allows the use of insecure and blocked hashing algorithms in restricted environments. False indicates that the hashing algorithm is not used in a security context, e.g. as a non-cryptographic one-way compression function.
但是,这提供了零指导
- 什么时候应该使用
usedforsecurity
- 什么时候你不应该使用
usedforsecurity
- 什么是“受限环境”
虽然我不是安全研究人员,但我非常清楚 md5 在任何意义上都不安全。因此,usedforsecurity
这个名字在很多方面都让我感到困惑。
usedforsecurity
的意义何在?
最佳答案
文档中给出的解释非常清楚何时应该或不应该使用 usedforsecurity
:如果您不出于安全目的使用该算法,则传递 False
.
其效果确实值得更多解释。在大多数 Python 版本中,此参数不执行任何操作。默认情况下启用不安全算法。
“受限环境”主要是使用OpenSSL FIPS module的Python的罕见构建。 (或 3.0 之前的 OpenSSL 上的 OpenSSL FIPS 模式)以禁用不安全的算法。如果您传递 usedforsecurity=False
,Python 将告诉 OpenSSL 无论如何都允许不安全的算法(如果您想查看代码,请参阅 here 、 here 和 here )。如果您确实不将该算法用于任何需要加密强度保证的事情,那么这是可以的。
如果您想了解更多信息,还可以查看issue discussion这最初导致引入此标志。
关于python - 用于安全的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77425682/