diff --git a/src/condensed.py b/src/condensed.py --- a/src/condensed.py +++ b/src/condensed.py @@ -52,6 +52,9 @@ import base64 import binascii +class SException(Exception): pass + + def reconstructRaw(*shares): """Tries to recover the secret from its shares. @@ -72,8 +75,10 @@ def reconstruct(*shares): :return: (str) reconstructed secret. Too few shares returns garbage.""" bs=reconstructRaw(*(decode(s) for s in shares)) - return bs.decode(encoding="utf-8") - + try: + return bs.decode(encoding="utf-8") + except UnicodeDecodeError: + raise SException('Failed to decode bytes to utf-8. Either you supplied invalid shares, or you missed the "raw" flag. Offending value: "{0}"'.format(bs)) def decode(share): @@ -81,12 +86,12 @@ def decode(share): (i,_,shareStr)=share.partition(".") i=int(i) if not 1<=i<=255: - raise ValueError() + raise SException("Malformed share: Failed 1<=k<=255, k={0}".format(i)) shareBytes=base64.b32decode(shareStr) return (i,shareBytes) except (ValueError,binascii.Error): - raise ValueError('bad share format: share="{0}"'.format(share)) + raise SException('Malformed share: share="{0}"'.format(share)) ### @@ -109,7 +114,7 @@ def run(): def _reconstruct(args): try: print(reconstruct(*args.share)) - except ValueError as e: print("operation failed: ",e) + except SException as e: print(e) if __name__=="__main__":