Search code examples
pythonhashlibripemd

Unsupported hash type ripemd160 with hashlib in Python


After a thorough search, I have not found a complete explanation and solution to this very common problem on the entire web. All scripts that need to encode with hashlib give me error:

Python 3.10

import hashlib
h = hashlib.new('ripemd160')

return:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/hashlib.py", line 166, in __hash_new
    return __get_builtin_constructor(name)(data)
  File "/usr/lib/python3.10/hashlib.py", line 123, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type ripemd160

I already tried to check if that hash exists in the library, and if I have it:

print(hashlib.algorithms_available): {'md5', 'sm3', 'sha3_512', 'sha384', 'sha256', 'sha1', 'shake_128', 'sha224', 'sha512_224', 'sha512_256', 'blake2b', 'ripemd160', 'md5-sha1', 'sha512', 'sha3_256', 'shake_256', 'sha3_384', 'whirlpool', 'md4', 'blake2s', 'sha3_224'}

I am having this problem in a vps with linux, but in my pc I use Windows and I don't have this problem.

I sincerely appreciate any help or suggestion.


Solution

  • Hashlib uses OpenSSL for ripemd160 and apparently OpenSSL disabled some older crypto algos around version 3.0 in November 2021. All the functions are still there but require manual enabling. See issue 16994 of OpenSSL github project for details.

    To quickly enable it, find the directory that holds your OpenSSL config file or a symlink to it, by running the below command:

    openssl version -d
    

    You can now go to the directory and edit the config file (it may be necessary to use sudo):

    nano openssl.cnf
    

    Make sure that the config file contains following lines:

    openssl_conf = openssl_init
    
    [openssl_init]
    providers = provider_sect
    
    [provider_sect]
    default = default_sect
    legacy = legacy_sect
    
    [default_sect]
    activate = 1
    
    [legacy_sect]
    activate = 1
    

    Tested on: OpenSSL 3.0.2, Python 3.10.4, Linux Ubuntu 22.04 LTS aarch64, I have no access to other platforms at the moment.