From 8559b9c668db9c90c9f409c705240a90eaab35f9 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 24 Sep 2021 20:07:05 +0000 Subject: [PATCH] do_hashlib_scrypt(): cleanups --- mmgen/crypto.py | 35 ++++++++++++++++++++++++----------- mmgen/globalvars.py | 15 ++++++++------- mmgen/util.py | 2 +- mmgen/wallet.py | 2 +- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/mmgen/crypto.py b/mmgen/crypto.py index 0fef50ab..e71a35c7 100755 --- a/mmgen/crypto.py +++ b/mmgen/crypto.py @@ -96,26 +96,39 @@ def scrypt_hash_passphrase(passwd,salt,hash_preset,buflen=32): # Buflen arg is for brainwallets only, which use this function to generate # the seed directly. - N,r,p = get_hash_params(hash_preset) - if isinstance(passwd,str): passwd = passwd.encode() + ps = get_hash_params(hash_preset) + + if isinstance(passwd,str): + passwd = passwd.encode() def do_hashlib_scrypt(): - from hashlib import scrypt # Python >= v3.6 - return scrypt(passwd,salt=salt,n=2**N,r=r,p=p,maxmem=0,dklen=buflen) + from hashlib import scrypt + return scrypt( + password = passwd, + salt = salt, + n = 2**ps.N, + r = ps.r, + p = ps.p, + maxmem = 0, + dklen = buflen ) def do_standalone_scrypt(): import scrypt - return scrypt.hash(passwd,salt,2**N,r,p,buflen=buflen) + return scrypt.hash( + password = passwd, + salt = salt, + N = 2**ps.N, + r = ps.r, + p = ps.p, + buflen = buflen ) if int(hash_preset) > 3: msg_r('Hashing passphrase, please wait...') - # hashlib.scrypt doesn't support N > 14 (hash preset 3) - if N > 14 or g.force_standalone_scrypt_module: - ret = do_standalone_scrypt() - else: - try: ret = do_hashlib_scrypt() - except: ret = do_standalone_scrypt() + # hashlib.scrypt doesn't support N > 14 (hash preset > 3) + ret = ( + do_standalone_scrypt() if ps.N > 14 or g.force_standalone_scrypt_module else + do_hashlib_scrypt() ) if int(hash_preset) > 3: msg_r('\b'*34 + ' '*34 + '\b'*34) diff --git a/mmgen/globalvars.py b/mmgen/globalvars.py index 4a74398b..04ea4fb8 100755 --- a/mmgen/globalvars.py +++ b/mmgen/globalvars.py @@ -282,14 +282,15 @@ class GlobalContext(Lockable): # Scrypt params: 'id_num': [N, r, p] (N is an exponent of two) # NB: hashlib.scrypt in Python (>=v3.6) supports max N value of 14. This means that # for hash presets > 3 the standalone scrypt library must be used! + _hp = namedtuple('scrypt_preset',['N','r','p']) hash_presets = { - '1': [12, 8, 1], - '2': [13, 8, 4], - '3': [14, 8, 8], - '4': [15, 8, 12], - '5': [16, 8, 16], - '6': [17, 8, 20], - '7': [18, 8, 24], + '1': _hp(12, 8, 1), + '2': _hp(13, 8, 4), + '3': _hp(14, 8, 8), + '4': _hp(15, 8, 12), + '5': _hp(16, 8, 16), + '6': _hp(17, 8, 20), + '7': _hp(18, 8, 24), } if os.getenv('MMGEN_TEST_SUITE'): diff --git a/mmgen/util.py b/mmgen/util.py index 6ae30a24..536e1697 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -426,7 +426,7 @@ def strip_comments(lines): def get_hash_params(hash_preset): if hash_preset in g.hash_presets: - return g.hash_presets[hash_preset] # N,p,r,buflen + return g.hash_presets[hash_preset] # N,r,p else: # Shouldn't be here die(3,f"{hash_preset}: invalid 'hash_preset' value") diff --git a/mmgen/wallet.py b/mmgen/wallet.py index 98a0bffc..4704a7d1 100755 --- a/mmgen/wallet.py +++ b/mmgen/wallet.py @@ -815,7 +815,7 @@ class MMGenWallet(WalletEnc): if opt.hash_preset and opt.hash_preset != hp: qmsg('Warning: ignoring user-requested hash preset {opt.hash_preset}') - hash_params = list(map(int,hpdata[1:])) + hash_params = tuple(map(int,hpdata[1:])) if hash_params != get_hash_params(d.hash_preset): msg(f"Hash parameters {' '.join(hash_params)!r} don't match hash preset {d.hash_preset!r}")