aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Reid <dreid@dreid.org>2013-11-11 11:48:39 -0800
committerDavid Reid <dreid@dreid.org>2013-11-18 09:57:18 -0800
commitd706515791ad42a527b39e899b89e4f669dff430 (patch)
treee1ecbca88057d82ac7e898483139c5587ba39ec0
parent729e62b0e19c2278da762af5f3fcc4e083747497 (diff)
downloadcryptography-d706515791ad42a527b39e899b89e4f669dff430.tar.gz
cryptography-d706515791ad42a527b39e899b89e4f669dff430.tar.bz2
cryptography-d706515791ad42a527b39e899b89e4f669dff430.zip
Move register so it can be used by cryptography.hazmat.bindings.interfaces.
-rw-r--r--cryptography/hazmat/bindings/interfaces.py57
-rw-r--r--cryptography/hazmat/bindings/openssl/backend.py10
-rw-r--r--cryptography/hazmat/primitives/ciphers/base.py3
-rw-r--r--cryptography/hazmat/primitives/ciphers/modes.py19
-rw-r--r--cryptography/hazmat/primitives/hashes.py19
-rw-r--r--cryptography/hazmat/primitives/hmac.py3
-rw-r--r--cryptography/hazmat/primitives/interfaces.py7
-rw-r--r--cryptography/hazmat/primitives/padding.py5
-rw-r--r--cryptography/utils.py21
9 files changed, 114 insertions, 30 deletions
diff --git a/cryptography/hazmat/bindings/interfaces.py b/cryptography/hazmat/bindings/interfaces.py
new file mode 100644
index 00000000..e9c8fef3
--- /dev/null
+++ b/cryptography/hazmat/bindings/interfaces.py
@@ -0,0 +1,57 @@
+# 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 __future__ import absolute_import, division, print_function
+
+import abc
+
+import six
+
+
+class CiphersProviderBackend(six.with_metaclass(abc.ABCMeta)):
+ @abc.abstractproperty
+ def ciphers(self):
+ """
+ """
+
+
+class CiphersProvider(six.with_metaclass(abc.ABCMeta)):
+ """
+ wat
+ """
+
+
+class HashesProviderBackend(six.with_metaclass(abc.ABCMeta)):
+ @abc.abstractproperty
+ def hashes(self):
+ """
+ """
+
+
+class HashesProvider(six.with_metaclass(abc.ABCMeta)):
+ """
+ wat
+ """
+
+
+class HMACsProviderBackend(six.with_metaclass(abc.ABCMeta)):
+ @abc.abstractproperty
+ def hmacs(self):
+ """
+ """
+
+
+class HMACsProvider(six.with_metaclass(abc.ABCMeta)):
+ """
+ wat
+ """
diff --git a/cryptography/hazmat/bindings/openssl/backend.py b/cryptography/hazmat/bindings/openssl/backend.py
index 844e175f..a7c0741c 100644
--- a/cryptography/hazmat/bindings/openssl/backend.py
+++ b/cryptography/hazmat/bindings/openssl/backend.py
@@ -18,7 +18,12 @@ import sys
import cffi
+from cryptography import utils
from cryptography.exceptions import UnsupportedAlgorithm
+from cryptography.hazmat.bindings.interfaces import (
+ CiphersProviderBackend, CiphersProvider, HashesProviderBackend,
+ HashesProvider, HMACsProviderBackend, HMACsProvider
+)
from cryptography.hazmat.primitives import interfaces
from cryptography.hazmat.primitives.ciphers.algorithms import (
AES, Blowfish, Camellia, CAST5, TripleDES, ARC4,
@@ -28,6 +33,9 @@ from cryptography.hazmat.primitives.ciphers.modes import (
)
+@utils.register_interface(CiphersProviderBackend)
+@utils.register_interface(HashesProviderBackend)
+@utils.register_interface(HMACsProviderBackend)
class Backend(object):
"""
OpenSSL API wrapper.
@@ -196,7 +204,7 @@ class GetCipherByName(object):
return backend.lib.EVP_get_cipherbyname(cipher_name.encode("ascii"))
-@interfaces.register(interfaces.CipherContext)
+@utils.register_interface(interfaces.CipherContext)
class _CipherContext(object):
_ENCRYPT = 1
_DECRYPT = 0
diff --git a/cryptography/hazmat/primitives/ciphers/base.py b/cryptography/hazmat/primitives/ciphers/base.py
index 78bf7e0c..3d733afc 100644
--- a/cryptography/hazmat/primitives/ciphers/base.py
+++ b/cryptography/hazmat/primitives/ciphers/base.py
@@ -13,6 +13,7 @@
from __future__ import absolute_import, division, print_function
+from cryptography import utils
from cryptography.exceptions import AlreadyFinalized
from cryptography.hazmat.primitives import interfaces
@@ -42,7 +43,7 @@ class Cipher(object):
))
-@interfaces.register(interfaces.CipherContext)
+@utils.register_interface(interfaces.CipherContext)
class _CipherContext(object):
def __init__(self, ctx):
self._ctx = ctx
diff --git a/cryptography/hazmat/primitives/ciphers/modes.py b/cryptography/hazmat/primitives/ciphers/modes.py
index 915fd83d..1d0de689 100644
--- a/cryptography/hazmat/primitives/ciphers/modes.py
+++ b/cryptography/hazmat/primitives/ciphers/modes.py
@@ -13,11 +13,12 @@
from __future__ import absolute_import, division, print_function
+from cryptography import utils
from cryptography.hazmat.primitives import interfaces
-@interfaces.register(interfaces.Mode)
-@interfaces.register(interfaces.ModeWithInitializationVector)
+@utils.register_interface(interfaces.Mode)
+@utils.register_interface(interfaces.ModeWithInitializationVector)
class CBC(object):
name = "CBC"
@@ -25,13 +26,13 @@ class CBC(object):
self.initialization_vector = initialization_vector
-@interfaces.register(interfaces.Mode)
+@utils.register_interface(interfaces.Mode)
class ECB(object):
name = "ECB"
-@interfaces.register(interfaces.Mode)
-@interfaces.register(interfaces.ModeWithInitializationVector)
+@utils.register_interface(interfaces.Mode)
+@utils.register_interface(interfaces.ModeWithInitializationVector)
class OFB(object):
name = "OFB"
@@ -39,8 +40,8 @@ class OFB(object):
self.initialization_vector = initialization_vector
-@interfaces.register(interfaces.Mode)
-@interfaces.register(interfaces.ModeWithInitializationVector)
+@utils.register_interface(interfaces.Mode)
+@utils.register_interface(interfaces.ModeWithInitializationVector)
class CFB(object):
name = "CFB"
@@ -48,8 +49,8 @@ class CFB(object):
self.initialization_vector = initialization_vector
-@interfaces.register(interfaces.Mode)
-@interfaces.register(interfaces.ModeWithNonce)
+@utils.register_interface(interfaces.Mode)
+@utils.register_interface(interfaces.ModeWithNonce)
class CTR(object):
name = "CTR"
diff --git a/cryptography/hazmat/primitives/hashes.py b/cryptography/hazmat/primitives/hashes.py
index 86c9fe2e..93fc8c42 100644
--- a/cryptography/hazmat/primitives/hashes.py
+++ b/cryptography/hazmat/primitives/hashes.py
@@ -15,11 +15,12 @@ from __future__ import absolute_import, division, print_function
import six
+from cryptography import utils
from cryptography.exceptions import AlreadyFinalized
from cryptography.hazmat.primitives import interfaces
-@interfaces.register(interfaces.HashContext)
+@utils.register_interface(interfaces.HashContext)
class Hash(object):
def __init__(self, algorithm, backend=None, ctx=None):
if not isinstance(algorithm, interfaces.HashAlgorithm):
@@ -59,56 +60,56 @@ class Hash(object):
return digest
-@interfaces.register(interfaces.HashAlgorithm)
+@utils.register_interface(interfaces.HashAlgorithm)
class SHA1(object):
name = "sha1"
digest_size = 20
block_size = 64
-@interfaces.register(interfaces.HashAlgorithm)
+@utils.register_interface(interfaces.HashAlgorithm)
class SHA224(object):
name = "sha224"
digest_size = 28
block_size = 64
-@interfaces.register(interfaces.HashAlgorithm)
+@utils.register_interface(interfaces.HashAlgorithm)
class SHA256(object):
name = "sha256"
digest_size = 32
block_size = 64
-@interfaces.register(interfaces.HashAlgorithm)
+@utils.register_interface(interfaces.HashAlgorithm)
class SHA384(object):
name = "sha384"
digest_size = 48
block_size = 128
-@interfaces.register(interfaces.HashAlgorithm)
+@utils.register_interface(interfaces.HashAlgorithm)
class SHA512(object):
name = "sha512"
digest_size = 64
block_size = 128
-@interfaces.register(interfaces.HashAlgorithm)
+@utils.register_interface(interfaces.HashAlgorithm)
class RIPEMD160(object):
name = "ripemd160"
digest_size = 20
block_size = 64
-@interfaces.register(interfaces.HashAlgorithm)
+@utils.register_interface(interfaces.HashAlgorithm)
class Whirlpool(object):
name = "whirlpool"
digest_size = 64
block_size = 64
-@interfaces.register(interfaces.HashAlgorithm)
+@utils.register_interface(interfaces.HashAlgorithm)
class MD5(object):
name = "md5"
digest_size = 16
diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py
index 1bbe39c7..08dfae01 100644
--- a/cryptography/hazmat/primitives/hmac.py
+++ b/cryptography/hazmat/primitives/hmac.py
@@ -15,11 +15,12 @@ from __future__ import absolute_import, division, print_function
import six
+from cryptography import utils
from cryptography.exceptions import AlreadyFinalized
from cryptography.hazmat.primitives import interfaces
-@interfaces.register(interfaces.HashContext)
+@utils.register_interface(interfaces.HashContext)
class HMAC(object):
def __init__(self, key, algorithm, ctx=None, backend=None):
if not isinstance(algorithm, interfaces.HashAlgorithm):
diff --git a/cryptography/hazmat/primitives/interfaces.py b/cryptography/hazmat/primitives/interfaces.py
index bbbb266c..8cc9d42c 100644
--- a/cryptography/hazmat/primitives/interfaces.py
+++ b/cryptography/hazmat/primitives/interfaces.py
@@ -18,13 +18,6 @@ import abc
import six
-def register(iface):
- def register_decorator(klass):
- iface.register(klass)
- return klass
- return register_decorator
-
-
class CipherAlgorithm(six.with_metaclass(abc.ABCMeta)):
@abc.abstractproperty
def name(self):
diff --git a/cryptography/hazmat/primitives/padding.py b/cryptography/hazmat/primitives/padding.py
index f41c62c3..2dbac752 100644
--- a/cryptography/hazmat/primitives/padding.py
+++ b/cryptography/hazmat/primitives/padding.py
@@ -13,6 +13,7 @@
import six
+from cryptography import utils
from cryptography.hazmat.primitives import interfaces
@@ -33,7 +34,7 @@ class PKCS7(object):
return _PKCS7UnpaddingContext(self.block_size)
-@interfaces.register(interfaces.PaddingContext)
+@utils.register_interface(interfaces.PaddingContext)
class _PKCS7PaddingContext(object):
def __init__(self, block_size):
self.block_size = block_size
@@ -67,7 +68,7 @@ class _PKCS7PaddingContext(object):
return result
-@interfaces.register(interfaces.PaddingContext)
+@utils.register_interface(interfaces.PaddingContext)
class _PKCS7UnpaddingContext(object):
def __init__(self, block_size):
self.block_size = block_size
diff --git a/cryptography/utils.py b/cryptography/utils.py
new file mode 100644
index 00000000..e697d515
--- /dev/null
+++ b/cryptography/utils.py
@@ -0,0 +1,21 @@
+# 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 __future__ import absolute_import, division, print_function
+
+
+def register_interface(iface):
+ def register_decorator(klass):
+ iface.register(klass)
+ return klass
+ return register_decorator