Changeset - 37a1df17b9a1
[Not reviewed]
default
0 7 0
Laman - 4 years ago 2020-04-10 12:49:31

changed naming to snake case. sadly no love for camel case in this world
7 files changed with 98 insertions and 98 deletions:
0 comments (0 inline, 0 general)
src/cli.py
Show inline comments
 
@@ -6,63 +6,63 @@ from shamira import generate, reconstruc
 

	
 

	
 
def run():
 
	parser = ArgumentParser()
 
	subparsers = parser.add_subparsers()
 

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

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

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

	
 

	
 
def buildSplitParser(parser):
 
def build_split_parser(parser):
 
	parser.add_argument("-k", type=int, required=True, help="number of shares necessary for recovering the secret")
 
	parser.add_argument("-n", type=int, required=True, help="number of generated shares")
 

	
 
	encoding=parser.add_mutually_exclusive_group()
 
	encoding = parser.add_mutually_exclusive_group()
 
	encoding.add_argument("--hex", action="store_true", help="encode shares' bytes as a hexadecimal string")
 
	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.set_defaults(func=_generate)
 
	
 

	
 
def buildJoinParser(parser):
 
	encoding=parser.add_mutually_exclusive_group()
 
def build_join_parser(parser):
 
	encoding = parser.add_mutually_exclusive_group()
 
	encoding.add_argument("--hex", action="store_true", help="decode shares' bytes from a hexadecimal string")
 
	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.set_defaults(func=_reconstruct)
 

	
 

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

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

	
 

	
 
def _reconstruct(args):
 
	encoding = getEncoding(args)
 
	encoding = get_encoding(args)
 
	try:
 
		print(reconstruct(*args.share, encoding=encoding, raw=args.raw))
 
	except SException as e:
 
		print(e)
 

	
 

	
 
def getEncoding(args):
 
def get_encoding(args):
 
	if args.hex: return "hex"
 
	elif args.b32: return "b32"
 
	elif args.b64: return "b64"
 
	else: return ""
src/condensed.py
Show inline comments
 
@@ -27,13 +27,13 @@ for i in range(256):
 
	L[acc] = i
 
	acc = gfmul(acc, g)
 
L[1] = 0
 
inv = [E[255-L[i]] if i!=0 else None for i in range(256)]  # multiplicative inverse
 

	
 

	
 
def getConstantCoef(*points):
 
def get_constant_coef(*points):
 
	"""Compute constant polynomial coefficient given the points.
 

	
 
	See https://en.wikipedia.org/wiki/Shamir's_Secret_Sharing#Computationally_Efficient_Approach"""
 
	k = len(points)
 
	res = 0
 
	for i in range(k):
 
@@ -52,47 +52,47 @@ import base64
 
import binascii
 

	
 

	
 
class SException(Exception): pass
 

	
 

	
 
def reconstructRaw(*shares):
 
def reconstruct_raw(*shares):
 
	"""Tries to recover the secret from its shares.
 

	
 
	:param shares: ((i, (bytes) share), ...)
 
	:return: (bytes) reconstructed secret. Too few shares returns garbage."""
 
	secretLen = len(shares[0][1])
 
	res = [None]*secretLen
 
	for i in range(secretLen):
 
	secret_len = len(shares[0][1])
 
	res = [None]*secret_len
 
	for i in range(secret_len):
 
		points = [(x, s[i]) for (x, s) in shares]
 
		res[i] = (getConstantCoef(*points))
 
		res[i] = (get_constant_coef(*points))
 
	return bytes(res)
 

	
 

	
 
