From 9bf92d67435c2b0c6376e32a8625544e6445e892 Mon Sep 17 00:00:00 2001 From: Allan Eising Date: Sat, 10 May 2025 08:39:33 +0200 Subject: [PATCH] Add helper for writing private keys to disk --- src/sshecret/crypto.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/sshecret/crypto.py b/src/sshecret/crypto.py index 76f602d..d3be32e 100644 --- a/src/sshecret/crypto.py +++ b/src/sshecret/crypto.py @@ -104,6 +104,20 @@ def generate_pem(private_key: rsa.RSAPrivateKey) -> str: return pem.decode() +def write_private_key(private_key: rsa.RSAPrivateKey, filename: Path) -> None: + """Write private key to disk.""" + encryption_algorithm = serialization.NoEncryption() + with open(filename, "wb") as f: + pem = private_key.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.OpenSSH, + encryption_algorithm=encryption_algorithm, + ) + lines = f.write(pem) + LOG.debug("Wrote %s lines", lines) + f.flush() + + def create_private_rsa_key(filename: Path, password: str | None = None) -> None: """Create an RSA Private key at the given path.