SolarEnergy/python_device/src/pki.py

20 lines
603 B
Python
Raw Normal View History

from OpenSSL import crypto
def gen_priv_key():
key = crypto.PKey()
key.generate_key(crypto.TYPE_RSA, 2048)
2024-06-30 08:14:42 +00:00
return crypto.dump_privatekey(crypto.FILETYPE_PEM, key).decode("utf-8")
def parse_priv_key(priv_key: str) -> crypto.PKey:
return crypto.load_privatekey(crypto.FILETYPE_PEM, priv_key)
def gen_csr(priv_key: str, cn: str) -> str:
priv_key = parse_priv_key(priv_key)
req = crypto.X509Req()
req.get_subject().CN = cn
req.set_pubkey(priv_key)
req.sign(priv_key, "sha256")
return crypto.dump_certificate_request(crypto.FILETYPE_PEM, req).decode("utf-8")