Python 3 standard library: hashlib password hash

Posted by JustGotAQuestion on Sat, 28 Mar 2020 11:28:43 +0100

1. hashlib password hash

The hashlib module defines an API to access different cryptographic hashing algorithms. To use a specific hash algorithm, create a hash object with the appropriate constructor function or new(). No matter which specific algorithm is used, these objects use the same API.

1.1 hash algorithm

Because hashlib has OpenSSL to provide "underlying support", all algorithms provided by OpenSSL library are available, including:

md5

sha1

sha224

sha256

sha384

sha512

Some algorithms are available on all platforms, while others rely on the underlying libraries. These two algorithms are provided by algorithms_guaranteed and algorithms_available respectively.

import hashlib

print('Guaranteed:\n{}\n'.format(
    ', '.join(sorted(hashlib.algorithms_guaranteed))))
print('Available:\n{}'.format(
    ', '.join(sorted(hashlib.algorithms_available))))
Guaranteed:
blake2b, blake2s, md5, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, shake_128, shake_256

Available:
DSA, DSA-SHA, MD4, MD5, RIPEMD160, SHA, SHA1, SHA224, SHA256, SHA384, SHA512, blake2b, blake2s, dsaEncryption, dsaWithSHA, ecdsa-with-SHA1, md4, md5, ripemd160, sha, sha1, sha224, sha256, sha384, sha3_224, sha3_256, sha3_384, sha3_512, sha512, shake_128, shake_256, whirlpool

1.2 MD5 example

To calculate a MD5 hash or digest for a data block (here is a Unicode string that is converted to a byte string), first we need to create a hash object, then add data, and finally call digest() or hexdigest().

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.'''

h = hashlib.md5()
h.update(lorem.encode('utf-8'))
print(h.hexdigest())

This example uses the hexdigest() method instead of digest () because the output is formatted for clear printing. If you can accept binary summary values, you can use digest().

 

1.3 SHA1 example

The SHA1 summary is calculated in the same way.  

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.'''

h = hashlib.sha1()
h.update(lorem.encode('utf-8'))
print(h.hexdigest())

The summary values in this example are different because the MD5 and SHA1 algorithms are different.

 

1.4 incremental update

The hash calculator's update() method can be called repeatedly. Each time it is called, the summary is updated based on the additional text provided. Incremental updates are more efficient than reading the entire file into memory and produce the same results.

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.'''

h = hashlib.md5()
h.update(lorem.encode('utf-8'))
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
    return

h = hashlib.md5()
for chunk in chunkize(64, lorem.encode('utf-8')):
    h.update(chunk)
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 shows how to incrementally update a summary when reading or generating data.

Topics: Python SHA1 OpenSSL calculator