Changeset - 4cdad28c44d9
[Not reviewed]
default
0 1 0
Laman - 4 years ago 2020-04-13 12:08:44

added alternative input options
1 file changed with 27 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/cli.py
Show inline comments
 
# GNU GPLv3, see LICENSE
 

	
 
import sys
 
from argparse import ArgumentParser
 

	
 
from shamira import generate, reconstruct, SException
 
@@ -27,7 +28,8 @@ def build_split_parser(parser):
 
	encoding.add_argument("--b32", action="store_true", help="encode shares' bytes as a base32 string")
 
	encoding.add_argument("--b64", action="store_true", help="encode shares' bytes as a base64 string")
 

	
 
	parser.add_argument("secret", help="secret to be parsed")
 
	parser.add_argument("secret", nargs="?", help="a secret to be split. Can be provided on the command line,"
 
		" redirected through stdin, or will be asked for interactively.")
 
	parser.set_defaults(func=_generate)
 
	
 

	
 
@@ -37,16 +39,24 @@ def build_join_parser(parser):
 
	encoding.add_argument("--b32", action="store_true", help="decode shares' bytes from a base32 string")
 
	encoding.add_argument("--b64", action="store_true", help="decode shares' bytes from a base64 string")
 

	
 
	parser.add_argument("-r", "--raw", action="store_true", help="return secret as raw bytes")
 
	parser.add_argument("share", nargs="+", help="shares to be joined")
 
	parser.add_argument("-r", "--raw", action="store_true", help="return the secret as raw bytes")
 
	parser.add_argument("share", nargs="*", help="shares to be joined. Can be provided on the command line,"
 
		" redirected through stdin, or will be asked for interactively.")
 
	parser.set_defaults(func=_reconstruct)
 

	
 

	
 
def _generate(args):
 
	encoding = get_encoding(args) or "b32"
 

	
 
	if args.secret:  # provided as a positional argument
 
		secret = args.secret
 
	elif sys.stdin.isatty():  # input from terminal
 
		secret = input("Enter your secret:\n")
 
	else:  # redirected from other source
 
		secret = sys.stdin.read()
 

	
 
	try:
 
		shares = generate(args.secret, args.k, args.n, encoding)
 
		shares = generate(secret, args.k, args.n, encoding)
 
		for s in shares:
 
			print(s)
 
	except SException as e:
 
@@ -55,8 +65,20 @@ def _generate(args):
 

	
 
def _reconstruct(args):
 
	encoding = get_encoding(args)
 

	
 
	if args.share:  # provided as a positional argument
 
		shares = args.share
 
	elif sys.stdin.isatty():  # input from terminal
 
		print("Enter the shares, each on separate line, end with an empty line:")
 
		shares = []
 
		while not shares or shares[-1]:
 
			shares.append(input())
 
		shares.pop()
 
	else:  # redirected from other source
 
		shares = sys.stdin.read().split()
 

	
 
	try:
 
		print(reconstruct(*args.share, encoding=encoding, raw=args.raw))
 
		print(reconstruct(*shares, encoding=encoding, raw=args.raw))
 
	except SException as e:
 
		print(e)
 

	
0 comments (0 inline, 0 general)