def reconstruct(*shares):
 
	"""Wraps reconstructRaw.
 
	"""Wraps reconstruct_raw.
 

	
 
	:param shares: ((str) share, ...)
 
	:return: (str) reconstructed secret. Too few shares returns garbage."""
 

	
 
	bs = reconstructRaw(*(decode(s) for s in shares))
 
	bs = reconstruct_raw(*(decode(s) for s in shares))
 
	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):
 
	try:
 
		(i, _, shareStr) = share.partition(".")
 
		(i, _, share_str) = share.partition(".")
 
		i = int(i)
 
		if not 1<=i<=255:
 
			raise SException("Malformed share: Failed 1<=k<=255, k={0}".format(i))
 

	
 
		shareBytes = base64.b32decode(shareStr)
 
		return (i, shareBytes)
 
		share_bytes = base64.b32decode(share_str)
 
		return (i, share_bytes)
 
	except (ValueError, binascii.Error):
 
		raise SException('Malformed share: share="{0}"'.format(share))
 

	
 
###
 

	
 
from argparse import ArgumentParser
src/gf256.py
Show inline comments
 
@@ -20,13 +20,13 @@ L = [None]*256  # logarithms
 
acc = 1
 
for i in range(256):
 
	E[i] = acc
 
	L[acc] = i
 
	acc = _gfmul(acc, g)
 
L[1] = 0
 
inv = [E[255-L[i]] if i != 0 else None for i in range(256)]  # multiplicative inverse
 
INV = [E[255-L[i]] if i!=0 else None for i in range(256)]  # multiplicative inverse
 

	
 

	
 
def gfmul(a, b):
 
	"""Fast multiplication. Basic multiplication is expensive. a*b==g**(log(a)+log(b))"""
 
	assert 0<=a<=255, 0<=b<=255
 
	if a==0 or b==0: return 0
 
@@ -37,28 +37,28 @@ def gfmul(a, b):
 

	
 
def evaluate(coefs, x):
 
	"""Evaluate polynomial's value at x.
 

	
 
	:param coefs: [a0, a1, ...]."""
 
	res = 0
 
	xK = 1
 
	xk = 1
 
	for a in coefs:
 
		res ^= gfmul(a, xK)
 
		xK = gfmul(xK, x)
 
		res ^= gfmul(a, xk)
 
		xk = gfmul(xk, x)
 
	return res
 

	
 

	
 
def getConstantCoef(*points):
 
def get_constant_coef(*points):
 
	"""Compute constant polynomial coefficient given the points.
 

	
 
	See https://en.wikipedia.org/wiki/Shamir's_Secret_Sharing#Computationally_Efficient_Approach"""
 
	k = len(points)
 
	res = 0
 
	for i in range(k):
 
		(x, y) = points[i]
 
		prod = 1
 
		for j in range(k):
 
			if i==j: continue
 
			(xj, yj) = points[j]
 
			prod = gfmul(prod, (gfmul(xj, inv[xj^x])))
 
			prod = gfmul(prod, (gfmul(xj, INV[xj^x])))
 
		res ^= gfmul(y, prod)
 
	return res
src/shamira.py
Show inline comments
 
@@ -12,70 +12,70 @@ class SException(Exception): pass
 
class InvalidParams(SException): pass
 
class DetectionException(SException): pass
 
class DecodingException(SException): pass
 
class MalformedShare(SException): pass
 

	
 

	
 
def _shareByte(secretB, k, n):
 
def _share_byte(secret_b, k, n):
 
	if not k<=n<255:
 
		raise InvalidParams("Failed k<=n<255, k={0}, n={1}".format(k, n))
 
	# we might be concerned with zero coefficients degenerating our polynomial, but there's no reason - we still need k shares to determine it is the case
 
	coefs = [int(secretB)]+[int(b) for b in os.urandom(k-1)]
 
	coefs = [int(secret_b)]+[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):
 
def generate_raw(secret, k, n):
 
	"""Splits secret into shares.
 

	
 
	:param secret: (bytes)
 
	:param k: number of shares necessary for secret recovery. 1 <= k <= n
 
	:param n: (int) number of shares generated. 1 <= n < 255
 
	:return: [(i, (bytes) share), ...]"""
 
	shares = [_shareByte(b, k, n) for b in secret]
 
	shares = [_share_byte(b, k, n) for b in secret]
 
	return [(i+1, bytes([s[i] for s in shares])) for i in range(n)]
 

	
 

	
 
def reconstructRaw(*shares):
 
