Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in charset_to_int #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions secretsharing/charset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def int_to_charset(x, charset):
x, digit = divmod(x, len(charset))
output += charset[digit]
# reverse the characters in the output and return
return output[::-1]
return output

def charset_to_int(s, charset):
""" Turn a string into a non-negative integer.
Expand All @@ -31,8 +31,10 @@ def charset_to_int(s, charset):
if (set(s) - set(charset)):
raise ValueError("s has chars that aren't in the charset.")
output = 0
i=0
for char in s:
output = output * len(charset) + charset.index(char)
output = output + (len(charset)**i)*charset.index(char)
i+=1
return output

def change_charset(s, original_charset, target_charset):
Expand Down
8 changes: 8 additions & 0 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ def split_and_recover_secret(self, sharer_class, m, n, secret):
def test_hex_to_hex_sharing(self):
recovered_secret = self.split_and_recover_secret(SecretSharer, 3, 5,
"c4bbcb1fbec99d65bf59d85c8cb62ee2db963f0fe106f483d9afa73bd4e39a8a")

def test_hex_with_zero_to_hex_sharing(self):
recovered_secret = self.split_and_recover_secret(SecretSharer, 3, 5,
"000ac4bbcb1fbec99d65bf59d85c8cb62ee2db963f0fe106f483d9afa73bd4e39a8a")

def test_printable_ascii_to_hex_sharing(self):
recovered_secret = self.split_and_recover_secret(PlaintextToHexSecretSharer, 3, 5,
"correct horse battery staple")

def test_printable_with_zero_ascii_to_hex_sharing(self):
recovered_secret = self.split_and_recover_secret(PlaintextToHexSecretSharer, 3, 5,
"000correct horse battery staple")

def test_b58_to_b32_sharing(self):
recovered_secret = self.split_and_recover_secret(BitcoinToB32SecretSharer, 3, 5,
Expand Down