use hashlib keccak256() function if available

Available if Python compiled with OpenSSL 3.2 or greater (Python >= 3.13)
This commit is contained in:
The MMGen Project 2025-09-23 09:20:53 +00:00
commit 2d01fbc45c
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 24 additions and 14 deletions

View file

@ -55,6 +55,14 @@ def load_cryptodome(called=[]):
sys.modules['Cryptodome'] = Crypto # Cryptodome == pycryptodomex
called.append(True)
def get_hashlib_keccak():
import hashlib
try:
hashlib.new('keccak-256')
except ValueError:
return False
return lambda data: hashlib.new('keccak-256', data)
# called with no arguments by proto.eth.tx.transaction:
def get_keccak(cfg=None, cached_ret=[]):
@ -62,11 +70,10 @@ def get_keccak(cfg=None, cached_ret=[]):
if cfg and cfg.use_internal_keccak_module:
cfg._util.qmsg('Using internal keccak module by user request')
from .contrib.keccak import keccak_256
else:
elif not (keccak_256 := get_hashlib_keccak()):
load_cryptodome()
from Crypto.Hash import keccak
def keccak_256(data):
return keccak.new(data=data, digest_bytes=32)
keccak_256 = lambda data: keccak.new(data=data, digest_bytes=32)
cached_ret.append(keccak_256)
return cached_ret[0]

View file

@ -593,7 +593,7 @@ vmsg = cfg._util.vmsg
proto = cfg._proto
if proto.coin in ('XMR', 'ETH', 'ETC'):
if proto.coin == 'XMR':
from mmgen.util2 import load_cryptodome
load_cryptodome()

View file

@ -44,16 +44,19 @@ class unit_tests:
gmsg('LED support found!')
return True
def keccak(self, name, ut): # used by ETH, XMR
from mmgen.util2 import get_keccak
try:
keccak_256 = get_keccak()
keccak_256(b'abc')
except Exception as e:
rmsg(str(e))
return False
else:
return True
def keccak(self, name, ut): # used by ETH, ETC, XMR
from mmgen.util2 import get_keccak, get_hashlib_keccak
if not (keccak_256 := get_hashlib_keccak()):
ymsg('Hashlib keccak_256() not available, falling back on cryptodome(x) package')
try:
keccak_256 = get_keccak()
except Exception as e:
rmsg(str(e))
return False
chk = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
assert keccak_256(b'').hexdigest() == chk, 'hash mismatch!'
return True
def pysocks(self, name, ut):
import requests, urllib3