diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2013-11-01 13:52:44 -0700 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2013-11-01 13:52:44 -0700 |
commit | ce46b89eed2d3a35043910c62f711690cb69c393 (patch) | |
tree | d882be5299a64554c7e64644d5c40560c3e72c67 /cryptography | |
parent | c94cdbca7f48095ddbaf15612c7b119b3ca26f73 (diff) | |
parent | 5560298b42f071db0bf7ea46c63ff52603370b38 (diff) | |
download | cryptography-ce46b89eed2d3a35043910c62f711690cb69c393.tar.gz cryptography-ce46b89eed2d3a35043910c62f711690cb69c393.tar.bz2 cryptography-ce46b89eed2d3a35043910c62f711690cb69c393.zip |
Merge pull request #200 from dreid/primitive-hashes
[WIP] Reduce the surface of the primitive hash interface.
Diffstat (limited to 'cryptography')
-rw-r--r-- | cryptography/hazmat/primitives/hashes.py | 59 | ||||
-rw-r--r-- | cryptography/hazmat/primitives/interfaces.py | 46 |
2 files changed, 78 insertions, 27 deletions
diff --git a/cryptography/hazmat/primitives/hashes.py b/cryptography/hazmat/primitives/hashes.py index 3ccb59d1..bdad5e16 100644 --- a/cryptography/hazmat/primitives/hashes.py +++ b/cryptography/hazmat/primitives/hashes.py @@ -13,89 +13,94 @@ from __future__ import absolute_import, division, print_function -import abc +import six -import binascii +from cryptography.hazmat.primitives import interfaces -import six +@interfaces.register(interfaces.HashContext) +class Hash(object): + def __init__(self, algorithm, backend=None, ctx=None): + if not isinstance(algorithm, interfaces.HashAlgorithm): + raise TypeError("Expected instance of interfaces.HashAlgorithm.") + self.algorithm = algorithm -class BaseHash(six.with_metaclass(abc.ABCMeta)): - def __init__(self, data=None, backend=None, ctx=None): if backend is None: from cryptography.hazmat.bindings import _default_backend backend = _default_backend + self._backend = backend + if ctx is None: - self._ctx = self._backend.hashes.create_ctx(self) + self._ctx = self._backend.hashes.create_ctx(self.algorithm) 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._backend.hashes.update_ctx(self._ctx, data) def copy(self): - return self.__class__(backend=self._backend, ctx=self._copy_ctx()) - - def digest(self): - return self._backend.hashes.finalize_ctx(self._copy_ctx(), - self.digest_size) - - def hexdigest(self): - return str(binascii.hexlify(self.digest()).decode("ascii")) + return self.__class__(self.algorithm, backend=self._backend, + ctx=self._backend.hashes.copy_ctx(self._ctx)) - def _copy_ctx(self): - return self._backend.hashes.copy_ctx(self._ctx) + def finalize(self): + return self._backend.hashes.finalize_ctx(self._ctx, + self.algorithm.digest_size) -class SHA1(BaseHash): +@interfaces.register(interfaces.HashAlgorithm) +class SHA1(object): name = "sha1" digest_size = 20 block_size = 64 -class SHA224(BaseHash): +@interfaces.register(interfaces.HashAlgorithm) +class SHA224(object): name = "sha224" digest_size = 28 block_size = 64 -class SHA256(BaseHash): +@interfaces.register(interfaces.HashAlgorithm) +class SHA256(object): name = "sha256" digest_size = 32 block_size = 64 -class SHA384(BaseHash): +@interfaces.register(interfaces.HashAlgorithm) +class SHA384(object): name = "sha384" digest_size = 48 block_size = 128 -class SHA512(BaseHash): +@interfaces.register(interfaces.HashAlgorithm) +class SHA512(object): name = "sha512" digest_size = 64 block_size = 128 -class RIPEMD160(BaseHash): +@interfaces.register(interfaces.HashAlgorithm) +class RIPEMD160(object): name = "ripemd160" digest_size = 20 block_size = 64 -class Whirlpool(BaseHash): +@interfaces.register(interfaces.HashAlgorithm) +class Whirlpool(object): name = "whirlpool" digest_size = 64 block_size = 64 -class MD5(BaseHash): +@interfaces.register(interfaces.HashAlgorithm) +class MD5(object): name = "md5" digest_size = 16 block_size = 64 diff --git a/cryptography/hazmat/primitives/interfaces.py b/cryptography/hazmat/primitives/interfaces.py index 217490fd..ebf5e31e 100644 --- a/cryptography/hazmat/primitives/interfaces.py +++ b/cryptography/hazmat/primitives/interfaces.py @@ -59,3 +59,49 @@ class PaddingContext(six.with_metaclass(abc.ABCMeta)): """ finalize return bytes """ + + +class HashAlgorithm(six.with_metaclass(abc.ABCMeta)): + @abc.abstractproperty + def name(self): + """ + A string naming this algorithm. (ex. 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. + """ + + +class HashContext(six.with_metaclass(abc.ABCMeta)): + @abc.abstractproperty + def algorithm(self): + """ + A HashAlgorithm that will be used by this context. + """ + + @abc.abstractmethod + def update(self, data): + """ + hash data as bytes + """ + + @abc.abstractmethod + def finalize(self): + """ + finalize this copy of the hash and return the digest as bytes. + """ + + @abc.abstractmethod + def copy(self): + """ + return a HashContext that is a copy of the current context. + """ |