dist-iot-net/src/cipher.py
hornet a85b08f050
Feature/py iot (#4)
* Changing files but having issues with space tabs.

* Removed config. Fixed indents.

* Removed config.

* Fixed test config. Fixed syntax in cli.

* Editing cli and hub to finally commit transactions.

* Added better logging logic.
Will clean up in the morning.

* Hub is more functional.

* Edited IoT to gen keys.

* Public keys are now generated, sent, and stored.
However, they are not transmitted when a new client joins.

* Added crypto testing

* Communication is now functional.
2021-11-28 19:00:46 +00:00

56 lines
1.1 KiB
Python

import time
import json
from random import random
class Client:
def __init__(self, ws):
self.new = True
self.ws = ws
self.n = None
self.e = None
def log_prefix(t, client=None):
if client is None:
return str(time.time()) + ' [' + t + ':srv] '
return str(time.time()) + ' [' + t + ':' + str(client['id']) + '] '
def load_msg(message):
try:
msg = json.loads(message)
except:
msg = message
return msg
def load_pub_key(cli, msg):
try:
cli.n = msg['pub_key']['n']
cli.e = msg['pub_key']['e']
return True
except:
print('\tMessage had no attribute pub_key. Failed.')
return False
def gen_comm_msg(to, ident, msg):
message = {
'type':'message',
'to': to,
'id': ident,
'contents': msg
}
return json.dumps(message)
def gen_pub_key_msg(n, e):
message = {
'type': 'pub_key',
'pub_key':{
'n':n,
'e':e
}
}
return json.dumps(message)