python-sshpubkeys icon indicating copy to clipboard operation
python-sshpubkeys copied to clipboard

Any examples of SSH-formatted key output for all types of keys?

Open nskmda opened this issue 11 months ago • 0 comments

Team,

I need to convert SSH2-formatted public keys to the SSH format. So far (by poking around) I managed to find out there's this public_bytes() method on the RSA type of keys (the ones which are put in the SSHKey.rsa attribute). But I also need to support other key types (like ECDSA). How should I serialize those into the SSH format?

The use case: original public keys are 'stored' in the 'plain text' (PEM? not much familiar w/the lingo) format containing those ---- BEGIN SSH2 PUBLIC KEY ---- markers. This format needs to be converted to the SSH (OpenSSH?) format like ssh-rsa <base64-encoded data>.

I tried to looks through the source code but am not sure about what to do w/apparently 'binary' string for a ECDSA public keys.

My current code looks like:

        parsedKey = SSHKey(publicKey)
        parsedKey.parse()
        if parsedKey.rsa:
            return (
                parsedKey.rsa.public_bytes(
                    serialization.Encoding.OpenSSH,
                    serialization.PublicFormat.OpenSSH)).decode()
        else:
            # this is wrong data format. need help here
            return parsedKey.ecdsa.to_string()

Any help?

nskmda avatar Jan 23 '25 19:01 nskmda