Python中使用hashlib出现不支持的哈希类型ripemd160。

10

经过彻底搜索,我没有在整个网络上找到完整的解释和解决此非常普遍问题的方法。所有需要使用hashlib编码的脚本都给我报错:

Python 3.10

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

返回:

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

我已经尝试检查库中是否存在该哈希值,并且我是否拥有它:

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'}

我在一台运行Linux的VPS上遇到了这个问题,但是在我的PC上使用Windows时并没有遇到这个问题。

我真诚地感谢任何帮助或建议。


还有其他的吗?你可以运行这个,然后分享你的输出。 - Kelly Bundy
1个回答

19

Hashlib使用OpenSSL实现ripemd160算法,但是自从2021年11月OpenSSL版本升级到3.0后,一些旧的加密算法被禁用了。虽然这些函数仍然存在,但需要手动启用。详情请参见OpenSSL github项目的issue 16994

要快速启用它,请运行以下命令查找包含您的OpenSSL配置文件或其符号链接的目录:

openssl version -d

现在您可以前往目录并编辑配置文件(可能需要使用sudo):

nano openssl.cnf

请确保配置文件包含以下行:

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

在 OpenSSL 3.0.2、Python 3.10.4 和 Linux Ubuntu 22.04 LTS aarch64 平台上测试通过,目前无法访问其他平台。


1
干得好。我用这种方式解决了Zeronet中的一个错误。 - caiofior

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接