-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
59 lines (42 loc) · 1.28 KB
/
protocol.py
File metadata and controls
59 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# protocol.py
# Code for the NUSP protocol
# This code is released to the public domain.
# "Share and enjoy....." :)
import json
# Protocol version and packet types
VERSION = 1
TYPE_HANDSHAKE_INIT = 0x01
TYPE_HANDSHAKE_RESP = 0x02
TYPE_HANDSHAKE_COMP = 0x03
TYPE_DATA = 0x10
def create_handshake_init_packet(client_public_key: bytes):
return {
"version": VERSION,
"type": TYPE_HANDSHAKE_INIT,
"client_pub": client_public_key.decode('latin1')
}
def create_handshake_resp_packet(server_public_key: bytes, server_scid: str):
return {
"version": VERSION,
"type": TYPE_HANDSHAKE_RESP,
"server_pub": server_public_key.decode('latin1'),
"server_scid": server_scid
}
def create_handshake_comp_packet():
return {
"version": VERSION,
"type": TYPE_HANDSHAKE_COMP
}
def create_data_packet(ciphertext: bytes, nonce: bytes):
return {
"version": VERSION,
"type": TYPE_DATA,
"nonce": nonce.decode('latin1'),
"ciphertext": ciphertext.decode('latin1')
}
def parse_packet(data: bytes):
# Expects a JSON-encoded packet
packet = json.loads(data.decode('utf-8'))
return packet
def packet_to_bytes(packet: dict):
return json.dumps(packet).encode('utf-8')