Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python hashlib Module

Python Hashlib Featured

The Python hashlib module is an interface for hashing messages easily. This contains numerous methods which will handle hashing any raw message in an encrypted format.

The core purpose of this module is to use a hash function on a string, and encrypt it so that it is very difficult to decrypt it.

Typically, an encrypted string is long enough such that it is almost impossible to get back the original string.

The below block diagram shows how we can achieve this to store passwords securely, in our application!

Hash Function
Hash Function

Notice that the hash function ensures that the length of the hashed output remains the same, and much larger than the length of the input string. So, this makes it very difficult to crack such hashes using brute force.

Having covered the concept, let’s now look at using this module!


Python hashlib

We need to import this module, which comes directly with the Python installation.

import hashlib

There are different hashing algorithms available, by which we can encrypt our string. Let’s look at what are the available ones that we can use.

import hashlib

print(hashlib.algorithms_available)
print(hashlib.algorithms_guaranteed)

The algorithms_available attribute prints every algorithm used in the system, which includes other programs such as ssh and OpenSSL.

The algorithms_guaranteed attribute lists all the algorithms in the module.

So for me (Linux system), my output looks like this. Your output may vary slightly if you are in Windows/macOS.

{'whirlpool', 'sha3-224', 'sha3-256', 'sha512-224', 'sha3-384', 'sha384', 'shake256', 'sha1', 'md5-sha1', 'md5', 'md4', 'mdc2', 'blake2b512', 'blake2s256', 'sha3_512', 'sha512-256', 'blake2s', 'ripemd160', 'sha3_384', 'shake128', 'shake_128', 'blake2b', 'sha512', 'sha3_224', 'shake_256', 'sha256', 'sha3_256', 'sha3-512', 'sha224', 'sm3'}
{'blake2b', 'md5', 'sha512', 'blake2s', 'sha3_224', 'shake_256', 'sha256', 'sha3_256', 'sha384', 'sha3_384', 'sha224', 'sha1', 'shake_128', 'sha3_512'}

Let’s now move on to Encrypting strings!


Encrypting Strings using Python hashlib

We cannot directly pass a string into the hash function. This is because it only accepts a list of bytes, so we must convert our string into a byte string, and then only pass into a hash function.

For this article, we’ll be using the SHA-256 Algorithm, which gives a hash of 256 bits.

import hashlib
# Initialize the empty message using SHA-256
message = hashlib.sha256()

# Update the message using byte strings
message.update(b'Hello there,')
message.update(b'how are you?')

# Print the message digest
print(message.digest())

Output

b',\xe1\xff7\xe7\xa0\xe7\xb2\xb4\xf9E!\xf2\xd9\xb8;\xdf\x7f\x10\xa8\x1ad1\xc0\x7f=\xbb\xb1\xf7\xeb7\xcf'

We use message.digest() to get the hash string from the message object.

The message.update() method does NOT work for nonbyte strings, so you need to encode it using string.encode(), if you are not passing it directly.

import hashlib

def hash_string(input):
    byte_input = input.encode()
    hash_object = hashlib.sha256(byte_input)
    return hash_object

def update_hash(hash_object, input_str):
    # Adds the string to the hash string
    hash_object.update(input_str.encode())

hash_object = hash_string('Hello from AskPython')

# Print the encrypted message
print(hash_object.hexdigest())

# Update the hash message
update_hash(hash_object, 'hello, how are you?')

# Print the encrypted message
print('After updating:', hash_object.hexdigest())

new_str = 'Hello from AskPythonhello, how are you?'
print(hash_string(new_str).hexdigest() == hash_object.hexdigest())

Output

e0a59a3889b1db4cc27f4fcc03353bf1db55114add1112f95532794fd392b202
After updating: 32f9cffab10b7bfef72892d5059de865914319a05bdf394b18d7b068fd806a0a
True

We’ve successfully passed our string into the hash function to get an encrypted hash, and also update it accordingly!

You can now extend this to different hash algorithms, which are given in the Documentation. In the near future, we’ll show you a step by step procedure to implement a good hash function, using this module as a building block. Tune in for more updates!


Conclusion

In this article, we learned about the Python hashlib module, for encrypting strings using different hash functions.

References