aboutsummaryrefslogtreecommitdiffstats
path: root/cryptography
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2013-10-22 19:24:31 -0700
committerAlex Gaynor <alex.gaynor@gmail.com>2013-10-22 19:24:31 -0700
commit25c0037d0d1bf4b4d426cc03b9390456d127d4d7 (patch)
treefebc087d21873d764c7701a18f79cf4eef649f9c /cryptography
parent68e5de708d623a03ea4cbd4d3a4297b5722950eb (diff)
parentf4c59767cdfe7716c82a72b00baa427637b505bd (diff)
downloadcryptography-25c0037d0d1bf4b4d426cc03b9390456d127d4d7.tar.gz
cryptography-25c0037d0d1bf4b4d426cc03b9390456d127d4d7.tar.bz2
cryptography-25c0037d0d1bf4b4d426cc03b9390456d127d4d7.zip
Merge pull request #172 from reaperhulk/api-to-backend-in-one-easy-step
The great api -> backend rename
Diffstat (limited to 'cryptography')
-rw-r--r--cryptography/bindings/__init__.py6
-rw-r--r--cryptography/bindings/openssl/__init__.py4
-rw-r--r--cryptography/bindings/openssl/backend.py (renamed from cryptography/bindings/openssl/api.py)12
-rw-r--r--cryptography/primitives/block/base.py36
-rw-r--r--cryptography/primitives/hashes.py27
5 files changed, 46 insertions, 39 deletions
diff --git a/cryptography/bindings/__init__.py b/cryptography/bindings/__init__.py
index 215f17c7..5006d134 100644
--- a/cryptography/bindings/__init__.py
+++ b/cryptography/bindings/__init__.py
@@ -14,7 +14,7 @@
from cryptography.bindings import openssl
-_default_api = openssl.api
-_ALL_APIS = [
- openssl.api
+_default_backend = openssl.backend
+_ALL_BACKENDS = [
+ openssl.backend
]
diff --git a/cryptography/bindings/openssl/__init__.py b/cryptography/bindings/openssl/__init__.py
index 103b1db0..cfe2e665 100644
--- a/cryptography/bindings/openssl/__init__.py
+++ b/cryptography/bindings/openssl/__init__.py
@@ -11,7 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-from cryptography.bindings.openssl.api import api
+from cryptography.bindings.openssl.backend import backend
-__all__ = ["api"]
+__all__ = ["backend"]
diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/backend.py
index fedaf9cc..492f2e54 100644
--- a/cryptography/bindings/openssl/api.py
+++ b/cryptography/bindings/openssl/backend.py
@@ -27,12 +27,12 @@ class GetCipherByName(object):
def __init__(self, fmt):
self._fmt = fmt
- def __call__(self, api, cipher, mode):
+ def __call__(self, backend, cipher, mode):
cipher_name = self._fmt.format(cipher=cipher, mode=mode).lower()
- return api.lib.EVP_get_cipherbyname(cipher_name.encode("ascii"))
+ return backend.lib.EVP_get_cipherbyname(cipher_name.encode("ascii"))
-class API(object):
+class Backend(object):
"""
OpenSSL API wrapper.
"""
@@ -144,7 +144,7 @@ class API(object):
def create_block_cipher_encrypt_context(self, cipher, mode):
ctx, evp, iv_nonce = self._create_block_cipher_context(cipher, mode)
- res = self.lib.EVP_EncryptInit_ex(ctx, evp, api.ffi.NULL, cipher.key,
+ res = self.lib.EVP_EncryptInit_ex(ctx, evp, self.ffi.NULL, cipher.key,
iv_nonce)
assert res != 0
# We purposely disable padding here as it's handled higher up in the
@@ -154,7 +154,7 @@ class API(object):
def create_block_cipher_decrypt_context(self, cipher, mode):
ctx, evp, iv_nonce = self._create_block_cipher_context(cipher, mode)
- res = self.lib.EVP_DecryptInit_ex(ctx, evp, api.ffi.NULL, cipher.key,
+ res = self.lib.EVP_DecryptInit_ex(ctx, evp, self.ffi.NULL, cipher.key,
iv_nonce)
assert res != 0
# We purposely disable padding here as it's handled higher up in the
@@ -247,4 +247,4 @@ class API(object):
return copied_ctx
-api = API()
+backend = Backend()
diff --git a/cryptography/primitives/block/base.py b/cryptography/primitives/block/base.py
index 12b6f626..e9f52887 100644
--- a/cryptography/primitives/block/base.py
+++ b/cryptography/primitives/block/base.py
@@ -17,58 +17,62 @@ from cryptography.primitives import interfaces
class BlockCipher(object):
- def __init__(self, cipher, mode, api=None):
+ def __init__(self, cipher, mode, backend=None):
super(BlockCipher, self).__init__()
- if api is None:
- from cryptography.bindings import _default_api as api
+ if backend is None:
+ from cryptography.bindings import _default_backend as backend
self.cipher = cipher
self.mode = mode
- self._api = api
+ self._backend = backend
def encryptor(self):
- return _CipherEncryptionContext(self.cipher, self.mode, self._api)
+ return _CipherEncryptionContext(self.cipher, self.mode, self._backend)
def decryptor(self):
- return _CipherDecryptionContext(self.cipher, self.mode, self._api)
+ return _CipherDecryptionContext(self.cipher, self.mode, self._backend)
@interfaces.register(interfaces.CipherContext)
class _CipherEncryptionContext(object):
- def __init__(self, cipher, mode, api):
+ def __init__(self, cipher, mode, backend):
super(_CipherEncryptionContext, self).__init__()
- self._api = api
- self._ctx = self._api.create_block_cipher_encrypt_context(cipher, mode)
+ self._backend = backend
+ self._ctx = self._backend.create_block_cipher_encrypt_context(
+ cipher, mode
+ )
def update(self, data):
if self._ctx is None:
raise ValueError("Context was already finalized")
- return self._api.update_encrypt_context(self._ctx, data)
+ return self._backend.update_encrypt_context(self._ctx, data)
def finalize(self):
if self._ctx is None:
raise ValueError("Context was already finalized")
- data = self._api.finalize_encrypt_context(self._ctx)
+ data = self._backend.finalize_encrypt_context(self._ctx)
self._ctx = None
return data
@interfaces.register(interfaces.CipherContext)
class _CipherDecryptionContext(object):
- def __init__(self, cipher, mode, api):
+ def __init__(self, cipher, mode, backend):
super(_CipherDecryptionContext, self).__init__()
- self._api = api
- self._ctx = self._api.create_block_cipher_decrypt_context(cipher, mode)
+ self._backend = backend
+ self._ctx = self._backend.create_block_cipher_decrypt_context(
+ cipher, mode
+ )
def update(self, data):
if self._ctx is None:
raise ValueError("Context was already finalized")
- return self._api.update_decrypt_context(self._ctx, data)
+ return self._backend.update_decrypt_context(self._ctx, data)
def finalize(self):
if self._ctx is None:
raise ValueError("Context was already finalized")
- data = self._api.finalize_decrypt_context(self._ctx)
+ data = self._backend.finalize_decrypt_context(self._ctx)
self._ctx = None
return data
diff --git a/cryptography/primitives/hashes.py b/cryptography/primitives/hashes.py
index 3aa52462..4cd68adc 100644
--- a/cryptography/primitives/hashes.py
+++ b/cryptography/primitives/hashes.py
@@ -19,35 +19,38 @@ import binascii
import six
-from cryptography.bindings import _default_api
-
class BaseHash(six.with_metaclass(abc.ABCMeta)):
- def __init__(self, data=None, api=None, ctx=None):
- if api is None:
- api = _default_api
- self._api = api
- self._ctx = self._api.create_hash_context(self) if ctx is None else ctx
+ def __init__(self, data=None, backend=None, ctx=None):
+ if backend is None:
+ from cryptography.bindings import _default_backend
+ backend = _default_backend
+ self._backend = backend
+ if ctx is None:
+ self._ctx = self._backend.create_hash_context(self)
+ else:
+ self._ctx = None
+
if data is not None:
self.update(data)
def update(self, data):
if isinstance(data, six.text_type):
raise TypeError("Unicode-objects must be encoded before hashing")
- self._api.update_hash_context(self._ctx, data)
+ self._backend.update_hash_context(self._ctx, data)
def copy(self):
- return self.__class__(api=self._api, ctx=self._copy_ctx())
+ return self.__class__(backend=self._backend, ctx=self._copy_ctx())
def digest(self):
- return self._api.finalize_hash_context(self._copy_ctx(),
- self.digest_size)
+ return self._backend.finalize_hash_context(self._copy_ctx(),
+ self.digest_size)
def hexdigest(self):
return str(binascii.hexlify(self.digest()).decode("ascii"))
def _copy_ctx(self):
- return self._api.copy_hash_context(self._ctx)
+ return self._backend.copy_hash_context(self._ctx)
class SHA1(BaseHash):