cryptoSL2024/net.py

30 lines
724 B
Python
Raw Normal View History

2024-04-18 00:09:59 +02:00
import socket
import pickle
import struct
import ecpy.curves
from otp import curve
def send_point(s: socket.socket, point: ecpy.curves.Point):
send(s, (point.x, point.y))
def recv_point(s: socket.socket) -> ecpy.curves.Point:
point = recv(s)
return ecpy.curves.Point(point[0], point[1], curve)
def send(s: socket.socket, data):
data = pickle.dumps(data, protocol=4)
data = struct.pack('<I', len(data)) + data
s.sendall(data)
def recv(s: socket.socket):
length = s.recv(4)
length, *_ = struct.unpack('<I', length)
pickled = b""
toread = length
while toread:
read = s.recv(toread)
pickled += read
toread -= len(pickled)
return pickle.loads(pickled)