Browse Source

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

The MMGen Project 3 years ago
parent
commit
73c3a06e29
3 changed files with 20 additions and 16 deletions
  1. 2 9
      mmgen/main_autosign.py
  2. 3 6
      mmgen/main_wallet.py
  3. 15 1
      mmgen/util.py

+ 2 - 9
mmgen/main_autosign.py

@@ -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...'):

+ 3 - 6
mmgen/main_wallet.py

@@ -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'

+ 15 - 1
mmgen/util.py

@@ -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)