From 601278a17617e5aa631d6395320340f4df3641b2 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 12 Feb 2015 12:51:00 -0600 Subject: move hash interfaces --- .../hazmat/backends/commoncrypto/hashes.py | 4 +- .../hazmat/backends/commoncrypto/hmac.py | 4 +- src/cryptography/hazmat/backends/openssl/hashes.py | 4 +- src/cryptography/hazmat/backends/openssl/hmac.py | 4 +- .../hazmat/primitives/asymmetric/padding.py | 10 +-- src/cryptography/hazmat/primitives/hashes.py | 75 ++++++++++++++++++---- src/cryptography/hazmat/primitives/hmac.py | 8 +-- .../hazmat/primitives/interfaces/__init__.py | 63 ++++++------------ 8 files changed, 99 insertions(+), 73 deletions(-) (limited to 'src') diff --git a/src/cryptography/hazmat/backends/commoncrypto/hashes.py b/src/cryptography/hazmat/backends/commoncrypto/hashes.py index 8ce20f6b..a54e9833 100644 --- a/src/cryptography/hazmat/backends/commoncrypto/hashes.py +++ b/src/cryptography/hazmat/backends/commoncrypto/hashes.py @@ -6,10 +6,10 @@ from __future__ import absolute_import, division, print_function from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm, _Reasons -from cryptography.hazmat.primitives import interfaces +from cryptography.hazmat.primitives import hashes -@utils.register_interface(interfaces.HashContext) +@utils.register_interface(hashes.HashContext) class _HashContext(object): def __init__(self, backend, algorithm, ctx=None): self._algorithm = algorithm diff --git a/src/cryptography/hazmat/backends/commoncrypto/hmac.py b/src/cryptography/hazmat/backends/commoncrypto/hmac.py index 942688e6..ae623d84 100644 --- a/src/cryptography/hazmat/backends/commoncrypto/hmac.py +++ b/src/cryptography/hazmat/backends/commoncrypto/hmac.py @@ -8,11 +8,11 @@ from cryptography import utils from cryptography.exceptions import ( InvalidSignature, UnsupportedAlgorithm, _Reasons ) -from cryptography.hazmat.primitives import constant_time, interfaces +from cryptography.hazmat.primitives import constant_time, hashes, interfaces @utils.register_interface(interfaces.MACContext) -@utils.register_interface(interfaces.HashContext) +@utils.register_interface(hashes.HashContext) class _HMACContext(object): def __init__(self, backend, key, algorithm, ctx=None): self._algorithm = algorithm diff --git a/src/cryptography/hazmat/backends/openssl/hashes.py b/src/cryptography/hazmat/backends/openssl/hashes.py index 8dc3c01f..2c1702f8 100644 --- a/src/cryptography/hazmat/backends/openssl/hashes.py +++ b/src/cryptography/hazmat/backends/openssl/hashes.py @@ -7,10 +7,10 @@ from __future__ import absolute_import, division, print_function from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm, _Reasons -from cryptography.hazmat.primitives import interfaces +from cryptography.hazmat.primitives import hashes -@utils.register_interface(interfaces.HashContext) +@utils.register_interface(hashes.HashContext) class _HashContext(object): def __init__(self, backend, algorithm, ctx=None): self._algorithm = algorithm diff --git a/src/cryptography/hazmat/backends/openssl/hmac.py b/src/cryptography/hazmat/backends/openssl/hmac.py index 356d2aa8..dfe9d93b 100644 --- a/src/cryptography/hazmat/backends/openssl/hmac.py +++ b/src/cryptography/hazmat/backends/openssl/hmac.py @@ -9,11 +9,11 @@ from cryptography import utils from cryptography.exceptions import ( InvalidSignature, UnsupportedAlgorithm, _Reasons ) -from cryptography.hazmat.primitives import constant_time, interfaces +from cryptography.hazmat.primitives import constant_time, hashes, interfaces @utils.register_interface(interfaces.MACContext) -@utils.register_interface(interfaces.HashContext) +@utils.register_interface(hashes.HashContext) class _HMACContext(object): def __init__(self, backend, key, algorithm, ctx=None): self._algorithm = algorithm diff --git a/src/cryptography/hazmat/primitives/asymmetric/padding.py b/src/cryptography/hazmat/primitives/asymmetric/padding.py index f51b201d..d0c3eade 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/padding.py +++ b/src/cryptography/hazmat/primitives/asymmetric/padding.py @@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function import six from cryptography import utils -from cryptography.hazmat.primitives import interfaces +from cryptography.hazmat.primitives import hashes, interfaces @utils.register_interface(interfaces.AsymmetricPadding) @@ -38,8 +38,8 @@ class OAEP(object): name = "EME-OAEP" def __init__(self, mgf, algorithm, label): - if not isinstance(algorithm, interfaces.HashAlgorithm): - raise TypeError("Expected instance of interfaces.HashAlgorithm.") + if not isinstance(algorithm, hashes.HashAlgorithm): + raise TypeError("Expected instance of hashes.HashAlgorithm.") self._mgf = mgf self._algorithm = algorithm @@ -50,7 +50,7 @@ class MGF1(object): MAX_LENGTH = object() def __init__(self, algorithm): - if not isinstance(algorithm, interfaces.HashAlgorithm): - raise TypeError("Expected instance of interfaces.HashAlgorithm.") + if not isinstance(algorithm, hashes.HashAlgorithm): + raise TypeError("Expected instance of hashes.HashAlgorithm.") self._algorithm = algorithm diff --git a/src/cryptography/hazmat/primitives/hashes.py b/src/cryptography/hazmat/primitives/hashes.py index f00c0741..6bc8500b 100644 --- a/src/cryptography/hazmat/primitives/hashes.py +++ b/src/cryptography/hazmat/primitives/hashes.py @@ -4,15 +4,66 @@ from __future__ import absolute_import, division, print_function +import abc + +import six + from cryptography import utils from cryptography.exceptions import ( AlreadyFinalized, UnsupportedAlgorithm, _Reasons ) from cryptography.hazmat.backends.interfaces import HashBackend -from cryptography.hazmat.primitives import interfaces -@utils.register_interface(interfaces.HashContext) +@six.add_metaclass(abc.ABCMeta) +class HashAlgorithm(object): + @abc.abstractproperty + def name(self): + """ + A string naming this algorithm (e.g. "sha256", "md5"). + """ + + @abc.abstractproperty + def digest_size(self): + """ + The size of the resulting digest in bytes. + """ + + @abc.abstractproperty + def block_size(self): + """ + The internal block size of the hash algorithm in bytes. + """ + + +@six.add_metaclass(abc.ABCMeta) +class HashContext(object): + @abc.abstractproperty + def algorithm(self): + """ + A HashAlgorithm that will be used by this context. + """ + + @abc.abstractmethod + def update(self, data): + """ + Processes the provided bytes through the hash. + """ + + @abc.abstractmethod + def finalize(self): + """ + Finalizes the hash context and returns the hash digest as bytes. + """ + + @abc.abstractmethod + def copy(self): + """ + Return a HashContext that is a copy of the current context. + """ + + +@utils.register_interface(HashContext) class Hash(object): def __init__(self, algorithm, backend, ctx=None): if not isinstance(backend, HashBackend): @@ -21,8 +72,8 @@ class Hash(object): _Reasons.BACKEND_MISSING_INTERFACE ) - if not isinstance(algorithm, interfaces.HashAlgorithm): - raise TypeError("Expected instance of interfaces.HashAlgorithm.") + if not isinstance(algorithm, HashAlgorithm): + raise TypeError("Expected instance of hashes.HashAlgorithm.") self._algorithm = algorithm self._backend = backend @@ -56,56 +107,56 @@ class Hash(object): return digest -@utils.register_interface(interfaces.HashAlgorithm) +@utils.register_interface(HashAlgorithm) class SHA1(object): name = "sha1" digest_size = 20 block_size = 64 -@utils.register_interface(interfaces.HashAlgorithm) +@utils.register_interface(HashAlgorithm) class SHA224(object): name = "sha224" digest_size = 28 block_size = 64 -@utils.register_interface(interfaces.HashAlgorithm) +@utils.register_interface(HashAlgorithm) class SHA256(object): name = "sha256" digest_size = 32 block_size = 64 -@utils.register_interface(interfaces.HashAlgorithm) +@utils.register_interface(HashAlgorithm) class SHA384(object): name = "sha384" digest_size = 48 block_size = 128 -@utils.register_interface(interfaces.HashAlgorithm) +@utils.register_interface(HashAlgorithm) class SHA512(object): name = "sha512" digest_size = 64 block_size = 128 -@utils.register_interface(interfaces.HashAlgorithm) +@utils.register_interface(HashAlgorithm) class RIPEMD160(object): name = "ripemd160" digest_size = 20 block_size = 64 -@utils.register_interface(interfaces.HashAlgorithm) +@utils.register_interface(HashAlgorithm) class Whirlpool(object): name = "whirlpool" digest_size = 64 block_size = 64 -@utils.register_interface(interfaces.HashAlgorithm) +@utils.register_interface(HashAlgorithm) class MD5(object): name = "md5" digest_size = 16 diff --git a/src/cryptography/hazmat/primitives/hmac.py b/src/cryptography/hazmat/primitives/hmac.py index 17f1084d..15b9ee6e 100644 --- a/src/cryptography/hazmat/primitives/hmac.py +++ b/src/cryptography/hazmat/primitives/hmac.py @@ -9,11 +9,11 @@ from cryptography.exceptions import ( AlreadyFinalized, UnsupportedAlgorithm, _Reasons ) from cryptography.hazmat.backends.interfaces import HMACBackend -from cryptography.hazmat.primitives import interfaces +from cryptography.hazmat.primitives import hashes, interfaces @utils.register_interface(interfaces.MACContext) -@utils.register_interface(interfaces.HashContext) +@utils.register_interface(hashes.HashContext) class HMAC(object): def __init__(self, key, algorithm, backend, ctx=None): if not isinstance(backend, HMACBackend): @@ -22,8 +22,8 @@ class HMAC(object): _Reasons.BACKEND_MISSING_INTERFACE ) - if not isinstance(algorithm, interfaces.HashAlgorithm): - raise TypeError("Expected instance of interfaces.HashAlgorithm.") + if not isinstance(algorithm, hashes.HashAlgorithm): + raise TypeError("Expected instance of hashes.HashAlgorithm.") self._algorithm = algorithm self._backend = backend diff --git a/src/cryptography/hazmat/primitives/interfaces/__init__.py b/src/cryptography/hazmat/primitives/interfaces/__init__.py index a2154df5..17bac1e6 100644 --- a/src/cryptography/hazmat/primitives/interfaces/__init__.py +++ b/src/cryptography/hazmat/primitives/interfaces/__init__.py @@ -9,6 +9,7 @@ import abc import six from cryptography import utils +from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa from cryptography.hazmat.primitives.interfaces.ciphers import ( BlockCipherAlgorithm, CipherAlgorithm, Mode, @@ -202,52 +203,26 @@ class PaddingContext(object): """ -@six.add_metaclass(abc.ABCMeta) -class HashAlgorithm(object): - @abc.abstractproperty - def name(self): - """ - A string naming this algorithm (e.g. "sha256", "md5"). - """ - - @abc.abstractproperty - def digest_size(self): - """ - The size of the resulting digest in bytes. - """ - - @abc.abstractproperty - def block_size(self): - """ - The internal block size of the hash algorithm in bytes. - """ - - -@six.add_metaclass(abc.ABCMeta) -class HashContext(object): - @abc.abstractproperty - def algorithm(self): - """ - A HashAlgorithm that will be used by this context. - """ - - @abc.abstractmethod - def update(self, data): - """ - Processes the provided bytes through the hash. - """ +HashContext = utils.deprecated( + hashes.HashContext, + __name__, + ( + "The HashContext interface has moved to the " + "cryptography.hazmat.primitives.hashes module" + ), + utils.DeprecatedIn08 +) - @abc.abstractmethod - def finalize(self): - """ - Finalizes the hash context and returns the hash digest as bytes. - """ - @abc.abstractmethod - def copy(self): - """ - Return a HashContext that is a copy of the current context. - """ +HashAlgorithm = utils.deprecated( + hashes.HashAlgorithm, + __name__, + ( + "The HashAlgorithm interface has moved to the " + "cryptography.hazmat.primitives.hashes module" + ), + utils.DeprecatedIn08 +) RSAPrivateKey = utils.deprecated( -- cgit v1.2.3