def reconstruct_raw(*shares):
 
	"""Tries to recover the secret from its shares.
 

	
 
	:param shares: ((i, (bytes) share), ...)
 
	:return: (bytes) reconstructed secret. Too few shares returns garbage."""
 
	secretLen = len(shares[0][1])
 
	res = [None]*secretLen
 
	for i in range(secretLen):
 
	secret_len = len(shares[0][1])
 
	res = [None]*secret_len
 
	for i in range(secret_len):
 
		points = [(x, s[i]) for (x, s) in shares]
 
		res[i] = (gf256.getConstantCoef(*points))
 
		res[i] = (gf256.get_constant_coef(*points))
 
	return bytes(res)
 

	
 

	
 
def generate(secret, k, n, encoding="b32"):
 
	"""Wraps generateRaw().
 
	"""Wraps generate_raw().
 

	
 
	:param secret: (str or bytes)
 
	:param k: number of shares necessary for secret recovery
 
	:param n: number of shares generated
 
	:param encoding: {hex, b32, b64} desired output encoding. Hexadecimal, Base32 or Base64.
 
	:return: [(str) share, ...]"""
 
	if isinstance(secret,str):
 
		secret = secret.encode("utf-8")
 
	shares = generateRaw(secret, k, n)
 
	shares = generate_raw(secret, k, n)
 
	return [encode(s, encoding) for s in shares]
 

	
 

	
 
def reconstruct(*shares, encoding="", raw=False):
 
	"""Wraps reconstructRaw.
 
	"""Wraps reconstruct_raw.
 

	
 
	:param shares: ((str) share, ...)
 
	:param encoding: {hex, b32, b64, ""} encoding of share strings. If not provided or empty, the function tries to guess it.
 
	:param raw: (bool) whether to return bytes (True) or str (False)
 
	:return: (str or bytes) reconstructed secret. Too few shares returns garbage."""
 
	if not encoding:
 
		encoding = detectEncoding(shares)
 
		encoding = detect_encoding(shares)
 

	
 
	bs = reconstructRaw(*(decode(s, encoding) for s in shares))
 
	bs = reconstruct_raw(*(decode(s, encoding) for s in shares))
 
	try:
 
		return bs if raw else bs.decode(encoding="utf-8")
 
	except UnicodeDecodeError:
 
		raise DecodingException('Failed to decode bytes to utf-8. Either you supplied invalid shares, or you missed the "raw" flag. Offending value: {0}'.format(bs))
 

	
 

	
 
@@ -86,26 +86,26 @@ def encode(share, encoding="b32"):
 
	(i, bs) = share
 
	return "{0}.{1}".format(i, f(bs).decode("utf-8"))
 

	
 

	
 
def decode(share, encoding="b32"):
 
	try:
 
		(i, _, shareStr) = share.partition(".")
 
		(i, _, share_str) = share.partition(".")
 
		i = int(i)
 
		if not 1<=i<=255:
 
			raise MalformedShare("Malformed share: Failed 1<=k<=255, k={0}".format(i))
 
		if encoding=="hex": f = base64.b16decode
 
		elif encoding=="b32": f = base64.b32decode
 
		else: f = base64.b64decode
 
		shareBytes = f(shareStr)
 
		return (i, shareBytes)
 
		share_bytes = f(share_str)
 
		return (i, share_bytes)
 
	except (ValueError, binascii.Error):
 
		raise MalformedShare('Malformed share: share="{0}", encoding="{1}"'.format(share, encoding))
 

	
 

	
 
def detectEncoding(shares):
 
def detect_encoding(shares):
 
	classes = [
 
		(re.compile(r"\d+\.([0-9A-F]{2})+"), "hex"),
 
		(re.compile(r"\d+\.([A-Z2-7]{8})*([A-Z2-7]{8}|[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}={1})"), "b32"),
 
		(re.compile(r"\d+\.([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}={2}|[A-Za-z0-9+/]{3}={1})"), "b64")
 
	]
 
	for (regexp, res) in classes:
src/tests/test_condensed.py
Show inline comments
 
@@ -2,13 +2,13 @@
 

	
 
import os
 
import random
 
from unittest import TestCase
 

	
 
from gf256 import _gfmul, evaluate
 
from shamira import generateRaw, generate
 
