SSH Public Key Parser

3.3.1 · active · verified Sun Apr 12

sshpubkeys is a Python library for parsing SSH public keys. It supports various key types (RSA, DSA, ECDSA, Ed25519) and can handle authorized_keys file formats, including options like 'command=' or 'no-port-forwarding'. The current version is 3.3.1, and it maintains an active, feature-driven release cadence.

Warnings

Install

Imports

Quickstart

Initialize an SSHKey object from a public key string, retrieve its properties, and perform basic validation. Demonstrates handling of `InvalidKeyException`.

from sshpubkeys import SSHKey, InvalidKeyException
import os

# This is a dummy example key. Replace with a real key or load from a file.
# For actual use, avoid hardcoding keys directly in code.
# A real key might look like: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCq2z/t... user@example.com'
example_public_key_string = os.environ.get(
    'SSH_PUBLIC_KEY', 
    'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCw0Y1c/tFk9k/N... dummy_key@example.com'
)

try:
    key = SSHKey(example_public_key_string)
    print(f"Key type: {key.key_type}")
    print(f"Key length: {key.length}")
    print(f"Fingerprint (MD5): {key.hash_md5}")
    print(f"Fingerprint (SHA256): {key.hash_sha256}")
    print(f"Comment: {key.comment}")
    key.verify_length() # Ensures key length is standard for its type
    print("Key is valid and has standard length.")
except InvalidKeyException as e:
    print(f"Error parsing SSH key: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →