fastapi-dls/app/util.py

39 lines
1.1 KiB
Python
Raw Normal View History

2024-06-13 18:16:51 +00:00
import logging
logging.basicConfig()
def load_file(filename: str) -> bytes:
log = logging.getLogger(f'{__name__}')
log.debug(f'Loading contents of file "{filename}')
2022-12-28 10:53:56 +00:00
with open(filename, 'rb') as file:
content = file.read()
return content
2024-06-13 18:16:51 +00:00
def load_key(filename: str) -> "RsaKey":
2022-12-29 19:40:42 +00:00
try:
# Crypto | Cryptodome on Debian
from Crypto.PublicKey import RSA
from Crypto.PublicKey.RSA import RsaKey
except ModuleNotFoundError:
from Cryptodome.PublicKey import RSA
from Cryptodome.PublicKey.RSA import RsaKey
2024-06-13 18:16:51 +00:00
log = logging.getLogger(__name__)
log.debug(f'Importing RSA-Key from "{filename}"')
2022-12-28 10:53:56 +00:00
return RSA.import_key(extern_key=load_file(filename), passphrase=None)
2022-12-29 19:40:42 +00:00
def generate_key() -> "RsaKey":
try:
# Crypto | Cryptodome on Debian
from Crypto.PublicKey import RSA
from Crypto.PublicKey.RSA import RsaKey
except ModuleNotFoundError:
from Cryptodome.PublicKey import RSA
from Cryptodome.PublicKey.RSA import RsaKey
2024-06-13 18:16:51 +00:00
log = logging.getLogger(__name__)
log.debug(f'Generating RSA-Key')
return RSA.generate(bits=2048)