def test_SetSplitRestoreKey(): for x in range(10): maxshares = 100 thresholdList = [] # Randomly generate two digit number that ranges from 3 to 100 threshold = randint(3, 100) # Generate pair of keys in pem format pub_priKey = Nakasendo.ECKey256K1() pub_priKey2 = Nakasendo.ECKey256K1() # Retrieve the private from the PEM string privKey = pub_priKey.priKey # Sets a key from a PEM format pub_priKey2.FromPEMStr(privKey) assert str(pub_priKey) == str(pub_priKey2), "Test failed" # Split a private key into a given number of shares splitKeyList = pub_priKey2.SplitKey(threshold, maxshares) assert len(splitKeyList) == maxshares, "Test failed" for i in range(threshold): thresholdList.append(splitKeyList[i]) assert len(thresholdList) == threshold, "Test failed" # Restore a private key from a given number of shares pub_priKey2.RecoverKey(thresholdList) assert pub_priKey2.priKey == privKey, "Test failed"
def test_SetKeyFromPem(): for x in range(10): # Generate pair of keys in pem format pub_priKey = Nakasendo.ECKey256K1() pub_priKey2 = Nakasendo.ECKey256K1() # Retrieve the private from the PEM string privKey = pub_priKey.priKey # Sets a key from a PEM format pub_priKey2.FromPEMStr(privKey) # Calculated private key should match the generated one assert str(pub_priKey) == str(pub_priKey2), "Test failed"
def test_ShareSecret(): for x in range(100): # Generate keys for alice and bob alice_pub_privKeyPEM = Nakasendo.ECKey256K1() bob_pub_privKeyPEM = Nakasendo.ECKey256K1() # alice_pubKeyPEM, alice_privKeyPEM = PyAsymKey.GenerateKeyPairPEM() # bob_pubKeyPEM, bob_privKeyPEM = PyAsymKey.GenerateKeyPairPEM() # Calculate shared secret from my private key and their public key secret_share_from_alice = alice_pub_privKeyPEM.CalculateSharedSecret( bob_pub_privKeyPEM.pubKey) secret_share_from_bob = bob_pub_privKeyPEM.CalculateSharedSecret( alice_pub_privKeyPEM.pubKey) assert secret_share_from_alice == secret_share_from_bob, "Test failed"
def test_Sign_Verification(): msg = 'Hello, I am a message, sign me' for x in range(100): # Generate pair of keys in PEM format pub_privKey = Nakasendo.ECKey256K1() pub_key = pub_privKey.pubKey # Sign message with private Key rSig, sSig = pub_privKey.sign(msg) # Verify message's signature with public key verify_ok = Nakasendo.verify(msg, pub_key, rSig, sSig) assert verify_ok, "Test failed"
def test_KeyDerive(): additive_msg = 'I am a random message used for key derivation' for x in range(100): # Generate keys for alice and bob alice_pub_privKeyPEM = Nakasendo.ECKey256K1() bob_pub_privKeyPEM = Nakasendo.ECKey256K1() # Derive public key from a given public key and a additive message alice_derived_pub = alice_pub_privKeyPEM.derivePublicKey(additive_msg) bob_derived_pub = bob_pub_privKeyPEM.derivePublicKey(additive_msg) # Derive pirvate key from a given private key and a additive message alice_derived_private = alice_pub_privKeyPEM.derivePrivateKey( additive_msg) bob_derived_private = bob_pub_privKeyPEM.derivePrivateKey(additive_msg) # Get public key PEM given the private key PEM alice_pub_priKey2 = Nakasendo.ECKey256K1() bob_pub_priKey2 = Nakasendo.ECKey256K1() alice_pub_priKey2.FromPEMStr(alice_derived_private) bob_pub_priKey2.FromPEMStr(bob_derived_private) assert alice_pub_priKey2.pubKey == alice_derived_pub, "Test failed" assert bob_pub_priKey2.pubKey == bob_derived_pub, "Test failed"
print('Found SDKLIBRARIES_ROOT="{}"'.format(str(sdk_libraries_root))) print('Modules directory ="{}"'.format(sdk_libraries_lib_dir_str)) try: import Nakasendo except ImportError as e: print('Error while loading SDKLibraries python modules {}'.format( e.message)) print( 'Try to define environment variable SDKLIBRARIES_ROOT pointing to the location of installed SDKLibraries or add this to PYTHONPATH' ) raise ImportError('Unable to load SDKLibraries python modules') if __name__ == "__main__": mykey = Nakasendo.ECKey256K1() print(mykey) shares = mykey.SplitKey(5, 15) print(shares) #At some point later.... print("a random collection of shares") secure_random = random.SystemRandom() subset = [] sharesCopy = shares random.shuffle(sharesCopy) for i in range(5):
try: import Nakasendo except ImportError as e: print('Error while loading SDKLibraries python modules {}'.format( e.message)) print( 'Try to define environment variable SDKLIBRARIES_ROOT pointing to the location of installed SDKLibraries or add this to PYTHONPATH' ) raise ImportError('Unable to load SDKLibraries python modules') if __name__ == "__main__": print("starting....") AliceKey = Nakasendo.ECKey256K1() BobsKey = Nakasendo.ECKey256K1() msg = 'This is a message that will be hased' #Alice and Bob preformn the same steps #ALice newAliceKeyPemFormat = AliceKey.derivePrivateKey(msg) AliceNewKey = Nakasendo.ECKey256K1() AliceNewKey.FromPEMStr(newAliceKeyPemFormat) pubKeyAlice = AliceNewKey.pubKey print(pubKeyAlice)
print(decoded) msgToEncode = 'Research team' for x in range(1, 10): myMsgHash = Nakasendo.MessageHash(msgToEncode) encoded = myMsgHash.Base64Encode() decoded = myMsgHash.Bas64Decode(encoded) print(decoded) msgToEncode = 'the quick brown fox jumped over the lazy dog' encodedAsHex = encoder.Encode(msgToEncode) print("EncodedMsgAsHex: %s" % encodedAsHex) decoded = encoder.Decode(encodedAsHex) print("Decoded Message %s" % decoded) mykey = Nakasendo.ECKey256K1() print(mykey) print("derive a public key..") myderivedKey = mykey.derivePublicKey("Using this message") print(myderivedKey) shares = mykey.SplitKey(3, 6) print('And now recover') recoveredKey = Nakasendo.ECKey256K1() recoveredKey.RecoverKey(shares) print(recoveredKey) #Sign a msessage msg = 'The quick brown fox jumped over the lazy dog' sig = mykey.sign(msg) print(sig)