aboutsummaryrefslogtreecommitdiffstats
path: root/cryptography
diff options
context:
space:
mode:
authorDonald Stufft <donald@stufft.io>2013-10-03 11:26:42 -0700
committerDonald Stufft <donald@stufft.io>2013-10-03 11:26:42 -0700
commit0f96716946c80fbaa2bab7813746baaba017f08d (patch)
tree9ebbf38b4779b64effec121a6d562c543b613e8b /cryptography
parent14fdcd186c9f6c7ccc1e6388347cd584822bc041 (diff)
parent81a5287984cd31080f7a5a1b249caf626ac8f6bf (diff)
downloadcryptography-0f96716946c80fbaa2bab7813746baaba017f08d.tar.gz
cryptography-0f96716946c80fbaa2bab7813746baaba017f08d.tar.bz2
cryptography-0f96716946c80fbaa2bab7813746baaba017f08d.zip
Merge pull request #85 from alex/explicit-api
Explicitly pass around the API, and run all tests under all available AP...
Diffstat (limited to 'cryptography')
-rw-r--r--cryptography/bindings/__init__.py17
-rw-r--r--cryptography/primitives/block/base.py14
2 files changed, 26 insertions, 5 deletions
diff --git a/cryptography/bindings/__init__.py b/cryptography/bindings/__init__.py
index e69de29b..8b165009 100644
--- a/cryptography/bindings/__init__.py
+++ b/cryptography/bindings/__init__.py
@@ -0,0 +1,17 @@
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from cryptography.bindings.openssl import api
+
+
+_default_api = api
diff --git a/cryptography/primitives/block/base.py b/cryptography/primitives/block/base.py
index b4137fd4..b84ca9c4 100644
--- a/cryptography/primitives/block/base.py
+++ b/cryptography/primitives/block/base.py
@@ -15,8 +15,7 @@ from __future__ import absolute_import, division, print_function
from enum import Enum
-# TODO: which binding is used should be an option somewhere
-from cryptography.bindings.openssl import api
+from cryptography.bindings import _default_api
class _Operation(Enum):
@@ -25,10 +24,15 @@ class _Operation(Enum):
class BlockCipher(object):
- def __init__(self, cipher, mode):
+ def __init__(self, cipher, mode, api=None):
super(BlockCipher, self).__init__()
+
+ if api is None:
+ api = _default_api
+
self.cipher = cipher
self.mode = mode
+ self._api = api
self._ctx = api.create_block_cipher_context(cipher, mode)
self._operation = None
@@ -48,14 +52,14 @@ class BlockCipher(object):
raise ValueError("BlockCipher cannot encrypt when the operation is"
" set to %s" % self._operation.name)
- return api.update_encrypt_context(self._ctx, plaintext)
+ return self._api.update_encrypt_context(self._ctx, plaintext)
def finalize(self):
if self._ctx is None:
raise ValueError("BlockCipher was already finalized")
if self._operation is _Operation.encrypt:
- result = api.finalize_encrypt_context(self._ctx)
+ result = self._api.finalize_encrypt_context(self._ctx)
else:
raise ValueError("BlockCipher cannot finalize the unknown "
"operation %s" % self._operation.name)