コード例 #1
0
def shamir_test(l, m, n):
	s = Shamir('english')
	seed = "Shamir's Secret Sharing Scheme!"[:l] # take first l characters
	shares = s.split(seed, m, n)
	print('original:', seed)
	print('shares:')
	for i in range(len(shares)):
		print('%2d :' % (i + 1), shares[i])
	shares = shares[:m] # take first m shares
	cmb = s.combine(shares)
	print ('combined:', cmb)
	if seed == cmb:
		print('TEST OK')
		print()
	else:
		print('TEST FAILED !!!')
		sys.exit(1)
コード例 #2
0
def shamir_test(l, m, n):
    s = Shamir('english')
    seed = "Shamir's Secret Sharing Scheme!"[:l]  # take first l characters
    shares = s.split(seed, m, n)
    print('original:', seed)
    print('shares:')
    for i in range(len(shares)):
        print('%2d :' % (i + 1), shares[i])
    shares = shares[:m]  # take first m shares
    cmb = s.combine(shares)
    print('combined:', cmb)
    if seed == cmb:
        print('TEST OK')
        print()
    else:
        print('TEST FAILED !!!')
        sys.exit(1)
コード例 #3
0
class KeySplitter:
    def __init__(self, numshares, threshold):
        self.splitter = Shamir(numshares, threshold)
        self.numshares = numshares
        self.threshold = threshold

    def split(self, key):
        xshares = [''] * self.numshares
        yshares = [''] * self.numshares
        for char in key:
            xcords, ycords = self.splitter.split(ord(char))
            for idx in range(self.numshares):
                xshares[idx] += chr(xcords[idx])
                yshares[idx] += chr(ycords[idx])
        return zip(xshares, yshares)

    def jsonify(self, shares, threshold, split):
        data = {
            'shares': shares,
            'threshold': threshold,
            'split': [base64.b64encode(split[0]),
                      base64.b64encode(split[1])]
        }
        return json.dumps(data)
コード例 #4
0
    l = [0 for i in range(0, word_count)]
    l[0] = nb >> word_size * (word_count - 1)

    for i in range(1, word_count):
        l[i] = (nb - special_sum(l[0:i])) >> ((word_count - i - 1) * word_size)
    return l


key = list(map(lambda x: wordlist.index(x), key))
nb = get_integer_from_key(key)
checksum = get_checksum(nb)

rec = (nb << 4) + checksum
print(to_list_of_ints(rec, 132, 11))

shares = Shamir.split(t, n, nb)
shares = [
    to_list_of_ints((share[1]._value << 4) + get_checksum(share[1]._value),
                    132, 11) for share in shares
]
print([[wordlist[index_word] for index_word in share] for share in shares])

# reconstruction
print("reconstruction:")
rec = [(i + 1, get_integer_from_key(shares[i])) for i in range(0, len(shares))]
reconstructed = Shamir.combine(rec)._value
reconstructed_final_int = (reconstructed << 4) + get_checksum(reconstructed)
l = to_list_of_ints(reconstructed_final_int, 132, 11)
print([wordlist[l[i]] for i in range(0, len(l))])