From bb96d6223fe05c37da1fabfa9b33d3e74c28ab82 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 27 Jan 2022 11:08:07 +0000 Subject: [PATCH] lazy imports of hashlib, string --- mmgen/baseconv.py | 2 +- mmgen/cfg.py | 3 ++- mmgen/crypto.py | 5 ++++- mmgen/util.py | 10 +++++++--- mmgen/wallet.py | 4 ++++ 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/mmgen/baseconv.py b/mmgen/baseconv.py index 157fa670..2681964b 100755 --- a/mmgen/baseconv.py +++ b/mmgen/baseconv.py @@ -20,7 +20,6 @@ baseconv.py: base conversion class for the MMGen suite """ -from hashlib import sha256 from collections import namedtuple from .exception import BaseConversionError,BaseConversionPadError,HexadecimalStringError,SeedLengthError @@ -92,6 +91,7 @@ class baseconv(object): return self.digits def get_wordlist_chksum(self): + from hashlib import sha256 return sha256( ' '.join(self.digits).encode() ).hexdigest()[:8] def check_wordlist(self): diff --git a/mmgen/cfg.py b/mmgen/cfg.py index 1c8e7e57..7dc1dd36 100755 --- a/mmgen/cfg.py +++ b/mmgen/cfg.py @@ -24,7 +24,7 @@ cfg.py: API for the MMGen runtime configuration file and related files # override_from_env() during init, so global config vars that are set from the environment # (such as g.test_suite) cannot be used here. -import sys,os,re,hashlib +import sys,os,re from collections import namedtuple from .globalvars import * @@ -111,6 +111,7 @@ class CfgFileSample(CfgFile): @staticmethod def compute_chksum(data): + import hashlib return hashlib.new('ripemd160','\n'.join(data).encode()).hexdigest() @property diff --git a/mmgen/crypto.py b/mmgen/crypto.py index 01ba295c..76ce1822 100755 --- a/mmgen/crypto.py +++ b/mmgen/crypto.py @@ -21,7 +21,6 @@ crypto.py: Random number, password hashing and symmetric encryption routines for """ import os -from hashlib import sha256 from collections import namedtuple from .globalvars import g @@ -70,6 +69,7 @@ def get_hash_params(hash_preset): die(3,f"{hash_preset}: invalid 'hash_preset' value") def sha256_rounds(s,n): + from hashlib import sha256 for i in range(n): s = sha256(s).digest() return s @@ -251,6 +251,7 @@ def add_user_random(rand_bytes,desc): assert type(rand_bytes) == bytes, 'add_user_random_chk1' if opt.usr_randchars: if not g.user_entropy: + from hashlib import sha256 g.user_entropy = sha256(_get_random_data_from_user(opt.usr_randchars,desc)).digest() urand_desc = 'user-supplied entropy' else: @@ -341,6 +342,7 @@ def mmgen_encrypt(data,desc='data',hash_preset=None): hash_preset = hp, passwd_file = opt.passwd_file ) key = make_key(passwd,salt,hp) + from hashlib import sha256 enc_d = encrypt_data( sha256(nonce+data).digest() + nonce + data, key, iv, desc=desc ) return salt+iv+enc_d @@ -359,6 +361,7 @@ def mmgen_decrypt(data,desc='data',hash_preset=None): key = make_key(passwd,salt,hp) dec_d = decrypt_data( enc_d, key, iv, desc ) sha256_len = 32 + from hashlib import sha256 if dec_d[:sha256_len] == sha256(dec_d[sha256_len:]).digest(): vmsg('OK') return dec_d[sha256_len+mmenc_nonce_len:] diff --git a/mmgen/util.py b/mmgen/util.py index bc0fc66e..645939c5 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -21,8 +21,6 @@ util.py: Low-level routines imported by other modules in the MMGen suite """ import sys,os,time,re -from hashlib import sha256 -from string import hexdigits,digits from .color import * from .globalvars import g @@ -276,7 +274,6 @@ def int2bytespec(n,spec,fmt,print_sym=True): return '{:{}f}{}'.format( n / spec2int(spec), fmt, spec if print_sym else '' ) def parse_bytespec(nbytes): - import re m = re.match(r'([0123456789.]+)(.*)',nbytes) if m: if m.group(2): @@ -331,16 +328,20 @@ def make_chksum_N(s,nchars,sep=False): s = s.encode() if nchars%4 or not (4 <= nchars <= 64): return False + from hashlib import sha256 s = sha256(sha256(s).digest()).hexdigest().upper() sep = ('',' ')[bool(sep)] return sep.join([s[i*4:i*4+4] for i in range(nchars//4)]) def make_chksum_8(s,sep=False): from .obj import HexStr + from hashlib import sha256 s = HexStr(sha256(sha256(s).digest()).hexdigest()[:8].upper(),case='upper') return '{} {}'.format(s[:4],s[4:]) if sep else s + def make_chksum_6(s): from .obj import HexStr + from hashlib import sha256 if isinstance(s,str): s = s.encode() return HexStr(sha256(s).hexdigest()[:6]) @@ -349,6 +350,7 @@ def is_chksum_6(s): return len(s) == 6 and is_hex_str_lc(s) def make_iv_chksum(s): + from hashlib import sha256 return sha256(s).hexdigest()[:8].upper() def splitN(s,n,sep=None): # always return an n-element list @@ -406,9 +408,11 @@ def is_int(s): return False def is_hex_str(s): + from string import hexdigits return set(list(s.lower())) <= set(list(hexdigits.lower())) def is_hex_str_lc(s): + from string import hexdigits return set(list(s)) <= set(list(hexdigits.lower())) def is_utf8(s): diff --git a/mmgen/wallet.py b/mmgen/wallet.py index 9c1735f6..13003379 100755 --- a/mmgen/wallet.py +++ b/mmgen/wallet.py @@ -350,6 +350,7 @@ class WalletEnc(Wallet): else: self._get_new_passphrase() + from hashlib import sha256 d.salt = sha256( crypto.get_random(128) ).digest()[:crypto.salt_len] key = crypto.make_key( d.passwd, d.salt, d.hash_preset ) d.key_id = make_chksum_8(key) @@ -911,6 +912,7 @@ to exit and re-run the program with the '--old-incog-fmt' option. } def _make_iv_chksum(self,s): + from hashlib import sha256 return sha256(s).hexdigest()[:8].upper() def _get_incog_data_len(self,seed_len): @@ -952,6 +954,7 @@ to exit and re-run the program with the '--old-incog-fmt' option. d.salt = crypto.get_random( crypto.salt_len ) key = crypto.make_key( d.passwd, d.salt, d.hash_preset, 'incog wallet key' ) + from hashlib import sha256 chk = sha256(self.seed.data).digest()[:8] d.enc_seed = crypto.encrypt_data( chk + self.seed.data, @@ -1001,6 +1004,7 @@ to exit and re-run the program with the '--old-incog-fmt' option. def _verify_seed_newfmt(self,data): chk,seed = data[:8],data[8:] + from hashlib import sha256 if sha256(seed).digest()[:8] == chk: qmsg('Passphrase{} are correct'.format( self.msg['dec_chk'].format('and') )) return seed