lokinet/contrib/py/pylokinet/pylokinet/rc.py

31 lines
709 B
Python
Raw Normal View History

2019-01-17 17:59:25 +01:00
from pylokinet import bencode
2019-01-17 17:21:35 +01:00
import pysodium
2019-01-17 18:30:46 +01:00
import binascii
import time
def _expired(ts, lifetime=84600000):
"""
return True if a timestamp is considered expired
lifetime is default 23.5 hours
"""
return (int(time.time()) * 1000) - ts >= lifetime
2019-01-17 17:21:35 +01:00
def validate(data):
rc = bencode.bdecode(data)
2019-01-17 18:30:46 +01:00
if b'z' not in rc or b'k' not in rc:
2019-01-17 17:21:35 +01:00
return False
2019-01-17 18:30:46 +01:00
sig = rc[b'z']
rc[b'z'] = b'\x00' * 64
2019-01-17 17:21:35 +01:00
buf = bencode.bencode(rc)
try:
2019-01-17 18:30:46 +01:00
k = rc[b'k']
pysodium.crypto_sign_verify_detached(sig, buf, k)
2019-01-17 17:21:35 +01:00
except:
return False
else:
return not _expired(rc[b't'])
2019-01-17 17:21:35 +01:00
def get_pubkey(data):
rc = bencode.bdecode(data)
2019-01-17 18:30:46 +01:00
if b'k' in rc:
return binascii.hexlify(rc[b'k']).decode('ascii')