from shamira import generate_raw, generate
 
from condensed import *
 

	
 

	
 
class TestCondensed(TestCase):
 
	_urandom = os.urandom
 

	
 
@@ -18,53 +18,53 @@ class TestCondensed(TestCase):
 
		os.urandom = lambda n: bytes(random.randint(0, 255) for i in range(n))
 

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

	
 
	def testGfmul(self):
 
	def test_gfmul(self):
 
		for a in range(256):
 
			for b in range(256):
 
				self.assertEqual(_gfmul(a, b), gfmul(a, b))
 

	
 
	def testGetConstantCoef(self):
 
		self.assertEqual(getConstantCoef((1, 1), (2, 2), (3, 3)), 0)
 
	def test_get_constant_coef(self):
 
		self.assertEqual(get_constant_coef((1, 1), (2, 2), (3, 3)), 0)
 

	
 
		random.seed(17)
 
		randomMatches = 0
 
		random_matches = 0
 
		for i in range(10):
 
			k = random.randint(2, 255)
 

	
 
			# exact
 
			res = self.checkCoefsMatch(k, k)
 
			res = self.check_coefs_match(k, k)
 
			self.assertEqual(res[0], res[1])
 

	
 
			# overdetermined
 
			res = self.checkCoefsMatch(k, 256)
 
			res = self.check_coefs_match(k, 256)
 
			self.assertEqual(res[0], res[1])
 

	
 
			# underdetermined => random
 
			res = self.checkCoefsMatch(k, k-1)
 
			res = self.check_coefs_match(k, k-1)
 
			if res[0]==res[1]:
 
				randomMatches += 1
 
		self.assertLess(randomMatches, 2)  # with a chance (255/256)**10=0.96 there should be no match
 
				random_matches += 1
 
		self.assertLess(random_matches, 2)  # with a chance (255/256)**10=0.96 there should be no match
 

	
 
	def checkCoefsMatch(self, k, m):
 
	def check_coefs_match(self, k, m):
 
		coefs = [random.randint(0, 255) for i in range(k)]
 
		points = [(j, evaluate(coefs, j)) for j in range(1, 256)]
 
		random.shuffle(points)
 
		return (getConstantCoef(*points[:m]), coefs[0])
 
		return (get_constant_coef(*points[:m]), coefs[0])
 

	
 
	def testGenerateReconstructRaw(self):
 
	def test_generate_reconstruct_raw(self):
 
		for (k, n) in [(2, 3), (254, 254)]:
 
			shares = generateRaw(b"abcd", k, n)
 
			shares = generate_raw(b"abcd", k, n)
 
			random.shuffle(shares)
 
			self.assertEqual(reconstructRaw(*shares[:k]), b"abcd")
 
			self.assertNotEqual(reconstructRaw(*shares[:k-1]), b"abcd")
 
			self.assertEqual(reconstruct_raw(*shares[:k]), b"abcd")
 
			self.assertNotEqual(reconstruct_raw(*shares[:k-1]), b"abcd")
 

	
 
	def testGenerateReconstruct(self):
 
	def test_generate_reconstruct(self):
 
		for secret in ["abcde", "ěščřžý"]:
 
			for (k, n) in [(2, 3), (254, 254)]:
 
				with self.subTest(sec=secret, k=k, n=n):
 
					shares = generate(secret, k, n)
 
					random.shuffle(shares)
 
					self.assertEqual(reconstruct(*shares[:k]), secret)
 
@@ -73,13 +73,13 @@ class TestCondensed(TestCase):
 
					except SException:
 
						pass
 
		shares = generate(b"\xfeaa", 2, 3)
 
		with self.assertRaises(SException):
 
			reconstruct(*shares)
 

	
 
	def testDecode(self):
 
	def test_decode(self):
 
		with self.assertRaises(SException):
 
			decode("AAA")
 
			decode("1.")
 
			decode(".AAA")
 
			decode("1AAA")
 
			decode("1.AAAQEAY")
src/tests/test_gf256.py
Show inline comments
 
@@ -6,57 +6,57 @@ from unittest import TestCase
 

	
 
