首页 > 解决方案 > Python 3.9.8、hashlib 和 RIPEMD160

问题描述

运行 MacOS 10.14.6。刚刚运行 MacPorts 更新。使用 Python 3.9.7 -> 3.9.8 和 OpenSSL 1.1.3 -> 3。

运行现有的 Python 代码会发现有hashlib问题,RIPEMD160 不再可用(Whirlpool 和可能的其他摘要也不再可用)。

Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/hashlib.py", line 160, in __hash_new
    return _hashlib.new(name, data, **kwargs)
ValueError: [digital envelope routines] initialization error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "testhash.py", line 3, in <module>
    r160 = hashlib.new('ripemd160')
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/hashlib.py", line 166, in __hash_new
    return __get_builtin_constructor(name)(data)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/hashlib.py", line 123, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type ripemd160

可以简单地复制

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

方法hashlib.algorithms_available表明hashlibthinksripemd160 可用

Available:  {'sha512', 'shake_128', 'ripemd160', 'sha224', 'md5', 'whirlpool', 'blake2b', 'sha3_512', 'md4', 'sha3_256', 'sha256', 'shake_256', 'md5-sha1', 'sha1', 'sha512_224', 'sm3', 'mdc2', 'blake2s', 'sha3_384', 'sha3_224', 'sha512_256', 'sha384'} 
Guaranteed: {'sha3_512', 'sha512', 'sha1', 'shake_128', 'sha3_384', 'sha224', 'md5', 'sha256', 'sha3_224', 'sha3_256', 'shake_256', 'blake2b', 'blake2s', 'sha384'} 

并询问openssl确认它确实具有这种能力。

回滚,同样的问题。我宁愿保持最新的安装。RIPEMD160 的使用不可协商。

我怀疑给出ports正确的开关、命令、环境变量会说服重新编译工作,但我不知道是什么。

关于发生了什么的任何想法,如何解决?

标签: python-3.xopensslmacportshashlib

解决方案


一个不太理想的临时解决方案是:

  1. 回滚 OpenSSL
port activate openssl @1.1_3
  1. 删除 Python 的安装(必要时回滚到以前的版本后)
port uninstall python39
port clean python39
  1. 重新编译 Python
port install -s -f python39

推荐阅读