Skip to content

Commit

Permalink
Add documentation and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Jun 5, 2016
1 parent 31573fb commit cdffb80
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Deprecations:
Changes:
^^^^^^^^

*none*
- ``OpenSSL.crypto.PKey`` can now be constructed from ``cryptography`` objects and also exported as such.
`#439 <https://github.com/pyca/pyopenssl/pull/439>`_


----
Expand Down
20 changes: 20 additions & 0 deletions src/OpenSSL/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,33 @@ def __init__(self):
self._initialized = False

def to_cryptography_key(self):
"""
Export as a ``cryptography`` key.
:rtype: One of ``cryptography``'s `key interfaces`_.
.. _key interfaces: https://cryptography.io/en/latest/hazmat/\
primitives/asymmetric/rsa/#key-interfaces
.. versionadded:: 16.1.0
"""
if self._only_public:
return backend._evp_pkey_to_public_key(self._pkey)
else:
return backend._evp_pkey_to_private_key(self._pkey)

@classmethod
def from_cryptography_key(cls, crypto_key):
"""
Construct based on a ``cryptography`` *crypto_key*.
:param crypto_key: A ``cryptography`` key.
:type crypto_key: One of ``cryptography``'s `key interfaces`_.
:rtype: PKey
.. versionadded:: 16.1.0
"""
pkey = cls()
pkey._pkey = crypto_key._evp_pkey
if isinstance(crypto_key, (rsa.RSAPublicKey, dsa.DSAPublicKey)):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ class TestPKey(object):

def test_convert_from_cryptography_private_key(self):
"""
Convert from a cryptography private key to a pyOpenSSL PKey.
PKey.from_cryptography_key creates a proper private PKey.
"""
key = serialization.load_pem_private_key(
intermediate_key_pem, None, backend
Expand All @@ -772,7 +772,7 @@ def test_convert_from_cryptography_private_key(self):

def test_convert_from_cryptography_public_key(self):
"""
Convert from a cryptography public key to a pyOpenSSL PKey.
PKey.from_cryptography_key creates a proper public PKey.
"""
key = serialization.load_pem_public_key(cleartextPublicKeyPEM, backend)
pkey = PKey.from_cryptography_key(key)
Expand All @@ -784,7 +784,7 @@ def test_convert_from_cryptography_public_key(self):

def test_convert_public_pkey_to_cryptography_key(self):
"""
Convert from a pyOpenSSL PKey to a cryptography public key.
PKey.to_cryptography_key creates a proper cryptography public key.
"""
pkey = load_publickey(FILETYPE_PEM, cleartextPublicKeyPEM)
key = pkey.to_cryptography_key()
Expand All @@ -794,7 +794,7 @@ def test_convert_public_pkey_to_cryptography_key(self):

def test_convert_private_pkey_to_cryptography_key(self):
"""
Convert from a pyOpenSSL PKey to a cryptography private key.
PKey.to_cryptography_key creates a proper cryptography private key.
"""
pkey = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)
key = pkey.to_cryptography_key()
Expand Down

0 comments on commit cdffb80

Please sign in to comment.