def register(message, signature): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = ('localhost', 10000) sock.connect(server_address) try: type = b'reg' pysocket.send_msg(sock, type) pysocket.send_msg(sock, message) pysocket.send_msg(sock, signature) succes = pysocket.recv_msg(sock) if succes == b'#succesRG': certificate = pysocket.recv_msg(sock) if certificate != b'#invalid': signature = pysocket.recv_msg(sock) sock.close() return certificate, signature else: sock.close() return b'invalid', b'invalid' else: sock.close() return b'#error', b'#error' finally: sock.close()
def request_currency(message, signature): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = ('localhost', 10000) sock.connect(server_address) try: type = b'req_currency' pysocket.send_msg(sock, type) pysocket.send_msg(sock, message) pysocket.send_msg(sock, signature) succes = pysocket.recv_msg(sock) if succes == b'#succesRC': print("succes RC") return pysocket.recv_msg(sock) else: sock.close() print(succes) finally: sock.close()
def comm(message, signature): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = ('localhost', 10002) sock.connect(server_address) try: type = b'com' pysocket.send_msg(sock, type) pysocket.send_msg(sock, message) pysocket.send_msg(sock, signature) succes = pysocket.recv_msg(sock) if succes == b'valid comm': return True, sock else: sock.close() return False finally: print("succes new comm")
def register_seller(message, signature): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = ('localhost', 10000) sock.connect(server_address) try: type = b'reg_seller' pysocket.send_msg(sock, type) pysocket.send_msg(sock, message) pysocket.send_msg(sock, signature) succes = pysocket.recv_msg(sock) if succes == b'#succesRGS': print("succes RGS") else: sock.close() print(succes) finally: sock.close()
def request_payment(user, message, signature): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = ('localhost', 10000) sock.connect(server_address) try: type = b'request_payment' pysocket.send_msg(sock, type) pysocket.send_msg(sock, user.encode()) pysocket.send_msg(sock, message) pysocket.send_msg(sock, signature) succes = pysocket.recv_msg(sock) if succes == b'#succesTM': print("succes TM") else: sock.close() print(succes) finally: sock.close()
def upload_hash(user, message, signature): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = ('localhost', 10000) sock.connect(server_address) try: type = b'upload_hash' pysocket.send_msg(sock, type) pysocket.send_msg(sock, user) pysocket.send_msg(sock, message) pysocket.send_msg(sock, signature) succes = pysocket.recv_msg(sock) if succes == b'#succesUH': print("succes UH") else: sock.close() print(succes) finally: sock.close()
def payment(sock, c1, c3, c5): i1 = c1[1] i3 = c3[1] i5 = c5[1] try: while True: if len(c1[0]) - i1 - 1 == 0 and len(c3[0]) - i3 - 1 == 0 and len( c5[0]) - i5 - 1 == 0: print("No more funds in the account. Closing operation") return False, i1, i3, i5 value = int(input("Value centi = ")) while value - 1 > (len(c1[0]) - i1 - 1) + ( len(c3[0]) - i3 - 1) * 3 + (len(c5[0]) - i5 - 1) * 5: print("Value too big") value = int(input("Value centi = ")) if value <= 0: print("Closing operation") return True, i1, i3, i5 type = b'pay' pysocket.send_msg(sock, type) payment_representation = get_payment_representation( value, len(c1[0]) - i1 - 1, len(c3[0]) - i3 - 1, len(c5[0]) - i5 - 1) print([ len(c1[0]) - i1 - 1, len(c3[0]) - i3 - 1, len(c5[0]) - i5 - 1 ]) print(payment_representation) if payment_representation[0] != 0 or payment_representation[ 1] != 0 or payment_representation[2] != 0: payment_representation_bytes = [ [ c1[0][payment_representation[0] + i1], payment_representation[0] ], [ c3[0][payment_representation[1] + i3], payment_representation[1] ], [ c5[0][payment_representation[2] + i5], payment_representation[2] ] ] else: print("No more funds in the account. Closing operation") return False, i1, i3, i5 payment_representation_bytes = pickle.dumps( payment_representation_bytes, -1) pysocket.send_msg(sock, payment_representation_bytes) succes = pysocket.recv_msg(sock) if succes == b'valid payment': i1 += payment_representation[0] i3 += payment_representation[1] i5 += payment_representation[2] print([ len(c1[0]) - i1 - 1, len(c3[0]) - i3 - 1, len(c5[0]) - i5 - 1 ]) print("Valid payment") else: print("Invalid payment") except: print("error")
connection, client_address = sock.accept() try: print(sys.stderr, 'connection from', client_address) c01 = 0 c03 = 0 c05 = 0 i1 = 0 i3 = 0 i5 = 0 cCU = 0 sCU = 0 cCOMM = 0 sCOMM = 0 # Receive the data in small chunks and retransmit it while True: type = pysocket.recv_msg(connection) if type == b'com': #angajamentul primit de la user message = pysocket.recv_msg(connection) semnatura = pysocket.recv_msg(connection) data1 = message data = pickle.loads(data1) c01 = data["c0"][0] c03 = data["c0"][1] c05 = data["c0"][2] cCU = data["cCU"] sCU = data["sCU"] cCOMM = message sCOMM = semnatura if not sign.auth_sign(message, keyserver_methods.req(
print (sys.stderr, 'starting up on %s port %s' % server_address) sock.bind(server_address) #structura cu dataele personale mydata={"B":"banca"} mydata["Kb"]=public_key.exportKey("PEM") # Listen for incoming connections sock.listen(1) while True: # Wait for a connection print (sys.stderr, 'waiting for a connection') connection, client_address = sock.accept() try: print ( sys.stderr, 'connection from', client_address) # Receive the data in small chunks and retransmit it type=pysocket.recv_msg(connection) if type==b'reg': #datele primite de la user message=pysocket.recv_msg(connection) message=crypt.decrypt(message,private_key.exportKey("PEM")) data1=message data=pickle.loads(data1) #completam structura cu datele necesare pentru user mydata["U"]=data["U"] database[data["U"]]={"currency":data["currency"],"pub_key":data["key"]} mydata["Uip"]=client_address difference2 = datetime.timedelta(weeks=5) mydata["exp"]=now+difference2 mydata["info"]={'serial':hash_functions.random_with_N_digits(10),'cred_limit':30000} data_string = pickle.dumps(mydata, -1)