From f186f51f02bbeb17e70436121430341a1f0fd40a Mon Sep 17 00:00:00 2001 From: miro Date: Mon, 30 Dec 2024 05:07:18 +0000 Subject: [PATCH] backwards compat --- poorman_handshake/asymmetric/utils.py | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/poorman_handshake/asymmetric/utils.py b/poorman_handshake/asymmetric/utils.py index 4ff511f..4806b82 100644 --- a/poorman_handshake/asymmetric/utils.py +++ b/poorman_handshake/asymmetric/utils.py @@ -1,3 +1,6 @@ +import logging +import os +import warnings from typing import Tuple, Union from Cryptodome.Cipher import PKCS1_OAEP @@ -6,6 +9,57 @@ from Cryptodome.Signature import pss +def export_private_key(path, key=None): + """ + Deprecated function for exporting an RSA private key. + Logs a deprecation warning and redirects to export_RSA_key. + + Args: + path (str): File path to save the key. + key: The RSA private key. + + Returns: + None + """ + warnings.warn( + "export_private_key is deprecated and will be removed in a future version. " + "Use export_RSA_key instead.", + DeprecationWarning, + stacklevel=2, + ) + logging.warning( + "export_private_key is deprecated and will be removed in a future version. Use export_RSA_key instead." + ) + export_RSA_key(key, path) + + +def create_private_key(name="PoorManHandshake", expires=None): + """ + Deprecated function for creating an RSA private key. + Logs a deprecation warning and creates a new RSA key. + + Args: + name (str): Unused parameter for naming the key. + expires: Unused parameter for key expiration. + + Returns: + RSA.RsaKey: The generated RSA private key. + """ + warnings.warn( + "create_private_key is deprecated and will be removed in a future version. " + "Use create_RSA_key instead.", + DeprecationWarning, + stacklevel=2, + ) + logging.warning( + "create_private_key is deprecated and will be removed in a future version. Use create_RSA_key instead." + ) + k = RSA.generate(2048) + # add property that NodeIdentity expects for compat + k.pubkey = k.public_key().export_key(format="PEM").decode("utf-8") + return k + + def export_RSA_key(key: Union[str, bytes, RSA.RsaKey], path: str): """ Exports an RSA key (public or private) to a file in PEM format. @@ -17,6 +71,9 @@ def export_RSA_key(key: Union[str, bytes, RSA.RsaKey], path: str): Returns: None """ + base = os.path.dirname(path) + if base: + os.makedirs(base, exist_ok=True) if isinstance(key, RSA.RsaKey): key = key.export_key(format="PEM") if isinstance(key, str):