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
 
@@ -12,7 +12,7 @@ def run():
 
	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)
 
@@ -45,14 +45,20 @@ def buildJoinParser(parser):
 
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):
src/condensed.py
Show inline comments
 
@@ -99,7 +99,7 @@ def run():
 

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

	
 
@@ -107,5 +107,10 @@ def run():
 
	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
 
@@ -9,7 +9,8 @@ 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
src/tests/test_shamira.py
Show inline comments
 
@@ -20,9 +20,9 @@ class TestShamira(TestCase):
 
		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)
0 comments (0 inline, 0 general)