2019-05-25 07:17:52 -03:00
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
from cryptography.hazmat.primitives import serialization
|
2023-11-30 07:55:51 -08:00
|
|
|
from cryptography.hazmat.primitives.asymmetric import ed25519, rsa
|
2019-05-25 07:17:52 -03:00
|
|
|
|
|
|
|
|
2023-10-12 23:43:41 +05:30
|
|
|
def check_private_key(private_key_material, key_type):
|
2021-07-26 07:40:39 +01:00
|
|
|
assert isinstance(private_key_material, str)
|
2019-05-25 07:17:52 -03:00
|
|
|
|
2023-10-12 23:43:41 +05:30
|
|
|
if key_type == "rsa":
|
|
|
|
private_key = serialization.load_pem_private_key(
|
|
|
|
data=private_key_material.encode("ascii"),
|
|
|
|
backend=default_backend(),
|
|
|
|
password=None,
|
|
|
|
)
|
|
|
|
assert isinstance(private_key, rsa.RSAPrivateKey)
|
|
|
|
elif key_type == "ed25519":
|
|
|
|
private_key = serialization.load_ssh_private_key(
|
|
|
|
data=private_key_material.encode("ascii"),
|
|
|
|
password=None,
|
|
|
|
)
|
|
|
|
assert isinstance(private_key, ed25519.Ed25519PrivateKey)
|
|
|
|
else:
|
|
|
|
raise AssertionError("Bad private key")
|