Package Crypto :: Package Hash :: Module hashalgo :: Class HashAlgo
[frames] | no frames]

Class HashAlgo

Known Subclasses:

A generic class for an abstract cryptographic hash algorithm.
Instance Methods
 
__init__(self, hashFactory, data=None)
Initialize the hash object.
 
update(self, data)
Continue hashing of a message by consuming the next chunk of data.
 
digest(self)
Return the binary (non-printable) digest of the message that has been hashed so far.
 
hexdigest(self)
Return the printable digest of the message that has been hashed so far.
 
copy(self)
Return a copy ("clone") of the hash object.
 
new(self, data=None)
Return a fresh instance of the hash object.
Class Variables
  digest_size = None
The size of the resulting hash in bytes.
Method Details

__init__(self, hashFactory, data=None)
(Constructor)

 
Initialize the hash object.
Parameters:
  • hashFactory (callable) - An object that will generate the actual hash implementation. hashFactory must have a new() method, or must be directly callable.
  • data (byte string) - The very first chunk of the message to hash. It is equivalent to an early call to update().

update(self, data)

 

Continue hashing of a message by consuming the next chunk of data.

Repeated calls are equivalent to a single call with the concatenation of all the arguments. In other words:

>>> m.update(a); m.update(b)

is equivalent to:

>>> m.update(a+b)
Parameters:
  • data (byte string) - The next chunk of the message being hashed.

digest(self)

 

Return the binary (non-printable) digest of the message that has been hashed so far.

This method does not change the state of the hash object. You can continue updating the object after calling this function.

Returns:
A byte string of digest_size bytes. It may contain non-ASCII characters, including null bytes.

hexdigest(self)

 

Return the printable digest of the message that has been hashed so far.

This method does not change the state of the hash object.

Returns:
A string of 2* digest_size characters. It contains only hexadecimal ASCII digits.

copy(self)

 

Return a copy ("clone") of the hash object.

The copy will have the same internal state as the original hash object. This can be used to efficiently compute the digests of strings that share a common initial substring.

Returns:
A hash object of the same type

new(self, data=None)

 

Return a fresh instance of the hash object.

Unlike the copy method, the internal state of the object is empty.

Parameters:
  • data (byte string) - The next chunk of the message being hashed.
Returns:
A hash object of the same type