1

这是我在 Python 中尝试过的代码,但我得到了AttributeError

>>> import hmac
>>> import hashlib
>>> h=hashlib.new('ripemd160')
>>> hmac.new("bedford","Hello",hashlib.ripemd160)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: module 'hashlib' has no attribute 'ripemd160'

我已经搜索了 Python 文档和很多论坛,但没有找到太多关于 Rivemd160 和 Python 的内容。

4

3 回答 3

1

ripemd160模块不直接支持hashlib

>>> hashlib.algorithms 

提供此模块保证支持的哈希算法名称的元组。

模块支持以下内容:md5, sha1, sha224, sha256, sha384, sha512

因此,您需要new再次使用构造函数或将引用传递给您已经创建的构造函数。

于 2016-06-04T08:11:53.530 回答
1

这会起作用:

hmac.new("bedford", "Hello", lambda: hashlib.new('ripemd160'))

或者

h=hashlib.new('ripemd160')
hmac.new("bedford", "Hello", lambda: h)
于 2016-06-04T08:16:14.607 回答
1

首先,密钥需要是二进制 (Python3) -> b"bedford"

然后,如果消息是 unicode 等(Python3),则需要对其进行编码->codecs.encode("Hello")

最后,使用lambda函数

import codecs
import hmac
import hashlib
h=hashlib.new('ripemd160')
hmac.new(b"bedford", codecs.encode("Hello"), lambda: h)
于 2016-06-04T08:18:41.053 回答