Changeset - 735b9c2a61e9
[Not reviewed]
default
0 4 0
Laman - 7 years ago 2017-10-01 17:51:06

cli catching thrown exceptions
4 files changed with 17 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/cli.py
Show inline comments
 
@@ -9,13 +9,13 @@ def run():
 
	parser=ArgumentParser()
 
	subparsers=parser.add_subparsers()
 

	
 
	buildSplitParser(subparsers.add_parser("split"))
 
	buildJoinParser(subparsers.add_parser("join"))
 

	
 
	parser.set_defaults(func=lambda: parser.error("missing command"))
 
	parser.set_defaults(func=lambda _: parser.error("missing command"))
 

	
 
	args=parser.parse_args()
 
	args.func(args)
 

	
 

	
 
def buildSplitParser(parser):
 
@@ -42,20 +42,26 @@ def buildJoinParser(parser):
 
	parser.set_defaults(func=_reconstruct)
 

	
 

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

	
 
	try:
 
	shares=generate(args.secret,args.k,args.n,encoding)
 
	for s in shares:
 
		print(s)
 
	except ValueError as e:
 
		print("operation failed: ",e)
 

	
 

	
 
def _reconstruct(args):
 
	encoding=getEncoding(args)
 
	try:
 
	print(reconstruct(*args.share,encoding=encoding,raw=args.raw))
 
	except ValueError as e:
 
		print("operation failed: ",e)
 

	
 

	
 
def getEncoding(args):
 
	if args.hex: return "hex"
 
	elif args.b32: return "b32"
 
	elif args.b64: return "b64"
src/condensed.py
Show inline comments
 
@@ -96,16 +96,21 @@ from argparse import ArgumentParser
 
def run():
 
	parser=ArgumentParser()
 
	subparsers=parser.add_subparsers()
 

	
 
	joiner=subparsers.add_parser("join")
 
	joiner.add_argument("share",nargs="+",help="shares to be joined")
 
	joiner.set_defaults(func=lambda args: print(reconstruct(*args.share)))
 
	joiner.set_defaults(func=_reconstruct)
 

	
 
	parser.set_defaults(func=lambda: parser.error("missing command"))
 

	
 
	args=parser.parse_args()
 
	args.func(args)
 

	
 

	
 
def _reconstruct(args):
 
	try: print(reconstruct(*args.share))
 
	except ValueError as e: print("operation failed: ",e)
 

	
 

	
 
if __name__=="__main__":
 
	run()
src/shamira.py
Show inline comments
 
@@ -6,13 +6,14 @@ import base64
 
import binascii
 

	
 
import gf256
 

	
 

	
 
def _shareByte(secretB,k,n):
 
	assert k<=n<255
 
	if not k<=n<255:
 
		raise ValueError("failing k<=n<255, k={0}, n={1}".format(k,n))
 
	coefs=[int(secretB)]+[int(b) for b in os.urandom(k-1)]
 
	points=[gf256.evaluate(coefs,i) for i in range(1,n+1)]
 
	return points
 

	
 

	
 
def generateRaw(secret,k,n):
src/tests/test_shamira.py
Show inline comments
 
@@ -17,15 +17,15 @@ class TestShamira(TestCase):
 

	
 
	@classmethod
 
	def tearDownClass(cls):
 
		os.urandom=cls._urandom
 

	
 
	def test_shareByte(self):
 
		with self.assertRaises(AssertionError): # too few shares
 
		with self.assertRaises(ValueError): # too few shares
 
			_shareByte(b"a",5,4)
 
		with self.assertRaises(AssertionError): # too many shares
 
		with self.assertRaises(ValueError): # too many shares
 
			_shareByte(b"a",5,255)
 
		with self.assertRaises(ValueError): # not castable to int
 
			_shareByte("x",2,3)
 

	
 
		vals=_shareByte(ord(b"a"),2,3)
 
		points=list(zip(range(1,256), vals))
0 comments (0 inline, 0 general)