import base64 from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization def get_pkey(key_path, passphrase): """Load private key from file.""" with open(key_path, "rb") as key: p_key = serialization.load_pem_private_key(key.read(), password=passphrase.encode(), backend=default_backend()) return p_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption(), ) def decode_sm_pkey(sm_private_key): return base64.b64decode(bytes(sm_private_key, encoding="utf-8"))