def listen_on_port(): # Sends sign_in message to server client_socket.sendto(make_sign_in_message(), (server_ip, server_port)) data, address = client_socket.recvfrom(BUFFER_SIZE) r1_hash = data.split(',') if len(r1_hash) == 2: r1 = r1_hash[0] hash = r1_hash[1] r2 = PoW.compute_r2(r1, hash) client, client_pubkey = Diffie_hellman.client_contribution() global my_username my_username, my_password = enter_username_password() global puzzle_random_number puzzle_random_number = binascii.hexlify(os.urandom(16)) # Generate random number c_key = str(client_pubkey) message_to_send = make_puzzle_message(r2, my_username, my_password, puzzle_random_number, c_key) client_socket.sendto(message_to_send, (server_ip, server_port)) data, address = client_socket.recvfrom(BUFFER_SIZE) response_split = data.split() if len(response_split) > 1 and response_split[0] == INCORRECT_USER_PASS_MESSAGE: handle_incorrect_user_password(response_split) else: split_data = data.split("---", 1) if len(split_data) == 2: data_verified = verify_signature(SERVER_PUBLIC_KEY_FULL_PATH, split_data[1], split_data[0]) # Signature is valid if data_verified: response = split_data[0].split(',') if len(response) == 2: random_answer, server_contribution = response if puzzle_random_number == random_answer: # Set shared key variable set_shared_key(client, server_contribution) # Let user type in commands start_input_thread() if shared_key_set: while True: manage_package_arrival() else: print SHARED_KEY_COULDNT_BE_ESTABLISHED exit(0) else: print DATA_COULDNT_BE_VERIFIED else: print DATA_COULDNT_BE_VERIFIED
def handle_sign_in(address): global pow_length pow_length = incoming_control.get_pow_length(pow_length) r1, r2, hash_value = PoW.proof_o_work(pow_length) # Add puzzle result to dictionary ip_port_association[address] = [ False, r2, get_expiration_of_puzzle() ] # [Logged_in, result_puzzle, expiration_puzzle] server_socket.sendto(str(r1) + ',' + str(hash_value), address)
Tx.gen_random_txblock(q, p, g, TxCnt, "transactions.txt") # Test 1 # Check all your transactions in a block ReturnCode = CheckBlock(q, p, g, TxCnt, "transactions.txt") if ReturnCode == -10000: print("File Problem") elif (ReturnCode < 0): print("Signtature Problem in Tranaction number", -ReturnCode) elif ReturnCode == 0: print("All Transactions Verify") else: print("Unexpected branching") # Test 2 # Check PoW of the sample block proof = PoW.CheckPow(p, q, g, 5, TxCnt, "block_sample.txt") if proof == "" or proof[:5] != "00000": print("PoW is NOT OK:", proof) else: print("PoW is OK:", proof) # Test 3 # This is for generating a PoW for the block in transactions.txt # You should have a function called "PoW" in file PoW.py PoWLen = 5 # The number of 0 hexadecimal digits; i.e. PoWLen block = PoW.PoW(PoWLen, q, p, g, TxCnt, "transactions.txt") f = open("block.txt", "w") f.write(block) f.close() # Check PoW
for i in range(0,blockCount): transaction=TxBlockGen.GenTxBlock(p, q, g, TxCount) TxBlockFileName = FileName+str(i)+".txt" TxBlockFile = open(TxBlockFileName, "w") TxBlockFile.write(transaction) TxBlockFile.close() print "Transaction block %d is written into TransactionBlock%d.txt" %(i,i) # Proof of work generation for given transcation blocks if PoWGenOn: FileName = "TransactionBlock" ChainFileName = "LongestChain.txt" for i in range(0,blockCount): TxBlockFileName = FileName+str(i)+".txt" if os.path.exists(TxBlockFileName) == True: PoW.PoW(TxBlockFileName, ChainFileName, PoWLen, TxLen) print "Proof of work is written/appended to "+ ChainFileName else: print "Error: ", TxBlockFileName, "does not exist" sys.exit() # Validate the block chain if BlockChainTestOn: BlockChainFileName = "LongestChain.txt" if os.path.exists(BlockChainFileName) == True: BlockChainFile = open(BlockChainFileName, "r") blocks = BlockChainFile.readlines() blockCnt = len(blocks)/LinkLen PoW = blocks[LinkLen-1][:-1]
# Test I # Check all your transactions in a block ReturnCode = CheckBlock("transactions.txt", q, p, g) if ReturnCode == -10000: print("File Problem") elif (ReturnCode < 0): print("Signtature Problem in Tranaction number", -ReturnCode) elif ReturnCode == 0: print("All Transactions Verify") else: print("Unexpected branching") # This is for generating a PoW for the block in transactions.txt # You should have a function called "PoW" in file PoW.py PoWLen = 5 # The number of 0 hexadecimal digits; i.e. PoWLen = x/4 block = PoW.PoW(PoWLen, q, p, g, "transactions.txt") f = open("block.txt", "w") f.write(block) f.close() # Test II # Check PoW f = open("block.txt", "r") block = f.readlines() block = "".join(block) blockHash = hashlib.sha3_256(block.encode('UTF-8')).hexdigest() if blockHash[0:PoWLen] == "0" * PoWLen: print("Proof-of-Work test: passed") else: print("Proof-of-Work test: failed") print("hash of the block: ", blockHash)
import os from binascii import * def is_hex(s): for c in s: if not c in "0123456789abcdef": return False return True def collision(h1, h2): return h1.string != h2.string and h1.digest() == h2.digest() if not PoW(5): exit() action_cnt = 2 seed = os.urandom(16) print(intro.format(action_cnt)) for i in range(action_cnt): print(menu) x = input() if not x in ["1", "2", "3"]: print(invalid_input) exit()