2 Crypto.Hash: Hash Functions

Hash functions take arbitrary strings as input, and produce an output of fixed size that is dependent on the input; it should never be possible to derive the input data given only the hash function's output. One simple hash function consists of simply adding together all the bytes of the input, and taking the result modulo 256. For a hash function to be cryptographically secure, it must be very difficult to find two messages with the same hash value, or to find a message with a given hash value. The simple additive hash function fails this criterion miserably and the hash functions described below meet this criterion (as far as we know). Examples of cryptographically secure hash functions include MD2, MD5, and SHA1.

Hash functions can be used simply as a checksum, or, in association with a public-key algorithm, can be used to implement digital signatures.

The hashing algorithms currently implemented are:

Hash function  Digest length 
MD2 128 bits
MD4 128 bits
MD5 128 bits
RIPEMD 160 bits
SHA1 160 bits
SHA256 256 bits

All hashing modules share the same interface. After importing a given hashing module, call the new() function to create a new hashing object. You can now feed arbitrary strings into the object with the update() method, and can ask for the hash value at any time by calling the digest() or hexdigest() methods. The new() function can also be passed an optional string parameter that will be immediately hashed into the object's state.

Hash function modules define one variable:

digest_size
An integer value; the size of the digest produced by the hashing objects. You could also obtain this value by creating a sample object, and taking the length of the digest string it returns, but using digest_size is faster.

The methods for hashing objects are always the following:

copy()
Return a separate copy of this hashing object. An update to this copy won't affect the original object.

digest()
Return the hash value of this hashing object, as a string containing 8-bit data. The object is not altered in any way by this function; you can continue updating the object after calling this function.

hexdigest()
Return the hash value of this hashing object, as a string containing the digest data as hexadecimal digits. The resulting string will be twice as long as that returned by digest(). The object is not altered in any way by this function; you can continue updating the object after calling this function.

update(arg)
Update this hashing object with the string arg.

Here's an example, using the MD5 algorithm:

>>> from Crypto.Hash import MD5
>>> m = MD5.new()
>>> m.update('abc')
>>> m.digest()
'\x90\x01P\x98<\xd2O\xb0\xd6\x96?}(\xe1\x7fr'
>>> m.hexdigest()
'900150983cd24fb0d6963f7d28e17f72'


Subsections