def testSignAndVerify(self): msg = b'1231232131' keyPair = CryptoUtil.genKeyPair() privKey = CryptoUtil.getPrivateKey(keyPair) pubKey = CryptoUtil.getPublicKey(keyPair) sig = CryptoUtil.signMessage(privKey, msg) self.assertTrue(CryptoUtil.verifySignature(pubKey, msg, sig), 'correct signature not verified')
def testBadSignature(self): msg = b'1231232131' keyPair = CryptoUtil.genKeyPair() privKey = CryptoUtil.getPrivateKey(keyPair) sig = CryptoUtil.signMessage(privKey, msg) keyPair2 = CryptoUtil.genKeyPair() pubKey2 = CryptoUtil.getPublicKey(keyPair2) self.assertFalse(CryptoUtil.verifySignature(pubKey2, msg, sig), 'incorrect signature was verified as correct')
exportedKey = b'-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQCyvh9t+A9qWzUZL2fPuh' + \ b'//LhAdnuRtnaG8slkrXo3AaTufedCH\nO+1kbz8csCeo0iQl0y4mQVW3Z3I3VWl/CGRFE9eSNw3gh' + \ b'doIYRB6dfo46kDY7GVf\ncYnSR7fR/LEBcKQ48HpxX8S8dMy/zkbfIFEB9tVsZz9wOvleJmib2igEY' + \ b'wIDAQAB\nAoGAUohRUODKlWx57yAhihxCK/zj+hllFmxDwzKC0/C7+U1d93wy2k7EhayMcr2w\nAlb' + \ b'N1B1X4NgoMSB177DjnoGMckX1r8A4jrvSDDqymyqQxsBHvYrMuzdLmNql8uHW\nSPp1HnKYyaOJGEW' + \ b'aevQmv2Hb0uu53gxWEvAEQUYJt0QFNKECQQC9KQfwM9FamgQR\niPAQLOCEAGIEr9a/zHjR9IkVfzT' + \ b'88zyMUNnnn8xBM96kDfpVKR5ge/Ahja179i9G\nG1jCFNGRAkEA8ea9mCOGIo4osriiM6/pRZn+zmD' + \ b'cQDhV9s0CTMtlJvxykVO18EQs\nWlHJpmWQqAmn4d8TM1xxJrf7acOcdwO8swJATsgq/TinpeNldGQ' + \ b'jD6mRLIj4Sdlu\nSF2BqHf/LAvZ5svrWMlHp/de230d6hfEPfmtJCQaQ+885NcIo1s6YULIcQJBAMd' + \ b't\noXUiJF2ssaTWTvMp3blCAi4G8M4JI+X6kiDZtqTzj0h8qQxSR/aWYxbJuP6wJzYy\nANRhK+/t0' + \ b'loZqI7+B70CQH2LLsccb1MHAkAGRqDZjtAfFAt7p7IaNeEp0jo4TcvK\ny50t2C9i5aa5tY6TFp4pQ' + \ b'3Vfk6l+21J6e5BKari9wfY=\n-----END RSA PRIVATE KEY-----' scroogePrivK = Crypto.PublicKey.RSA.importKey(exportedKey) scroogePubK = CryptoUtil.getPublicKey(scroogePrivK) keyPair = CryptoUtil.genKeyPair() alicePrivK = CryptoUtil.getPrivateKey(keyPair) alicePubK = CryptoUtil.getPublicKey(keyPair) # Make a genesis transaction genesis = Transaction() genesisValue = 25.0 genesis.addOutput(genesisValue, scroogePubK) genesis.finalize() # Make an initial pool initialPool = UTXOPool() utxo = UTXO(genesis.getHash(), 0) initialPool.addUTXO(utxo, genesis.getOutput(0)) # Note this transaction is not valid -- one of its inputs does not exist tx2in2out = Transaction()
import unittest import CryptoUtil from Block import Block from Block import COINBASE from BlockChain import CUT_OFF_AGE from BlockChain import BlockChain from Transaction import Transaction from BlockHandler import BlockHandler scroogeKeyPair = CryptoUtil.genKeyPair() scroogePriKey = CryptoUtil.getPrivateKey(scroogeKeyPair) scroogePubKey = CryptoUtil.getPublicKey(scroogeKeyPair) def signInput(tx, priKey, index): tx.addSignature(CryptoUtil.signMessage(priKey, tx.getRawDataToSign(index)), index) class BlockChainTest(unittest.TestCase): def testMethods(self): genesis = Block(b'', scroogePubKey) genesis.finalize() blockChain = BlockChain(genesis) blockHandler = BlockHandler(blockChain) # Genesis block test self.assertEqual(genesis.getHash(), blockChain.getMaxHeightBlock().getHash(), \ 'genesis should be max height block') self.assertEqual(blockChain.getMaxHeightBlock(), genesis, \ 'genesis should be max height block')