Skip to content

Commit

Permalink
change as_bytes to private_bytes, link more things
Browse files Browse the repository at this point in the history
  • Loading branch information
reaperhulk committed Mar 1, 2015
1 parent 35194c9 commit 223a8f0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Changelog
and deprecated
:class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithNumbers`.
* Added
:meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization.as_bytes`
:meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization.private_bytes`
to
:class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization`.

Expand Down
8 changes: 4 additions & 4 deletions docs/hazmat/primitives/asymmetric/rsa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ Key serialization
If you have a key that you've loaded or generated which implements the
:class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization`
interface you can use
:meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization.as_bytes`
:meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization.private_bytes`
to serialize the key.

.. doctest::

>>> from cryptography.hazmat.primitives import serialization
>>> pem = private_key.as_bytes(
>>> pem = private_key.private_bytes(
... encoding=serialization.Encoding.PEM,
... format=serialization.Format.PKCS8,
... encryption_algorithm=serialization.BestAvailableEncryption(b'mypassword')
Expand All @@ -105,7 +105,7 @@ It is also possible to serialize without encryption using

.. doctest::

>>> pem = private_key.as_bytes(
>>> pem = private_key.private_bytes(
... encoding=serialization.Encoding.PEM,
... format=serialization.Format.TraditionalOpenSSL,
... encryption_algorithm=serialization.NoEncryption()
Expand Down Expand Up @@ -534,7 +534,7 @@ Key interfaces
:class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateNumbers`
instance.

.. method:: as_bytes(encoding, format, encryption_algorithm)
.. method:: private_bytes(encoding, format, encryption_algorithm)

Allows serialization of the key to bytes. Encoding (
:attr:`~cryptography.hazmat.primitives.serialization.Encoding.PEM` or
Expand Down
11 changes: 7 additions & 4 deletions docs/hazmat/primitives/asymmetric/serialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ Serialization Formats

.. versionadded:: 0.8

An enumeration for key formats.
An enumeration for key formats. Used with
:class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization.private_bytes`.

.. attribute:: TraditionalOpenSSL

Expand All @@ -310,7 +311,8 @@ Serialization Encodings

.. versionadded:: 0.8

An enumeration for encoding types.
An enumeration for encoding types. Used with
:class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization.private_bytes`.

.. attribute:: PEM

Expand All @@ -328,9 +330,10 @@ Serialization Encryption Types

Objects with this interface are usable as encryption types with methods
like
:meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization.as_bytes`.
:meth:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization.private_bytes`.
All other classes in this section represent the available choices for
encryption and have this interface.
encryption and have this interface. They are used with
:class:`~cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKeyWithSerialization.private_bytes`.

.. class:: BestAvailableEncryption(password)

Expand Down
2 changes: 1 addition & 1 deletion src/cryptography/hazmat/backends/openssl/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def private_numbers(self):
)
)

def as_bytes(self, encoding, format, encryption_algorithm):
def private_bytes(self, encoding, format, encryption_algorithm):
if not isinstance(encoding, Encoding):
raise TypeError("encoding must be an item from the Encoding enum")

