Skip to content

Commit

Permalink
[Memo] unpad memo correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Mar 22, 2016
1 parent c426ad7 commit c57a07e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions graphenebase/memo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import hashlib
from binascii import hexlify, unhexlify
from Crypto.Cipher import AES
from .account import PrivateKey, PublicKey
from graphenebase.account import PrivateKey, PublicKey
import struct

" This class and the methods require python3 "
assert sys.version_info[0] == 3, "graphenelib requires python3"
Expand Down Expand Up @@ -51,11 +52,17 @@ def init_aes(shared_secret, nonce) :


def _pad(s, BS):
import struct
numBytes = (BS - len(s) % BS)
return s + numBytes * struct.pack('B', numBytes)


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


def encode_memo(priv, pub, nonce, message) :
""" Encode a message with a shared secret between Alice and Bob
Expand Down Expand Up @@ -103,7 +110,7 @@ def decode_memo(priv, pub, nonce, message) :
" TODO, verify checksum "
message = cleartext[4:]
try :
return message.decode('utf8').strip()
return _unpad(message.decode('utf8'), 16)
except :
raise ValueError(message)

Expand Down

0 comments on commit c57a07e

Please sign in to comment.