def store_keys(privkey: RsaKey, pubkey: RsaKey): try: priv_file: BinaryIO = open(".config/rsa_private.key", "wb") pub_file: BinaryIO = open(".config/rsa_public.key", "wb") priv_file.write(privkey.export_key()) pub_file.write(pubkey.export_key()) priv_file.close() pub_file.close() except Exception as e: print(e) print("Cannot store keys in .config dir. Exiting...") raise SystemExit
def rsa_export_key(self, key_file: str, key: RSA.RsaKey) -> None: """Export RSA key to file Args: key_file (str): Path to RSA key file key (RSA.RsaKey): RSA key to export """ key_file_obj = Path(key_file).resolve() key_file_obj.parent.mkdir(parents=True, exist_ok=True) with key_file_obj.open('wb') as f: f.write(key.export_key())
def write_pubkey(self, pubkey: RSA.RsaKey) -> None: """Write a pubkey to the endpoint.""" print(f"Sending a pubkey to {self.endpoint_addr}") self._sock.sendall( Message.of(pubkey.export_key(), ContentType.BINARY).to_bytes()) self._sent_pubkey = True if self._endpoint_pubkey is not None: self._connected = True print(f"Established connection to {self.endpoint_addr}")
def user_register(self, username: str, email: str, public_key: RsaKey): """ Registers a new user :param username: :param email: :param public_key: :return: a dictionary with keys userID and ts """ url = API.base_url + "/users/register" body = { "nombre": username, "email": email, "publicKey": public_key.export_key("PEM").decode() } response = requests.post(url, headers=self.header, json=body) parsed_response = json.loads(response.text) if response.status_code != 200: raise api_exceptions[parsed_response["error_code"]] return parsed_response
def set_key(self, key: RsaKey): self.config["SecureBox"]["key"] = key.export_key("PEM").decode()
def __add_host_key(self, new_key: RSA.RsaKey, hostname: str) -> None: self.acceptable_hosts.append(new_key) with open(f'{self.path}/{hostname}', 'w') as key_file: key_file.writelines(new_key.export_key(format='OpenSSH').decode('utf-8'))