def runTest(self): key = RSA.importKey(PKCS1_15_NoParams.rsakey) hashed = SHA1.new(b("Test")) good_signature = PKCS1_v1_5.new(key).sign(hashed) verifier = PKCS1_v1_5.new(key.publickey()) self.assertEqual(verifier.verify(hashed, good_signature), True) # Flip a few bits in the signature bad_signature = strxor(good_signature, bchr(1) * len(good_signature)) self.assertEqual(verifier.verify(hashed, bad_signature), False)
def validate_transaction_data_consensus(transaction, blockchain): """ validates the transaction's data :param transaction: transaction to validate :type transaction: Transaction :param blockchain: blockchain to use to check transaction's validity :type blockchain: Blockchain :return: True if transaction is valid, False if not :rtype: tuple """ # validate input sources and signatures input_amount = 0 output_amount = 0 for input1 in transaction.inputs: block = blockchain.get_block_consensus_chain(input1[1]) input_transaction = block.transactions[input1[2] - 1] appears = False for source_output in input_transaction: if source_output[0] == input1[0]: appears = True input_amount += source_output[1] if not appears: return False, "transaction input's source does not appear in source block" hasher = SHA256.new(transaction.signing_format().encode("utf-8")) verifier = PKCS1_v1_5.new(RSA.import_key(input1[0])) if not verifier.verify(hasher, input1[3]): return False, "signature is not valid" # input sources and signatures are valid, check that input amount equals output amount for output in transaction.outputs: output_amount += output[1] if not output_amount - input_amount: return False, "input and output amounts are not equal" # input amount equals output amounts, validate that no transactions are a double spend for input1 in transaction.inputs: for x in range(input1[1] + 1, len(blockchain) + 1): block = blockchain.get_block_consensus_chain(x) for block_transaction in block.transactions: for input2 in block_transaction.inputs: if input2[0] == input1[0] and input2[1] == input1[ 1] and input2[2] == input1[2]: return False, "double spend" return True, ""
def roundup(x): return int(math.ceil(x / 10.0)) * 10 cid = sys.argv[1] SERVER = sys.argv[2] PORT = sys.argv[3] BYZANTINE = sys.argv[4] key = False with open("keys.pem", "r") as myfile: key = RSA.importKey(myfile.read()) public_key = key.publickey() verifier = PKCS1_v1_5.new(public_key) client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) intro = "REGISTER_REQUEST " + str(cid) intro = intro.encode() client.sendto(intro, (SERVER, int(PORT))) response, address = client.recvfrom(1024) neighbors = pickle.loads(response) print(neighbors) messages = [] signs = []
outputs = [] BYZANTINE = 'y' client_connections = [] match_message = "" messages = [] running = 1 key = False with open("keys.pem", "r") as myfile: key = RSA.importKey(myfile.read()) signer = PKCS1_v1_5.new(key) flag = True while running: inputready, outputready, exceptready = select.select( inputs, outputs, inputs, 5000) for s in inputready: if s == server: data, address = server.recvfrom(1024) if data: try: data.decode() except: flag = False