【问题标题】:Create a hashvalue using a custom key and the algorithm ripemd160使用自定义键和算法ripemd160 创建哈希值
【发布时间】:2016-06-04 07:59:40
【问题描述】:

这是我在 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 的内容。

【问题讨论】:

    标签: python ripemd


    【解决方案1】:

    ripemd160 模块不直接支持hashlib

    >>> hashlib.algorithms 
    

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

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

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

    【讨论】:

      【解决方案2】:

      这可行:

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

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

      【讨论】:

        【解决方案3】:

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

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

        最后,使用lambda functions:

        import codecs
        import hmac
        import hashlib
        h=hashlib.new('ripemd160')
        hmac.new(b"bedford", codecs.encode("Hello"), lambda: h)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-22
          • 1970-01-01
          • 2013-10-29
          • 2013-08-07
          • 2021-06-01
          • 1970-01-01
          • 2012-01-25
          • 1970-01-01
          相关资源
          最近更新 更多