Convert python cryptography EC key to OpenSSH format
Question: I am looking to convert EC key generated using cryptography module to their
respective OpenSSH strings. like

    
    
    ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAhANiNlmyHtBUgaPXG+CtCVK8mQxBUtDjX3/nqqPZAHhduAAAAIE/JNDqLTeq9WVa5XWyU2Y7NJXfV54wakHmsP5gRNeh2
    

This is the code I use for EC key generation

    
    
    from cryptography.hazmat.backends import default_backend
    from cryptography.hazmat.primitives.asymmetric import ec
    key=ec.generate_private_key(ec.SECP256R1(), default_backend())
    

I tried the following.

    
    
    numbers = key.private_numbers()
    opensshpublic = 'ecdsa-sha2-nistp256' + base64.b64encode('nistp256' + numbers.public_numbers.x, numbers.public_numbers.y)
    

but that didn't seem to work.

I suppose there should be a easy way to do this but I am missing something.


Answer: Cryptography added support for doing this in June 2016, it is possible as
follows

    
    
    from cryptography.hazmat.primitives import serialization
    key.public_bytes(serialization.Encoding.OpenSSH, serialization.PublicFormat.OpenSSH)
    

this gives those neat OpenSSH public keys

