Skip to content

Commit

Permalink
backwards compat
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Dec 30, 2024
1 parent 7edc2c2 commit f186f51
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions poorman_handshake/asymmetric/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import logging
import os
import warnings
from typing import Tuple, Union

from Cryptodome.Cipher import PKCS1_OAEP
Expand All @@ -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.
Expand All @@ -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):
Expand Down

0 comments on commit f186f51

Please sign in to comment.