我正在使用 Python 2.7 并正在使用该hmac
库创建一个 HMAC。Python 3.3 包含一个compare_digest()
函数,可以比较两个摘要并抵抗定时攻击,但在 2.7 中不可用。普遍的建议是不要推出我自己的加密货币,那么是否有任何成熟的 Python 库提供该功能?PyCrypto 似乎没有。
问问题
2780 次
3 回答
3
对于从搜索中找到此内容的任何人,如果使用 Django,那么您也可以constant_time_compare
使用django.utils.crypto
.
>>> from django.utils.crypto import constant_time_compare
>>> constant_time_compare("foo", "bar")
False
>>> constant_time_compare("foo", "foo")
True
这带有与(如果存在则hmac.compare_digest
实际使用)相同的警告:hmac.compare_digest
注意:如果 a 和 b 的长度不同,或者如果发生错误,则定时攻击理论上可以揭示有关 a 和 b 的类型和长度的信息,但不能揭示它们的值。
于 2015-04-05T11:38:09.083 回答
3
我建议您只使用 3.3 中提供的安全比较方法。
这是一个与 Python 实现非常相似的实现:
def compare_digest(x, y):
if not (isinstance(x, bytes) and isinstance(y, bytes)):
raise TypeError("both inputs should be instances of bytes")
if len(x) != len(y):
return False
result = 0
for a, b in zip(x, y):
result |= a ^ b
return result == 0
看不出这将如何违反任何许可证。
于 2013-08-11T16:12:59.297 回答
1
如果您可以访问 Python 2.7.7,compare_digest()
则最近已向后移植到此版本(以及 2.7.9 中更安全的 3.x SSL 模块)。
于 2014-12-14T06:32:31.433 回答