locator1 = _pyndn.KeyLocator_to_ndn(name=ndn_name1) print(locator1) locator1_obj = _pyndn.KeyLocator_obj_from_ndn(locator1) print(locator1_obj) print(locator1_obj.keyName) name2 = _pyndn.name_comps_from_ndn(locator1_obj.keyName.ndn_data) print(name2) for comp1, comp2 in zip(name1, name2): if comp1 != comp2: raise AssertionError("Got a different output: '%s' != '%s'" % (comp1, comp2)) key1 = NDN.getDefaultKey() locator2 = _pyndn.KeyLocator_to_ndn(key=key1.ndn_data_public) print(locator2) locator2_obj = _pyndn.KeyLocator_obj_from_ndn(locator2) key2 = locator2_obj.key print(key2) print(key1.ndn_data_public) print(key2.ndn_data_public) assert(key1.publicToDER() == key2.publicToDER()) del key2 key2 = _pyndn.Key_obj_from_ndn(key1.ndn_data_private)
import os, filecmp from base64 import b64encode, b64decode from pyndn import Key, _pyndn, NDN k = NDN.getDefaultKey() private1_der = k.privateToDER() public1_der = k.publicToDER() k2 = Key() k2.fromDER(private=private1_der) private2_der = k2.privateToDER() public2_der = k2.publicToDER() assert(private1_der == private2_der) assert(public1_der == public2_der) assert(k.publicKeyID == k2.publicKeyID) del(k2) k2 = Key() k2.fromDER(public=public1_der) try: private2_der = k2.privateToDER() except: pass else: raise AssertionError("This should fail - this is not a private key")
root = os.path.join("tmp") private_pem1 = os.path.join(root, 'private1.pem') public_pem1 = os.path.join(root, 'public1.pem') private_pem2 = os.path.join(root, 'private2.pem') public_pem2 = os.path.join(root, 'public2.pem') def rm_files(*list): for file in list: if os.path.exists(file): os.remove(file) rm_files(private_pem1, public_pem1, private_pem2, public_pem2) k = NDN.getDefaultKey() k.privateToPEM(filename=private_pem1) k.publicToPEM(filename=public_pem1) k2 = Key() k2.fromPEM(filename=private_pem1) k2.privateToPEM(filename=private_pem2) k2.publicToPEM(filename=public_pem2) assert (filecmp.cmp(private_pem1, private_pem2)) assert (filecmp.cmp(public_pem1, public_pem2)) print(b64encode(k.publicKeyID)) print(b64encode(k2.publicKeyID)) assert (k.publicKeyID == k2.publicKeyID)
locator1 = _pyndn.KeyLocator_to_ndn(name=ndn_name1) print(locator1) locator1_obj = _pyndn.KeyLocator_obj_from_ndn(locator1) print(locator1_obj) print(locator1_obj.keyName) name2 = _pyndn.name_comps_from_ndn(locator1_obj.keyName.ndn_data) print(name2) for comp1, comp2 in zip(name1, name2): if comp1 != comp2: raise AssertionError("Got a different output: '%s' != '%s'" % (comp1, comp2)) key1 = NDN.getDefaultKey() locator2 = _pyndn.KeyLocator_to_ndn(key=key1.ndn_data_public) print(locator2) locator2_obj = _pyndn.KeyLocator_obj_from_ndn(locator2) key2 = locator2_obj.key print(key2) print(key1.ndn_data_public) print(key2.ndn_data_public) assert (key1.publicToDER() == key2.publicToDER()) del key2 key2 = _pyndn.Key_obj_from_ndn(key1.ndn_data_private)