66from Crypto .Signature import pkcs1_15
77from binascii import hexlify
88from time import sleep
9+ from threading import Lock
10+
11+ private_keys_mutex = Lock ()
912
1013from socket import socket , AF_INET , SOCK_STREAM
1114
@@ -32,15 +35,17 @@ def client(mode, login, vote):
3235 if login not in list (keys .keys ()):
3336 client_socket = socket (AF_INET , SOCK_STREAM )
3437
35- auth_ip = get_addr ("auth.com" )
36- client_socket .connect ((auth_ip [0 ], int (auth_ip [1 ])))
3738
3839 private_key = RSA .generate (1024 )
3940 public_key = private_key .publickey ()
4041
41- keys [login ] = private_key .export_key ().decode ()
42+ with private_keys_mutex :
43+ keys = readjson ("private_keys" ) or {}
44+ keys [login ] = private_key .export_key ().decode ()
45+ writejson (keys , "private_keys" )
4246
43- writejson (keys , "private_keys" )
47+ auth_ip = get_addr ("auth.com" )
48+ client_socket .connect ((auth_ip [0 ], int (auth_ip [1 ])))
4449
4550 client_socket .send (login .encode ())
4651 ok = client_socket .recv (1024 ).decode ()
@@ -61,7 +66,7 @@ def client(mode, login, vote):
6166 candidates = client_socket .recv (1024 ).decode ().split (',' )
6267 for i in range (len (candidates )):
6368 candidates [i ] = candidates [i ].replace ("[" ,"" ).replace ("]" ,"" ).replace ("'" ,"" ).strip ()
64- print (f"[{ i + 1 } ] { candidates [i ]} " )
69+ if mode == "interative" : print (f"[{ i + 1 } ] { candidates [i ]} " )
6570
6671 client_socket .send ("vote" .encode ())
6772 if mode == "interative" :
@@ -87,10 +92,10 @@ def client(mode, login, vote):
8792 candidates = client_socket .recv (1024 ).decode ().split (',' )
8893 for i in range (len (candidates )):
8994 candidates [i ] = candidates [i ].replace ("[" ,"" ).replace ("]" ,"" ).replace ("'" ,"" ).strip ()
90- print (f"[{ i + 1 } ] { candidates [i ]} " )
95+ if ( mode == "interative" ): print (f"[{ i + 1 } ] { candidates [i ]} " )
9196
9297 client_socket .send ("vote" .encode ())
93- vote = int (input ("Digite o número do candidato: " ))
98+ if ( mode == "interative" ): vote = int (input ("Digite o número do candidato: " ))
9499
95100 candidate_name = candidates [vote - 1 ].encode ()
96101 hash_vote = SHA256 .new (candidate_name )
0 commit comments