Skip to content

Commit

Permalink
Make memo _pad work with bytes, instead of strings.
Browse files Browse the repository at this point in the history
This simplifies the code and removes the ambiguity with
"last byte" fetching. If the message contains unicode characters
in the end, they will be dealt with correctly now.
  • Loading branch information
jhtitor committed Nov 28, 2018
1 parent 588b8e7 commit b3f1bda
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions bitsharesbase/memo.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def _pad(s, BS):


def _unpad(s, BS):
count = int(struct.unpack('B', bytes(s[-1], 'ascii'))[0])
if bytes(s[-count::], 'ascii') == count * struct.pack('B', count):
count = s[-1]
if s[-count::] == count * struct.pack('B', count):
return s[:-count]
return s

Expand Down Expand Up @@ -116,6 +116,7 @@ def decode_memo(priv, pub, nonce, message):
" TODO, verify checksum "
message = cleartext[4:]
try:
return _unpad(message.decode('utf8'), 16)
message = _unpad(message, 16)
except Exception as e:
raise ValueError(message)
return message.decode('utf8')
4 changes: 2 additions & 2 deletions tests/test_memo.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class Testcases(unittest.TestCase):
def test_padding(self):
for l in range(0, 255):
s = bytes(l * chr(l), 'utf-8')
padded = _pad(s, 16).decode('utf-8')
self.assertEqual(s.decode('utf-8'), _unpad(padded, 16))
padded = _pad(s, 16)
self.assertEqual(s, _unpad(padded, 16))

def test_decrypt(self):
for memo in test_cases:
Expand Down

0 comments on commit b3f1bda

Please sign in to comment.