def generate_key_pair(self): # 3.1.1 questions = [{ 'type': 'list', 'name': 'regenerate', 'message': 'Do you want to regenerate the key pair?', 'choices': ['Yes', 'No'], 'filter': lambda val: val.lower() }, { 'type': 'list', 'name': 'save_pwd', 'message': 'Do you want save password?', 'choices': ['Yes', 'No'], 'filter': lambda val: val.lower(), 'when': lambda answer: answer['regenerate'] == 'yes' }] crypto = CryptoUtility() answer = prompt(questions, style=self.style) if answer['regenerate'] == 'yes': crypto.generate_key_pair() print('Generating key pair...') crypto.password = self._set_password() if answer['save_pwd'] == 'yes': print(crypto.export_password_hash_to_file()) else: crypto.delete_password_hash_file() print(crypto.export_private_key_to_file()) print(crypto.export_public_key_to_file()) print('Key pair successfully generated!\n') self._show_public_key() self.configure_key_pair() else: self.configure_key_pair()
def set_key_pair_from_string(self): # 3.1.3 questions = [ { 'type': 'input', 'name': 'private_key_store', 'message': 'Input private key store json:', }, { 'type': 'password', 'message': 'Enter the password to decrypt private key:', 'name': 'password', 'when': lambda answer: answer['private_key_store'] != '', }, ] new_password = [ { 'type': 'list', 'name': 'new_pwd', 'message': 'Do you want set a new password?', 'choices': ['Yes', 'No'], 'filter': lambda val: val.lower(), } ] save_password = [ { 'type': 'list', 'name': 'save_pwd', 'message': 'Do you want to save password?', 'choices': ['Yes', 'No'], 'filter': lambda val: val.lower(), } ] crypto = CryptoUtility(self.key_path) answer = prompt(questions) if answer['private_key_store'] != '': print('Setting key pair...') try: crypto.password = answer['password'] if not crypto.set_private_key(answer['private_key_store']): self.configure_key_pair() answer = prompt(new_password) if answer['new_pwd'] == 'yes': crypto.password = self._set_password() answer = prompt(save_password) if answer['save_pwd'] == 'yes': crypto.export_password_hash_to_file() else: crypto.delete_password_hash_file() print(crypto.export_private_key_to_file()) print(crypto.export_public_key_to_file()) print('Key pair successfully generated!') except Exception as e: print(e) self.configure_key_pair()
def set_public_key_from_string(self): # 3.2.2 questions = [{ 'type': 'input', 'name': 'public_key', 'message': 'Input public_key as Base64:', }] crypto = CryptoUtility() answer = prompt(questions, style=self.style) if answer['public_key'] != '': try: crypto.set_public_key(answer['public_key']) print(crypto.export_public_key_to_file()) print('Key successfully stored!\n') except Exception as e: print(e) self.configure_public_key()