def load_keyfile(fn): with open(fn, 'r') as f: key = eval(f.read()) if key['type'] == 'ed25519': fid = bytes.fromhex(key['public']) signer = crypto.ED25519(bytes.fromhex(key['private'])) elif key['type'] == 'hmac_sha256': fid = bytes.fromhex(key['feed_id']) signer = crypto.HMAC256(bytes.fromhex(key['private'])) return fid, signer
def load_keyfile(fn): with open(fn, 'r') as f: key = eval(f.read()) if key['type'] == 'ed25519': fid = bytes.fromhex(key['public']) signer = crypto.ED25519(bytes.fromhex(key['private'])) digestmod = 'sha256' elif key['type'] in ['hmac_sha256', 'hmac_sha1', 'hmac_md5']: fid = bytes.fromhex(key['feed_id']) digestmod = key['type'][5:] signer = crypto.HMAC(digestmod, bytes.fromhex(key['private'])) return fid, signer, digestmod
def setup_feed_crypto(self, alias, dir=CONFIG_DIR): fid_path = dir + alias + ".fid" sk_path = dir + alias + ".sk" fid_exists = os.path.isfile(fid_path) sk_exists = os.path.isfile(sk_path) if fid_exists and sk_exists: f = open(fid_path, "r") self.fid[alias] = f.readline() f.close() f = open(sk_path, "r") self.sk[alias] = f.readline() f.close() else: key_pair = crypto.ED25519() key_pair.create() self.fid[alias] = key_pair.get_public_key().hex() self.sk[alias] = key_pair.get_private_key().hex() f = open(fid_path, "w") f.write(self.fid[alias]) f.close() f = open(sk_path, "w") f.write(self.sk[alias]) f.close()
def setup_feed_crypto(self): fid_path = self.alias + ".fid" sk_path = self.alias + ".sk" fid_exists = os.path.isfile(fid_path) sk_exists = os.path.isfile(sk_path) if fid_exists and sk_exists: f = open(fid_path, "r") self.fid = f.readline() f.close() f = open(sk_path, "r") self.sk = f.readline() f.close() else: key_pair = crypto.ED25519() key_pair.create() self.fid = key_pair.get_public_key().hex() self.sk = key_pair.get_private_key().hex() f = open(fid_path, "w") f.write(self.fid) f.close() f = open(sk_path, "w") f.write(self.sk) f.close()
def get_signer(self, alias): return crypto.ED25519(bytes.fromhex(self.sk[alias]))
import binascii import json import logging import os import lib.gg as event import lib.pcap as log import lib.crypto as crypto # ---------------------------------------------------------------------- if __name__ == '__main__': # create feed ID keypair = crypto.ED25519() keypair.create() # create and sign two events e1 = event.EVENT(prev=None, feed=keypair.public, seq=1, time=1564444800, content=b'{\n "name": "foo.box"\n}\n', content_enc=event.GG_CONTENT_ENCODING_JSON) e1.signature = keypair.sign(e1.event_to_cbor()) e2 = event.EVENT(prev=e1.get_sha256(), feed=keypair.public, seq=2, time=1564444801,
def create_feed(): ''' This method is used to create the feeds out of the ISP-Client contract. :return: ''' global client_log global client_key global next_request_ID global client_config # if the feeds exist if os.path.exists( f'{client_config["location"]}/{client_config["alias"]}.pcap' ) and os.path.exists( f'{client_config["location"]}/{client_config["key"]}'): logging.info(f'Feed and key exist') client_key = f'{client_config["location"]}/{client_config["key"]}' client_log = f'{client_config["location"]}/{client_config["alias"]}.pcap' # if only the key file exists elif not os.path.exists( f'{client_config["location"]}/{client_config["alias"]}.pcap' ) and os.path.exists( f'{client_config["location"]}/{client_config["key"]}'): logging.debug("key exists feed not") fid, signer = feed.load_keyfile( f'{client_config["location"]}/{client_config["key"]}') client_feed = feed.FEED( f'{client_config["location"]}/{client_config["alias"]}.pcap', fid, signer, True) client_log = f'{client_config["location"]}/{client_config["alias"]}.pcap' client_key = f'{client_config["location"]}/{client_config["key"]}' pk = feed.get_public_key(client_key) logging.debug(pk) feed_entry = { 'type': 'initiation', 'alias': client_config['alias'], 'key': pk, 'location': client_config['location'] } logging.info(f'writing in {client_log}: {feed_entry}') client_feed.write(feed_entry) # if nothing exists else: key_pair = crypto.ED25519() key_pair.create() header = ( "# new ED25519 key pair: ALWAYS keep the private key as a secret\n" ) keys = ('{\n ' + (',\n '.join(key_pair.as_string().split(','))[1:-1]) + '\n}') logging.info( "# new ED25519 key pair: ALWAYS keep the private key as a secret") logging.info('{\n ' + (',\n '.join(key_pair.as_string().split(','))[1:-1]) + '\n}') if not os.path.exists(f'{client_config["location"]}'): os.mkdir(f'{client_config["location"]}') f = open(f'{client_config["location"]}/{client_config["key"]}', 'w') f.write(header) f.write(keys) f.close() try: os.remove( f'{client_config["location"]}/{client_config["alias"]}.pcap') except: pass fid, signer = feed.load_keyfile( f'{client_config["location"]}/{client_config["key"]}') client_feed = feed.FEED( f'{client_config["location"]}/{client_config["alias"]}.pcap', fid, signer, True) client_log = f'{client_config["location"]}/{client_config["alias"]}.pcap' client_key = f'{client_config["location"]}/{client_config["key"]}' pk = feed.get_public_key( f'{client_config["location"]}/{client_config["key"]}') feed_entry = { 'type': 'initiation', 'alias': client_config['alias'], 'key': pk, 'location': client_config['location'] } logging.info(f'writing in {client_log}: {feed_entry}') client_feed.write(feed_entry)
def create_E2E_feed(spk): ''' After an approved introduce-request creates the feed pair for the newly established contract :param spk: server public key ''' global c_server_dict cpk = client_config["name"] c_location = client_config["location"] i_location = client_config["isp_location"] # key file generation key_pair = crypto.ED25519() key_pair.create() header = ( "# new ED25519 key pair: ALWAYS keep the private key as a secret\n") keys = ('{\n ' + (',\n '.join(key_pair.as_string().split(','))[1:-1]) + '\n}') logging.info( "# new ED25519 key pair: ALWAYS keep the private key as a secret") logging.info('{\n ' + (',\n '.join(key_pair.as_string().split(','))[1:-1]) + '\n}') # directory generation if not os.path.exists(f'{c_location}'): os.mkdir(f'{c_location}') f = open(f'{c_location}/{cpk}_{spk}.key', 'w') f.write(header) f.write(keys) f.close() # feed generation try: os.remove(f'{c_location}/{cpk}_{spk}.pcap') except: pass fid, signer = feed.load_keyfile(f'{c_location}/{cpk}_{spk}.key') E2E_feed = feed.FEED(f'{c_location}/{cpk}_{spk}.pcap', fid, signer, True) alias_c_s = f'{c_location}/{cpk}_{spk}.pcap' alias_s_c = f'{c_location}/{spk}_{cpk}.pcap' key_c_s = f'{c_location}/{cpk}_{spk}.key' # saving contract rep = replicator.Replicator(f'{cpk}_{spk}.pcap', alias_c_s, i_location) cserver = cServer(spk, alias_s_c, alias_c_s, key_c_s, next_request_ID - 1, [], rep) c_server_dict[spk] = cserver logging.debug(c_server_dict[spk].to_string()) # write contract in first log entry feed_entry = { 'type': 'init', 'key': feed.get_public_key(key_c_s), 'cserver': cserver.asdict() } logging.info(f'writing in {spk}: {feed_entry}') E2E_feed.write(feed_entry) cserver.replicator.replicate() return eval(keys)["public"]
def get_signer(self): return crypto.ED25519(bytes.fromhex(self.sk))