from gf256 import _gfmul
 
from gf256 import *
 

	
 

	
 
class TestGF256(TestCase):
 
	def test_gfmul(self):
 
	def test__gfmul(self):
 
		self.assertEqual(_gfmul(0, 0), 0)
 
		self.assertEqual(_gfmul(1, 1), 1)
 
		self.assertEqual(_gfmul(2, 2), 4)
 
		self.assertEqual(_gfmul(0, 21), 0)
 
		self.assertEqual(_gfmul(0x53, 0xca), 0x01)
 
		self.assertEqual(_gfmul(0xff, 0xff), 0x13)
 

	
 
	def testGfmul(self):
 
	def test_gfmul(self):
 
		for a in range(256):
 
			for b in range(256):
 
				self.assertEqual(_gfmul(a, b), gfmul(a, b))
 

	
 
	def testEvaluate(self):
 
	def test_evaluate(self):
 
		for x in range(256):
 
			(a0, a1, a2, a3) = (x, x>>1, x>>2, x>>3)
 
			self.assertEqual(evaluate([17], x), 17)  # constant polynomial
 
			self.assertEqual(evaluate([a0, a1, a2, a3], 0), x)  # any polynomial at 0
 
			self.assertEqual(evaluate([a0, a1, a2, a3], 1), a0^a1^a2^a3)  # polynomial at 1 == sum of coefficients
 

	
 
	def testGetConstantCoef(self):
 
		self.assertEqual(getConstantCoef((1, 1), (2, 2), (3, 3)), 0)
 
	def test_get_constant_coef(self):
 
		self.assertEqual(get_constant_coef((1, 1), (2, 2), (3, 3)), 0)
 

	
 
		random.seed(17)
 
		randomMatches = 0
 
		random_matches = 0
 
		for i in range(10):
 
			k = random.randint(2, 255)
 

	
 
			# exact
 
			res = self.checkCoefsMatch(k, k)
 
			res = self.check_coefs_match(k, k)
 
			self.assertEqual(res[0], res[1])
 

	
 
			# overdetermined
 
			res = self.checkCoefsMatch(k, 256)
 
			res = self.check_coefs_match(k, 256)
 
			self.assertEqual(res[0], res[1])
 

	
 
			# underdetermined => random
 
			res = self.checkCoefsMatch(k, k-1)
 
			res = self.check_coefs_match(k, k-1)
 
			if res[0]==res[1]:
 
				randomMatches += 1
 
		self.assertLess(randomMatches, 2)  # with a chance (255/256)**10=0.96 there should be no match
 
				random_matches += 1
 
		self.assertLess(random_matches, 2)  # with a chance (255/256)**10=0.96 there should be no match
 

	
 
	def checkCoefsMatch(self, k, m):
 
	def check_coefs_match(self, k, m):
 
		coefs = [random.randint(0, 255) for i in range(k)]
 
		points = [(j, evaluate(coefs, j)) for j in range(1, 256)]
 
		random.shuffle(points)
 
		return (getConstantCoef(*points[:m]), coefs[0])
 
		return (get_constant_coef(*points[:m]), coefs[0])
 

	
 

	
 
if __name__=='__main__':
 
	unittest.main()
src/tests/test_shamira.py
Show inline comments
 
# GNU GPLv3, see LICENSE
 

	
 
import random
 
from unittest import TestCase
 

	
 
from shamira import _shareByte
 
from shamira import _share_byte
 
from shamira import *
 

	
 

	
 
class TestShamira(TestCase):
 
	_urandom = os.urandom
 

	
 
