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

Generating an authorized_keys line from a SSHKey object

Open gbrault opened this issue 2 years ago • 1 comments

Thanks for this library.

I am creating keys using the SSHKey object providing each bit and pieces from a database.

I would like now generating a line of authorized_keys with this entry, what function should I add to the SSHKey object to serialize it in such a compatible string?

gbrault avatar Dec 23 '22 14:12 gbrault

a method to add to the keys.py file in the SSHKey file

    def encode(self):
        from cryptography.hazmat.primitives import serialization
        options = ",".join([f"""{k}="{v[0]}" """ for k,v in self.options.items()]).replace("'",'"').replace(" ,",",")
        key = self.rsa.public_bytes(encoding=serialization.Encoding.OpenSSH,format=serialization.PublicFormat.OpenSSH).decode('ascii')
        comment = self.comment
        rsa_key = f"{options} {key} {comment}"
        return rsa_key

gbrault avatar Dec 24 '22 14:12 gbrault