export_key

classmethod KeyManager.export_key(key: Union[cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization, cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKeyWithSerialization, cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PrivateKey, cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey, cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey, cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PublicKey], **kwargs)bytes[source]

Export/serialize a given public/private key object as bytes.

Uses the default formatting arguments for the detected algorithm from identify_algorithm(), but you can also force it to treat it as a certain algorithm by passing alg

Uses default formatting options by looking up the algorithm in default_formats

Uses the private key serialization arguments for the detected algorithm out of generators

Example:

>>> priv, pub = KeyManager.generate_keypair_raw('ed25519')
>>> key = KeyManager.export_key(pub)
>>> print(key.decode())
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIOeLS2XOcQz11VUnzh6KIZaNtT10YfzHv779zjm95XSy
-----END PRIVATE KEY-----
Parameters
  • alg (str) – An algorithm name as a string, e.g. rsa or ed25519

  • key (combined_key_types) – An instance of a public/private key type listed in combined_key_types

  • format (Format) – Override some or all of the default format/encoding for the keys. Dict Keys: private_format,public_format,private_encoding,public_encoding

  • format – If passed a Format instance, then this instance will be used for serialization instead of merging defaults from default_formats

  • alg – Use this algorithm name e.g. 'rsa' instead of detecting using identify_algorithm()

Return bytes key

The serialized key.