当我在Macbook上运行以下内容时,出现错误:
>>> import hashlib
>>> hashlib.md5(usedforsecurity=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: openssl_md5() takes no keyword arguments
但是当我在我的Linux机器上运行它时,它可以工作!
>>> import hashlib
>>> hashlib.md5(usedforsecurity=False)
<md5 HASH object @ 0x7f763c1375d0>
我的问题是,我需要在启用FIPS的系统上运行一些安全的,非安全相关的代码(例如管理用户请求的缓存,将用户查询哈希作为MD5字符串)。使用usedforsecurity
标志可防止FIPs异常。
这工作正常,除非我想在我的Macbook上测试我的代码。我的Macbook的“libcrypto”库显然不支持这个usedforsecurity
标志。有没有一种好方法可以检测hashlib.md5
背后的底层C绑定是否支持此标志?
无法明确检查C绑定是否具有特定的关键字参数:
>>> import inspect
>>> inspect.getargspec(hashlib.md5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/inspect.py", line 815, in getargspec
raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <built-in function openssl_md5> is not a Python function
这是我能想到的最好的,使用try / except:
>>> import hashlib
>>> has_usedforsecurity_flag = False
>>> try:
... hashlib.md5(usedforsecurity=False)
... has_usedforsecurity_flag = True
... except Exception as e:
... print e
... # Doesn't have the flag.
...
<md5 HASH object @ 0x7f763c0b9bc0>