encode/decode

This commit is contained in:
j 2014-09-09 12:08:04 +02:00
parent 26d3f1f8fb
commit 1e09e70574
7 changed files with 22 additions and 16 deletions

View file

@ -55,8 +55,8 @@ class Changelog(db.Model):
c.revision = cls.query.filter_by(user_id=user.id).count()
c.data = json.dumps([action] + list(args))
_data = str(c.revision) + str(c.timestamp) + c.data
_data = _data.encode('utf-8')
c.sig = settings.sk.sign(_data, encoding='base64')
_data = _data.encode()
c.sig = settings.sk.sign(_data, encoding='base64').decode()
state.db.session.add(c)
state.db.session.commit()
if state.nodes:
@ -80,9 +80,9 @@ class Changelog(db.Model):
next_revision = last.revision + 1 if last else 0
if revision == next_revision:
_data = str(revision) + str(timestamp) + data
_data = _data.encode('utf-8')
_data = _data.encode()
if rebuild:
sig = settings.sk.sign(_data, encoding='base64')
sig = settings.sk.sign(_data, encoding='base64').decode()
if valid(user.id, _data, sig):
c = cls()
c.created = datetime.utcnow()
@ -112,14 +112,14 @@ class Changelog(db.Model):
def verify(self):
_data = str(self.revision) + str(self.timestamp) + self.data
_data = _data.encode('utf-8')
return valid(self.user_id, _data, self.sig)
_data = _data.encode()
return valid(self.user_id, _data, self.sig.encode())
@classmethod
def _rebuild(cls):
for c in cls.query.filter_by(user_id=settings.USER_ID):
_data = str(c.revision) + str(c.timestamp) + c.data
_data = _data.encode('utf-8')
_data = _data.encode()
c.sig = settings.sk.sign(_data, encoding='base64')
state.db.session.add(c)
state.db.session.commit()

View file

@ -37,7 +37,7 @@ def get(vk):
def put(sk, data):
id = sk.get_verifying_key().to_ascii(encoding='base64').decode()
data = json.dumps(data).encode('utf-8')
data = json.dumps(data).encode()
sig = sk.sign(data, encoding='base64')
url ='%s/%s' % (base, id)
headers = {

View file

@ -8,4 +8,4 @@ def loads(*args, **kargs):
def dumps(*args, **kargs):
#print('dumps', args, kargs)
return json.dumps(*args, **kargs).encode('utf-8')
return json.dumps(*args, **kargs).encode()

View file

@ -57,8 +57,8 @@ class LocalNodesBase(Thread):
'port': server['node_port'],
'cert': server['cert']
})
sig = sk.sign(message.encode('utf-8'), encoding='base64').decode('utf-8')
packet = json.dumps([sig, USER_ID, message]).encode('utf-8')
sig = sk.sign(message.encode(), encoding='base64').decode()
packet = json.dumps([sig, USER_ID, message]).encode()
else:
packet = None
return packet

View file

@ -125,7 +125,7 @@ class Node(Thread):
logger.debug('unable to find host %s', self.user_id)
self.online = False
return None
content = json.dumps([action, args]).encode('utf-8')
content = json.dumps([action, args]).encode()
sig = settings.sk.sign(content, encoding=ENCODING).decode()
headers = {
'User-Agent': settings.USER_AGENT,
@ -185,7 +185,7 @@ class Node(Thread):
def _valid(self, data, sig):
if isinstance(data, str):
data = data.encode('utf-8')
data = data.encode()
try:
self.vk.verify(sig, data, encoding=ENCODING)
#except ed25519.BadSignatureError:

View file

@ -34,7 +34,7 @@ def _to_json(python_object):
def json_dumps(obj):
indent = 2
return json.dumps(obj, indent=indent, default=_to_json, ensure_ascii=False).encode('utf-8')
return json.dumps(obj, indent=indent, default=_to_json, ensure_ascii=False).encode()
def run_async(func):
@wraps(func)

View file

@ -114,9 +114,15 @@ def valid(key, value, sig):
'''
validate that value was signed by key
'''
vk = ed25519.VerifyingKey(str(key), encoding=ENCODING)
if isinstance(sig, str):
sig = sig.encode()
if isinstance(value, str):
value = value.encode()
if isinstance(key, str):
key = key.encode()
vk = ed25519.VerifyingKey(key, encoding=ENCODING)
try:
vk.verify(str(sig), str(value), encoding=ENCODING)
vk.verify(sig, value, encoding=ENCODING)
#except ed25519.BadSignatureError:
except:
return False