diff --git a/delivery1/client/subject.py b/delivery1/client/subject.py deleted file mode 100644 index 8d34ffc..0000000 --- a/delivery1/client/subject.py +++ /dev/null @@ -1,115 +0,0 @@ -import os -import sys -import argparse -import logging -import json -import requests -from bin import * - -# Identity attributes -# {'username' : '', 'full_name' : '', 'email' : '', public_key : '' } - -logging.basicConfig(format='%(levelname)s\t- %(message)s') -logger = logging.getLogger() -logger.setLevel(logging.INFO) - -def load_state(): - state = {} - state_dir = os.path.join(os.path.expanduser('~'), '.sio') - state_file = os.path.join(state_dir, 'state.json') - - logger.debug('State folder: ' + state_dir) - logger.debug('State file: ' + state_file) - - if os.path.exists(state_file): - logger.debug('Loading state') - with open(state_file,'r') as f: - state = json.loads(f.read()) - - if state is None: - state = {} - - return state - -def parse_env(state): - if 'REP_ADDRESS' in os.environ: - state['REP_ADDRESS'] = os.getenv('REP_ADDRESS') - logger.debug('Setting REP_ADDRESS from Environment to: ' + state['REP_ADDRESS']) - - if 'REP_PUB_KEY' in os.environ: - rep_pub_key = os.getenv('REP_PUB_KEY') - logger.debug('Loading REP_PUB_KEY fron: ' + state['REP_PUB_KEY']) - if os.path.exists(rep_pub_key): - with open(rep_pub_key, 'r') as f: - state['REP_PUB_KEY'] = f.read() - logger.debug('Loaded REP_PUB_KEY from Environment') - return state - -def parse_args(state): - parser = argparse.ArgumentParser() - - parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") - parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") - parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") - parser.add_argument("-c", "--command", help="Command to execute") - parser.add_argument('arg0', nargs='?', default=None) - parser.add_argument('arg1', nargs='?', default=None) - parser.add_argument('arg2', nargs='?', default=None) - parser.add_argument('arg3', nargs='?', default=None) - parser.add_argument('arg4', nargs='?', default=None) - parser.add_argument('arg5', nargs='?', default=None) - - args = parser.parse_args() - if args.verbose: - logger.setLevel(logging.DEBUG) - logger.info('Setting log level to DEBUG') - - if args.key: - if not os.path.exists(args.key[0]) or not os.path.isfile(args.key[0]): - logger.error(f'Key file not found or invalid: {args.key[0]}') - sys.exit(-1) - - with open(args.key[0], 'r') as f: - state['REP_PUB_KEY'] = f.read() - logger.info('Overriding REP_PUB_KEY from command line') - - if args.repo: - state['REP_ADDRESS'] = args.repo[0] - logger.info('Overriding REP_ADDRESS from command line') - - #if args.command: - # logger.info("Command: " + args.command) - - return state#, {'command': args.command, 'arg0': args.arg0, 'arg1': args.arg1, 'arg2': args.arg2, 'arg3': args.arg3, 'arg4': args.arg4, 'arg5': args.arg5} - -def save(state): - state_dir = os.path.join(os.path.expanduser('~'), '.sio') - state_file = os.path.join(state_dir, 'state.json') - - if not os.path.exists(state_dir): - logger.debug('Creating state folder') - os.mkdir(state_dir) - - with open(state_file, 'w') as f: - f.write(json.dumps(state, indent=4)) - -#Main function for checking commands -def main(args): - - state = load_state() - state = parse_env(state) - #state, args = parse_args(state) - state = parse_args(state) - - if 'REP_ADDRESS' not in state: - logger.error("Must define Repository Address") - sys.exit(-1) - - # if 'REP_PUB_KEY' not in state: - # logger.error("Must set the Repository Public Key") - # sys.exit(-1) - - return state - -if __name__ == '__main__': - main(sys.argv) diff --git a/delivery1/lib/asymmetric_functs.py b/delivery1/lib/asymmetric_functs.py index 6d2569a..1c0e7c1 100644 --- a/delivery1/lib/asymmetric_functs.py +++ b/delivery1/lib/asymmetric_functs.py @@ -66,8 +66,12 @@ def load_public_key(file): public_key = serialization.load_pem_public_key( key_file.read(), ) + public_key_pem = public_key.public_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PublicFormat.SubjectPublicKeyInfo + ).decode('utf-8') - return public_key + return public_key_pem # ----------------- # decrypt functions diff --git a/delivery1/lib/digest.py b/delivery1/lib/digest.py index 2b7452f..5e062ae 100644 --- a/delivery1/lib/digest.py +++ b/delivery1/lib/digest.py @@ -4,4 +4,4 @@ import cryptography.hazmat.primitives.hashes def get_hash(data): digest = cryptography.hazmat.primitives.hashes.Hash(cryptography.hazmat.primitives.hashes.SHA256()) digest.update(data) - return digest.finalize() \ No newline at end of file + return digest.finalize().hex() \ No newline at end of file