Create a hashvalue using a custom key and the algorithm ripemd160

489 Views Asked by At

Here's the code I tried in Python but I get 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'

I have searched the Python documentation and lots of forums but do not find much on ripemd160 and Python.

3

There are 3 best solutions below

0
Moses Koledoye On

ripemd160 is not supported directly by the hashlib module:

>>> hashlib.algorithms 

A tuple providing the names of the hash algorithms guaranteed to be supported by this module.

The following are supported by the module: md5, sha1, sha224, sha256, sha384, sha512

So you need to use the new constructor again or pass the reference to the one you already created.

0
Ajeet Shah On

This would work:

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

or

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

Firstly, the key needs to be binary (Python3) -> b"bedford".

Then, the message needs to be encoded if it's unicode etc (Python3) -> codecs.encode("Hello")

Finally, use lambda functions:

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