Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Generating an authorized_keys line from a SSHKey object #96

Open
gbrault opened this issue Dec 23, 2022 · 1 comment
Open

Generating an authorized_keys line from a SSHKey object #96

gbrault opened this issue Dec 23, 2022 · 1 comment

Comments

@gbrault
Copy link

gbrault commented Dec 23, 2022

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
Copy link
Author

gbrault commented Dec 24, 2022

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant