-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
58 lines (49 loc) · 2.01 KB
/
util.py
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
56
57
58
import json
import settings
def create_algorithm_header_client(supported_algorithms, public_key_len, name):
header = {
'supported_algorithms': supported_algorithms,
'public_key_len': public_key_len,
'name': name
}
data = json.dumps(header)
data = data.encode()
if len(data) > settings.HANDHSAKE_HEADER_LEN:
raise Exception("header is too long!")
# add padding, so the message is the correct length
return data.ljust(settings.HANDHSAKE_HEADER_LEN)
def create_algorithm_header_server(picked_algorithm, public_key_len, name):
header = {
'algorithm': picked_algorithm,
'public_key_len': public_key_len,
'name': name
}
data = json.dumps(header)
data = data.encode()
if len(data) > settings.HANDHSAKE_HEADER_LEN:
raise Exception("header is too long!")
# add padding, so the message is the correct length
return data.ljust(settings.HANDHSAKE_HEADER_LEN)
# pick the strongest algorithm from the clients configuration
def pick_algorithm(strong_algorithms, client_config):
client_algorithms = client_config['supported_algorithms']
for alg in strong_algorithms:
if alg in client_algorithms:
return alg
return None
# print a message from a peer and keep the current cursor position
# in the console.
# credit: https://stackoverflow.com/questions/75507228/python-print-message-while-asking-user-for-input
def print_message(msg):
lines = msg.split('\n')
for line in lines:
print(
"\u001B[s" # Save current cursor position
"\u001B[A" # Move cursor up one line
"\u001B[999D" # Move cursor to beginning of line
"\u001B[S" # Scroll up/pan window down 1 line
"\u001B[L", # Insert new line
end="")
print(line, end="")
print("\u001B[u", end="") # Move back to the former cursor position
print("", end="", flush=True) # Flush message