Skip to content

Instantly share code, notes, and snippets.

@ohaval
Created November 1, 2021 17:34
Show Gist options
  • Save ohaval/b7c5ec05c7391c103a1e8a2342ceba3e to your computer and use it in GitHub Desktop.
Save ohaval/b7c5ec05c7391c103a1e8a2342ceba3e to your computer and use it in GitHub Desktop.
Generate and export ECDSA public and private keys in Python.
"""This example shows how easy it is to generate and export ECDSA keys with python.
This program is similar to `ssh-keygen -t ecdsa` with no passphrase.
To export the private key with a passphrase, read paramiko.pkey.PKey._write_private_key method.
"""
import paramiko
from cryptography.hazmat.primitives.serialization import (
Encoding, PrivateFormat, PublicFormat, NoEncryption
)
key = paramiko.ECDSAKey.generate()
with open("id_ecdsa", "wb") as fh:
data = key.signing_key.private_bytes(Encoding.PEM,
PrivateFormat.OpenSSH,
NoEncryption())
fh.write(data)
with open("id_ecdsa.pub", "wb") as fh:
data = key.verifying_key.public_bytes(Encoding.OpenSSH,
PublicFormat.OpenSSH)
fh.write(data + b"\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment