diff options
author | Jeff Forcier <jeff@bitprophet.org> | 2023-04-14 16:45:06 -0400 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2023-05-05 12:27:03 -0400 |
commit | 5d3aad0ca44df998bfe37f23d371926ff1a7ffb3 (patch) | |
tree | a7df0707d51d971d9a3183bbf2d948a12f80b9db | |
parent | 57c33481fda5cd9338df2bb4d5c090f50bcc7343 (diff) |
Add algorithm_name property to PKey
-rw-r--r-- | paramiko/pkey.py | 14 | ||||
-rw-r--r-- | sites/www/changelog.rst | 5 | ||||
-rw-r--r-- | tests/test_pkey.py | 5 |
3 files changed, 24 insertions, 0 deletions
diff --git a/paramiko/pkey.py b/paramiko/pkey.py index f8878be3..69117bc1 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -142,6 +142,20 @@ class PKey: """ return "" + @property + def algorithm_name(self): + """ + Return the key algorithm identifier for this key. + + Similar to `get_name`, but aimed at pure algorithm name instead of SSH + protocol field value. + """ + # TODO in Python 3.9: use .removeprefix() + no_prefix = self.get_name().replace("ssh-", "") + # Mostly for ECDSA's sake; OpenSSH does basically this too. + no_suffix = no_prefix.split("-")[0] + return no_suffix.upper() + def get_bits(self): """ Return the number of significant bits in this key. This is useful diff --git a/sites/www/changelog.rst b/sites/www/changelog.rst index 8ff9f179..0f652650 100644 --- a/sites/www/changelog.rst +++ b/sites/www/changelog.rst @@ -2,6 +2,11 @@ Changelog ========= +- :feature:`-` `~paramiko.pkey.PKey` grew a new ``.algorithm_name`` property + which displays the key algorithm; this is typically derived from the value of + `~paramiko.pkey.PKey.get_name`. For example, ED25519 keys have a ``get_name`` + of ``ssh-ed25519`` (the SSH protocol key type field value), and now have a + ``algorithm_name`` of ``ED25519``. - :feature:`-` `~paramiko.pkey.PKey` grew a new ``.fingerprint`` property which emits a fingerprint string matching the SHA256+Base64 values printed by various OpenSSH tooling (eg ``ssh-add -l``, ``ssh -v``). This is intended to diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 477da0e3..b1b9d51c 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -653,6 +653,11 @@ class KeyTest(unittest.TestCase): "SHA256:J6VESFdD3xSChn8y9PzWzeF+1tl892mOy2TqkMLO4ow", ] + def test_algorithm_property(self): + # Assumes the RSA, DSS, ECDSA, Ed25519 order seen in 'def keys'. + algorithms = [x.algorithm_name for x, _ in self.keys()] + assert algorithms == ["RSA", "DSS", "ECDSA", "ED25519"] + def test_ed25519_nonbytes_password(self): # https://github.com/paramiko/paramiko/issues/1039 Ed25519Key.from_private_key_file( |