hashlib – Cryptographic hashes and message digests

Purpose:Cryptographic hashes and message digests
Available In:2.5

The hashlib module deprecates the separate md5 and sha modules and makes their API consistent. To work with a specific hash algorithm, use the appropriate constructor function to create a hash object. Then you can use the same API to interact with the hash no matter what algorithm is being used.

Since hashlib is “backed” by OpenSSL, all of of the algorithms provided by that library are available, including:

  • md5
  • sha1
  • sha224
  • sha256
  • sha384
  • sha512

Sample Data

All of the examples below use the same sample data:

import hashlib

lorem = '''Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.'''

MD5 Example

To calculate the MD5 digest for a block of data (here an ASCII string), create the hash object, add the data, and compute the digest.

import hashlib

from hashlib_data import lorem

h = hashlib.md5()
print h.hexdigest()

This example uses the hexdigest() method instead of digest() because the output is formatted to be printed. If a binary digest value is acceptable, you can use digest().

$ python hashlib_md5.py


SHA1 Example

A SHA1 digest for the same data would be calculated in much the same way.

import hashlib

from hashlib_data import lorem

h = hashlib.sha1()
print h.hexdigest()

The digest value is different in this example because we changed the algorithm from MD5 to SHA1

$ python hashlib_sha1.py



Sometimes it is more convenient to refer to the algorithm by name in a string rather than by using the constructor function directly. It is useful, for example, to be able to store the hash type in a configuration file. In those cases, use new() to create a hash calculator.

import hashlib
import sys

    hash_name = sys.argv[1]
except IndexError:
    print 'Specify the hash name as the first argument.'
        data = sys.argv[2]
    except IndexError:    
        from hashlib_data import lorem as data
    h = hashlib.new(hash_name)
    print h.hexdigest()

When run with a variety of arguments:

$ python hashlib_new.py sha1


$ python hashlib_new.py sha256


$ python hashlib_new.py sha512


$ python hashlib_new.py md5


Calling update() more than once

The update() method of the hash calculators can be called repeatedly. Each time, the digest is updated based on the additional text fed in. This can be much more efficient than reading an entire file into memory, for example.

import hashlib

from hashlib_data import lorem

h = hashlib.md5()
all_at_once = h.hexdigest()

def chunkize(size, text):
    "Return parts of the text in size-based increments."
    start = 0
    while start < len(text):
        chunk = text[start:start+size]
        yield chunk
        start += size

h = hashlib.md5()
for chunk in chunkize(64, lorem):
line_by_line = h.hexdigest()

print 'All at once :', all_at_once
print 'Line by line:', line_by_line
print 'Same        :', (all_at_once == line_by_line)

This example is a little contrived because it works with such a small amount of text, but it illustrates how you could incrementally update a digest as data is read or otherwise produced.

$ python hashlib_update.py

All at once : c3abe541f361b1bfbbcfecbf53aad1fb
Line by line: c3abe541f361b1bfbbcfecbf53aad1fb
Same        : True

See also

The standard library documentation for this module.
Voidspace: IronPython and hashlib
A wrapper for hashlib that works with IronPython.
The hmac module.