Skip to content

Commit

Permalink
regen key
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Dec 30, 2024
1 parent 364f3a6 commit b338291
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions poorman_handshake/asymmetric/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
from binascii import hexlify, unhexlify
from os.path import isfile
from typing import Union

import shutil
from Cryptodome.PublicKey import RSA
from Cryptodome.Random import get_random_bytes

Expand Down Expand Up @@ -33,13 +35,16 @@ def __init__(self, path: str = None, key_size: int = 2048):
path (str, optional): Path to load or save the private key.
key_size (int, optional): Size of the RSA key in bits (default is 2048).
"""
self.private_key = None
if path and isfile(path):
try:
self.load_private(path)
except ValueError:
print(f"file does not look like a valid RSA key, please delete it and try again. '{path}'")
raise
else:
shutil.copy(path, path + ".bak")
os.remove(path)
print(f"file does not look like a valid RSA key, regenerating '{path}'")

if not self.private_key:
self.private_key = RSA.generate(key_size)
if path:
self.export_private_key(path)
Expand Down

0 comments on commit b338291

Please sign in to comment.