@@ -16,34 +16,34 @@ class TestShamira(TestCase):
 
		os.urandom = lambda n: bytes(random.randint(0, 255) for i in range(n))
 

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

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

	
 
		vals = _shareByte(ord(b"a"), 2, 3)
 
		vals = _share_byte(ord(b"a"), 2, 3)
 
		points = list(zip(range(1, 256), vals))
 
		self.assertEqual(gf256.getConstantCoef(*points), ord(b"a"))
 
		self.assertEqual(gf256.getConstantCoef(*points[:2]), ord(b"a"))
 
		self.assertNotEqual(gf256.getConstantCoef(*points[:1]), ord(b"a"))  # underdetermined => random
 
		self.assertEqual(gf256.get_constant_coef(*points), ord(b"a"))
 
		self.assertEqual(gf256.get_constant_coef(*points[:2]), ord(b"a"))
 
		self.assertNotEqual(gf256.get_constant_coef(*points[:1]), ord(b"a"))  # underdetermined => random
 

	
 
	def testGenerateReconstructRaw(self):
 
	def test_generate_reconstruct_raw(self):
 
		for (k, n) in [(2, 3), (254, 254)]:
 
			shares = generateRaw(b"abcd", k, n)
 
			shares = generate_raw(b"abcd", k, n)
 
			random.shuffle(shares)
 
			self.assertEqual(reconstructRaw(*shares[:k]), b"abcd")
 
			self.assertNotEqual(reconstructRaw(*shares[:k-1]), b"abcd")
 
			self.assertEqual(reconstruct_raw(*shares[:k]), b"abcd")
 
			self.assertNotEqual(reconstruct_raw(*shares[:k-1]), b"abcd")
 

	
 
	def testGenerateReconstruct(self):
 
	def test_generate_reconstruct(self):
 
		for encoding in ["hex", "b32", "b64"]:
 
			for secret in [b"abcd", "abcde", "ěščřžý"]:
 
				for (k, n) in [(2, 3), (254, 254)]:
 
					raw = isinstance(secret, bytes)
 
					with self.subTest(enc=encoding, r=raw, sec=secret, k=k, n=n):
 
						shares = generate(secret, k, n, encoding)
 
@@ -53,19 +53,19 @@ class TestShamira(TestCase):
 
						s = secret if raw else secret.encode("utf-8")
 
						self.assertNotEqual(reconstruct(*shares[:k-1], encoding=encoding, raw=True), s)
 
		shares = generate(b"\xfeaa", 2, 3)
 
		with self.assertRaises(DecodingException):
 
			reconstruct(*shares)
 

	
 
	def testEncode(self):
 
	def test_encode(self):
 
		share = (2, b"\x00\x01\x02")
 
		for (encoding, encodedStr) in [("hex", '000102'), ("b32", 'AAAQE==='), ("b64", 'AAEC')]:
 
		for (encoding, encoded_str) in [("hex", '000102'), ("b32", 'AAAQE==='), ("b64", 'AAEC')]:
 
			with self.subTest(enc=encoding):
 
				self.assertEqual(encode(share, encoding), "2."+encodedStr)
 
				self.assertEqual(encode(share, encoding), "2."+encoded_str)
 

	
 
	def testDecode(self):
 
	def test_decode(self):
 
		with self.assertRaises(MalformedShare):
 
			decode("AAA")
 
			decode("1.")
 
			decode(".AAA")
 
			decode("1AAA")
 
			decode("1.0001020f", "hex")
 
@@ -92,11 +92,11 @@ class TestShamira(TestCase):
 
			["1.AAECAw==", "2.AAAQE==="],
 
			[".00010203"],  # no index
 
			["00010203"]  # no index
 
		]:
 
			with self.subTest(shares=shares):
 
				with self.assertRaises(DetectionException):
 
					detectEncoding(shares)
 
		self.assertEqual(detectEncoding(["10.00010203"]), "hex")
 
		self.assertEqual(detectEncoding(["2.AAAQEAY="]), "b32")
 
		self.assertEqual(detectEncoding(["3.AAECAw=="]), "b64")
 
		self.assertEqual(detectEncoding(["3.AAECAwQF", "1.00010203"]), "b64")
 
					detect_encoding(shares)
 
		self.assertEqual(detect_encoding(["10.00010203"]), "hex")
 
		self.assertEqual(detect_encoding(["2.AAAQEAY="]), "b32")
 
		self.assertEqual(detect_encoding(["3.AAECAw=="]), "b64")
 
		self.assertEqual(detect_encoding(["3.AAECAwQF", "1.00010203"]), "b64")
0 comments (0 inline, 0 general)