令人讨厌的加密弃用警告,因为到处都缺少hmac.compare_time功能



事情进展顺利,直到我的一个项目开始在每次执行的顶部打印它,至少一次:

local/lib/python2.7/site-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a 2.7.x release that supports hmac.compare_digest as soon as possible.

我不知道它为什么开始并且它正在破坏应用程序/工具的输出,尤其是当它被其他工具捕获和使用时。就像古往今来的许多困难一样,我相当确定它与urllib有关,并且通过关联,requests.更糟糕的是,我有太多的项目和交叉依赖项,以至于我不可能通过调用warnings.filterwarnings()来更新所有导入和分支以禁止警告。

我有 Python 2.7.6 .显然,这在 2.7.7 中消失了。只是,我有一些系统有 2.7.6,我没有看到警告。因此,某些东西可能会也可能不会在一个版本中禁用它们,我可能无意中将其替换为另一个版本。

我的 Ubuntu、Python、urllib、requests(带有安全选项(、加密和 hmac 都是相同的版本/构建,这些版本/构建在

打印警告的系统上和不打印警告的系统上。网上似乎没有相关的警告或公告,到目前为止,似乎任何相关项目都是静态/稳定的(即使"hmac"可以通过PIP安装,它也在八年内没有改变(。

我遇到这个错误已经有一段时间了。对于我的环境,将 Python 升级到比 2.7.6 更高的版本很痛苦。更简单的解决方案是使用 pip 降级加密模块:

pip2.7 install cryptography==2.2.2

我认为最好的解决方案是升级你的python版本。

这个答案是针对 Python3 的

我通过使用Paramiko时寻找答案而来到这里。对于那些仍在寻找简单答案的人。在导入 Paramiko 之前,我得到了这些密码学弃用警告,替换为这些代码行:

import warnings 
warnings.filterwarnings(action='ignore',module='.*paramiko.*')

我希望这有帮助

如果您想更有选择性地抑制该特定弃用警告:

import warnings
from cryptography.utils import CryptographyDeprecationWarning
warnings.filterwarnings("ignore", category=CryptographyDeprecationWarning)

。好吧,我很确定上述内容在发布时确实有效,或者在某些情况下仍然有效。但最近我发现了一种情况,它没有抑制警告,而 Nikolaj Š. 评论中的这个建议确实:

import warnings
# Suppress the deprecation warning from the cryptography module.
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import cryptography

所以。。。随便你去吧!

我开始收到这个警告,因为这是一个直接的requests.get电话。此警告在加载模块cryptography.hazmat.primitives.constant_time时打印,因此每个 Python 程序通常只应出现一次。如果你多次看到它,那一定是因为一个Python程序(如实用程序(被多次执行。您只需要识别该程序并将以下代码添加到主入口点:

import cryptography
from cryptography import utils
with warnings.catch_warnings():
warnings.simplefilter('ignore', cryptography.utils.DeprecatedIn23)
import cryptography.hazmat.primitives.constant_time

我通过从python源代码中删除警告来修复使用Python 2的所有本地项目和工具[your_installation_path]PythonLibsite-packagescryptography__init__.py

只需删除文件末尾的截图并删除__init__.pyc文件,以便使用更改重新编译:

if sys.version_info[0] == 2:
warnings.warn(
"Python 2 is no longer supported by the Python core team. Support for "
"it is now deprecated in cryptography, and will be removed in the "
"next release.",
CryptographyDeprecationWarning,
stacklevel=2,
)

仅适用于 Python3:

根据明显的Paramiko更新,这对我有用并解决了我遇到的类似问题/症状:

pip3 install --upgrade paramiko

这在我的系统上安装了paramiko 2.6.0,取代了2.4.2:

$ pip3 install --upgrade paramiko
[...]
Installing collected packages: paramiko
Found existing installation: paramiko 2.4.2
Uninstalling paramiko-2.4.2:
Successfully uninstalled paramiko-2.4.2
Successfully installed paramiko-2.6.0
$

我的 Python2环境似乎搞砸了,所以我无法在 Python2 上测试它。

当您执行 pip2.7 安装加密==2.2.2 时,似乎仍然会发生错误。 我相信你也需要sudo pip2.7 install --upgrade pip麻生,截至 5 年 5 月 19 日,最新的似乎是加密学=2.6.1

最新更新