Registry / auth-security / sshpubkeys

sshpubkeys

JSON →
library3.3.1pypypi✓ verified 22d ago

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.

pip install sshpubkeys
INSTALL
IMPORT
SIG · SSHPUBKEYS
S
sshpubkeys
auth-securitypythonv3.3.1
Install
2.6s avg
Import
73ms
Disk
35MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.3.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.072s · 36.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.6s · import 0.074s · 37MB
35MB installed
● package 35MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SSHKey
from sshpubkeys import SSHKey
InvalidKeyException
from sshpubkeys import InvalidKeyException

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}")
Debug
Known issues
breakingSupport for Python 2.6 and Python 3.3 was dropped.
fix
Upgrade your Python environment to Python 3.4+ (preferably 3.6+).
affects: v3.0.0 onwards
breakingHandling of DSA keys changed; 'loose mode' no longer accepts non-standard DSA key lengths.
fix
Ensure all DSA keys conform to standard lengths (e.g., 1024-bit). If you rely on non-standard lengths, you may need to regenerate or migrate keys.
affects: v3.0.0 onwards
breakingThe internal ECDSA implementation dependency shifted from `ecdsa` to `cryptography`. While generally internal, direct interaction with `_key.ecdsa_key` or similar internal objects will break.
fix
Refactor code to use only the public API of `SSHKey`. Avoid accessing internal, private attributes or methods that may rely on specific underlying cryptographic libraries.
affects: v3.2.0 onwards
gotchaPython 3.5 support was temporarily broken in versions immediately preceding `3.3.1`, despite earlier support.
fix
If using Python 3.5, ensure you are on `sshpubkeys` version `3.3.1` or later for correct compatibility, or preferably, upgrade to a newer, actively supported Python version.
affects: v3.3.0 (specifically a bug introduced and then reverted by v3.3.1)
gotchaA typo `ed25516` was corrected to `ed25519` for the key type. While primarily an internal fix, users relying on string comparisons with the incorrect type might be affected.
fix
Always use the `key_type` property and verify against the correct 'ed25519' string. Ensure your code doesn't implicitly rely on the old typo for string comparisons or type lookups.
affects: v3.2.0 onwards (fix applied in v3.2.0)
Errors
Common errors & fixes
PublicKeyError: Invalid key format
The input string provided to SSHKey does not represent a valid SSH public key in any recognized format.
fix
Ensure the input string is a correctly formatted SSH public key (e.g., 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...'); often caused by malformed or incomplete key strings.
PublicKeyError: Invalid options in authorized_keys line: <MALFORMED_OPTION>
The options specified in an authorized_keys line are malformed or use incorrect syntax (e.g., missing quotes around command values).
fix
Correct the syntax of the options in the authorized_keys line, ensuring proper quoting for values (e.g., `command="/usr/bin/my_script"` instead of `command=/usr/bin/my_script`).
PublicKeyError: Unknown key type: <UNKNOWN_TYPE>
The SSH key type specified in the public key string (e.g., 'ssh-rsa', 'ssh-dss') is not recognized or supported by the sshpubkeys library.
fix
Verify that the key type is a standard and supported SSH key type (e.g., `ssh-rsa`, `ssh-dss`, `ecdsa-sha2-nistp256`, `ssh-ed25519`) and that it is spelled correctly.
Upgrade
Version history
3.3.1latest on PyPI · released Feb 4, 2021
Audit
Dependencies
cryptographyrequiredProvides underlying cryptographic primitives for key parsing and validation. Replaced `ecdsa` as of v3.2.0.
Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources
sshpubkeys — pip install sshpubkeys · libregistry