fastapi-dls/app/helper.py
Oscar Krause b5cade6a95 main.app
2022-12-16 13:51:14 +01:00

29 lines
902 B
Python

from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKeyWithSerialization
from cryptography.hazmat.primitives.serialization import load_pem_private_key, Encoding, PrivateFormat, PublicFormat, \
NoEncryption
def load_file(filename) -> bytes:
with open(filename, 'rb') as file:
content = file.read()
return content
def load_key(filename) -> RSAPrivateKeyWithSerialization:
return load_pem_private_key(data=load_file(filename), password=None)
def private_bytes(rsa: RSAPrivateKeyWithSerialization) -> bytes:
return rsa.private_bytes(
encoding=Encoding.PEM,
format=PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=NoEncryption()
)
def public_key(rsa: RSAPrivateKeyWithSerialization) -> bytes:
return rsa.public_key().public_bytes(
encoding=Encoding.PEM,
format=PublicFormat.SubjectPublicKeyInfo
)