Expand Down
2 changes: 1 addition & 1 deletion src/cryptography/hazmat/primitives/asymmetric/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def private_numbers(self):
"""

@abc.abstractmethod
def as_bytes(self, encoding, format, encryption_algorithm):
def private_bytes(self, encoding, format, encryption_algorithm):
"""
Returns the key serialized as bytes.
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/hazmat/backends/test_openssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def test_password_length_limit(self):
password = b"x" * 1024
key = RSA_KEY_2048.private_key(backend)
with pytest.raises(ValueError):
key.as_bytes(
key.private_bytes(
serialization.Encoding.PEM,
serialization.Format.PKCS8,
serialization.BestAvailableEncryption(password)
Expand All @@ -511,7 +511,7 @@ def test_password_length_limit(self):
def test_unsupported_key_encoding(self):
key = RSA_KEY_2048.private_key(backend)
with pytest.raises(ValueError):
key.as_bytes(
key.private_bytes(
serialization.Encoding.DER,
serialization.Format.PKCS8,
serialization.NoEncryption()
Expand Down
28 changes: 14 additions & 14 deletions tests/hazmat/primitives/test_rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1764,10 +1764,10 @@ class TestRSAPEMWriter(object):
]
)
)
def test_as_bytes_encrypted_pem(self, backend, fmt, password):
def test_private_bytes_encrypted_pem(self, backend, fmt, password):
key = RSA_KEY_2048.private_key(backend)
_skip_if_no_serialization(key, backend)
serialized = key.as_bytes(
serialized = key.private_bytes(
serialization.Encoding.PEM,
fmt,
serialization.BestAvailableEncryption(password)
Expand All @@ -1783,10 +1783,10 @@ def test_as_bytes_encrypted_pem(self, backend, fmt, password):
"fmt",
[serialization.Format.TraditionalOpenSSL, serialization.Format.PKCS8],
)
def test_as_bytes_unencrypted_pem(self, backend, fmt):
def test_private_bytes_unencrypted_pem(self, backend, fmt):
key = RSA_KEY_2048.private_key(backend)
_skip_if_no_serialization(key, backend)
serialized = key.as_bytes(
serialized = key.private_bytes(
serialization.Encoding.PEM,
fmt,
serialization.NoEncryption()
Expand All @@ -1798,7 +1798,7 @@ def test_as_bytes_unencrypted_pem(self, backend, fmt):
priv_num = key.private_numbers()
assert loaded_priv_num == priv_num

def test_as_bytes_traditional_openssl_unencrypted_pem(self, backend):
def test_private_bytes_traditional_openssl_unencrypted_pem(self, backend):
key_bytes = load_vectors_from_file(
os.path.join(
"asymmetric",
Expand All @@ -1808,48 +1808,48 @@ def test_as_bytes_traditional_openssl_unencrypted_pem(self, backend):
lambda pemfile: pemfile.read().encode()
)
key = serialization.load_pem_private_key(key_bytes, None, backend)
serialized = key.as_bytes(
serialized = key.private_bytes(
serialization.Encoding.PEM,
serialization.Format.TraditionalOpenSSL,
serialization.NoEncryption()
)
assert serialized == key_bytes

def test_as_bytes_invalid_encoding(self, backend):
def test_private_bytes_invalid_encoding(self, backend):
key = RSA_KEY_2048.private_key(backend)
_skip_if_no_serialization(key, backend)
with pytest.raises(TypeError):
key.as_bytes(
key.private_bytes(
"notencoding",
serialization.Format.PKCS8,
serialization.NoEncryption()
)

def test_as_bytes_invalid_format(self, backend):
def test_private_bytes_invalid_format(self, backend):
key = RSA_KEY_2048.private_key(backend)
_skip_if_no_serialization(key, backend)
with pytest.raises(TypeError):
key.as_bytes(
key.private_bytes(
serialization.Encoding.PEM,
"invalidformat",
serialization.NoEncryption()
)

def test_as_bytes_invalid_encryption_algorithm(self, backend):
def test_private_bytes_invalid_encryption_algorithm(self, backend):
key = RSA_KEY_2048.private_key(backend)
_skip_if_no_serialization(key, backend)
with pytest.raises(TypeError):
key.as_bytes(
key.private_bytes(
serialization.Encoding.PEM,
serialization.Format.TraditionalOpenSSL,
"notanencalg"
)

def test_as_bytes_unsupported_encryption_type(self, backend):
def test_private_bytes_unsupported_encryption_type(self, backend):
key = RSA_KEY_2048.private_key(backend)
_skip_if_no_serialization(key, backend)
with pytest.raises(ValueError):
key.as_bytes(
key.private_bytes(
serialization.Encoding.PEM,
serialization.Format.TraditionalOpenSSL,
DummyKeyEncryption()
Expand Down

0 comments on commit 223a8f0

Please sign in to comment.