python-sshpubkeys
python-sshpubkeys copied to clipboard
Generating an authorized_keys line from a SSHKey object
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?
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