util.py: add check_binary(), shred_file()

This commit is contained in:
The MMGen Project 2021-09-01 16:56:47 +00:00
commit 73c3a06e29
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 20 additions and 16 deletions

View file

@ -322,8 +322,8 @@ def wipe_existing_key():
try: os.stat(fn)
except: pass
else:
msg(f'\nWiping existing key {fn!r}')
run(['wipe','-cf',fn],check=True)
msg(f'\nShredding existing key {fn!r}')
shred_file( fn, verbose=opt.verbose )
def create_key():
kdata = os.urandom(32).hex()
@ -372,12 +372,6 @@ def get_insert_status():
except: return False
else: return True
def check_wipe_present():
try:
run(['wipe','-v'],stdout=DEVNULL,stderr=DEVNULL,check=True)
except:
die(2,"The 'wipe' utility must be installed before running this program")
async def do_loop():
n,prev_status = 0,False
if not opt.stealth_led:
@ -406,7 +400,6 @@ if len(cmd_args) == 1:
elif cmd != 'wait':
die(1,f'{cmd!r}: unrecognized command')
check_wipe_present()
wfs = get_wallet_files()
def at_exit(exit_val,message='\nCleaning up...'):

View file

@ -224,12 +224,9 @@ if invoked_as == 'passchg' and ss_in.infile.dirname == g.data_dir:
confirm_or_raise(m1,m2,exit_msg='Password not changed')
ss_out.write_to_file(desc='New wallet',outdir=g.data_dir)
bmsg('Securely deleting old wallet')
from subprocess import run
run(
['shred','--iterations=30','--zero','--remove=wipesync']
+ (['--verbose'] if opt.verbose else [])
+ [ss_in.infile.name],
check=True )
shred_file(
ss_in.infile.name,
verbose = opt.verbose )
else:
try:
assert invoked_as == 'gen', 'dw'

View file

@ -21,7 +21,7 @@ util.py: Low-level routines imported by other modules in the MMGen suite
"""
import sys,os,time,stat,re
from subprocess import run
from subprocess import run,PIPE,DEVNULL
from hashlib import sha256
from string import hexdigits,digits
from .color import *
@ -487,6 +487,20 @@ def compare_or_die(val1, desc1, val2, desc2, e='Error'):
dmsg('{} OK ({})'.format(capfirst(desc2),val2))
return True
def check_binary(args):
try:
run(args,stdout=DEVNULL,stderr=DEVNULL,check=True)
except:
rdie(2,f'{args[0]!r} binary missing, not in path, or not executable')
def shred_file(fn,verbose=False):
check_binary(['shred','--version'])
run(
['shred','--force','--iterations=30','--zero','--remove=wipesync']
+ (['--verbose'] if verbose else [])
+ [fn],
check=True )
def open_file_or_exit(filename,mode,silent=False):
try:
return open(filename, mode)