mirror of
https://gitea.publichub.eu/oscar.krause/fastapi-dls.git
synced 2025-11-09 01:36:06 +00:00
main.app
This commit is contained in:
28
app/helper.py
Normal file
28
app/helper.py
Normal file
@@ -0,0 +1,28 @@
|
||||
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
|
||||
)
|
||||
Reference in New Issue
Block a user