(original) (raw)

import abc as _abc class CryptoHash(metaclass=_abc.ABCMeta): """Abstract base class for cryptographic hash functions (PEP 247)""" __slots__ = () def __init__(self, string=None): # The string argument must be a bytes-like object if string is not None: self.update(string) @_abc.abstractproperty def block_size(self): """Internal block size of the hashing algorithm.""" raise NotImplementedError @_abc.abstractproperty def digest_size(self): """The size of the digest produced by the hashing objects.""" raise NotImplementedError @_abc.abstractproperty def name(self): """The name of the digest.""" raise NotImplementedError @_abc.abstractmethod def copy(self): """Return a separate copy of this hashing object.""" raise NotImplementedError @_abc.abstractmethod def digest(self): """Return the hash value as 8-bit data (bytes).""" raise NotImplementedError def hexdigest(self): """Return the hash value as string containing hexadecimal digits.""" return ''.join('{:02x}'.format(b) for b in self.digest()) @_abc.abstractmethod def update(self, string): """Hash 'string' into the current state of the hashing object.""" raise NotImplementedError