Using the hashlib Library in Python

The hashlib library in Python provides various hashing algorithms. Let's explore how to use them.

Encoding a Message

First, we need to encode our message into bytes using UTF-8 encoding:

import hashlib

# encode it to bytes using UTF-8 encoding
message = "Some text to hash".encode()

Using Different Hash Algorithms

MD5

MD5 is an older and less secure hashing algorithm. It's not recommended for use due to its vulnerability to collisions:

# hash with MD5 (not recommended)
print("MD5:", hashlib.md5(message).hexdigest())
Output: MD5: 3eecc85e6440899b28a9ea6d8369f01c
Note: MD5 is pretty obsolete now, and you should never use it, as it isn't collision-resistant.

SHA-2

SHA-2 includes SHA-256 and SHA-512, which are more secure and commonly used:

# hash with SHA-2 (SHA-256 & SHA-512)
print("SHA-256:", hashlib.sha256(message).hexdigest())
print("SHA-512:", hashlib.sha512(message).hexdigest())
Output:
SHA-256: 7a86e0e93e6aa6cf49f19368ca7242e24640a988ac8e5508dfcede39fa53faa2
SHA-512: 96fa772f72678c85bbd5d23b66d51d50f8f9824a0aba0ded624ab61fe8b602bf4e3611075fe13595d3e74c63c59f7d79241acc97888e9a7a5c791159c85c3ccd

SHA-2 is a family of four hash functions: SHA-224, SHA-256, SHA-384, and SHA-512. SHA-256 and SHA-512 are the most commonly used.

SHA-3

SHA-3 is the latest family of hashing algorithms and offers better security:

# hash with SHA-3
print("SHA-3-256:", hashlib.sha3_256(message).hexdigest())
print("SHA-3-512:", hashlib.sha3_512(message).hexdigest())
Output:
SHA-3-256: d7007c1cd52f8168f22fa25ef011a5b3644bcb437efa46de34761d3340187609
SHA-3-512: de6b4c8f7d4fd608987c123122bcc63081372d09b4bc14955bfc828335dec1246b5c6633c5b1c87d2ad2b777d713d7777819263e7ad675a3743bf2a35bc699d0

BLAKE2

BLAKE2 is faster than SHA-1, SHA-2, SHA-3, and even MD5, and more secure than SHA-2:

# hash with BLAKE2
# 256-bit BLAKE2 (or BLAKE2s)
print("BLAKE2c:", hashlib.blake2s(message).hexdigest())
# 512-bit BLAKE2 (or BLAKE2b)
print("BLAKE2b:", hashlib.blake2b(message).hexdigest())
Output:
BLAKE2c: 6889074426b5454d751547cd33ca4c64cd693f86ce69be5c951223f3af845786
BLAKE2b: 13e2ca8f6a282f27b2022dde683490b1085b3e16a98ee77b44b25bc84a0366afe8d70a4aa47dd10e064f1f772573513d64d56e5ef646fb935c040b32f67e5ab2

Conclusion

BLAKE2 hashes are faster than SHA-1, SHA-2, SHA-3, and MD5, and more secure than SHA-2. BLAKE2 is widely used and has been integrated into major cryptography libraries such as OpenSSL and Sodium.

To hash an entire file, you can read the file content and pass the bytes to any of the functions we covered.

For further reading, refer to the official Python documentation for the hashlib module.

Finally, if you're interested in ethical hacking, check out our guide where we build 35+ hacking tools and scripts using Python!

Examples are from the following article:

How to Use Hashing Algorithms in Python using hashlib - https://thepythoncode.com/article/build-a-password-manager-in-python