Master Python’s hashlib: From MD5 to SHA‑512 and Custom Key Encryption

This article introduces Python’s hashlib module, explains the principles and characteristics of common hash algorithms such as MD5, SHA‑1, SHA‑224, SHA‑256, SHA‑384, and SHA‑512, demonstrates how to compute their digests with code examples, and shows simple key‑based encryption techniques.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Python’s hashlib: From MD5 to SHA‑512 and Custom Key Encryption

Introduction

In Python we often need to compute cryptographic hashes such as MD5 and SHA‑1. The hashlib module provides a convenient way to generate these digests. It works on binary data, so strings must be encoded before hashing.

Basic Usage

import hashlib
string = '任性的90后boy'
# Using encode
a = hashlib.sha1()
a.update(string.encode('utf-8'))
print('sha1 with encode:', a.hexdigest())
# Using bytes directly
a = hashlib.sha1()
a.update(bytes(string, encoding='utf-8'))
print('sha1 with bytes:', a.hexdigest())

The following two attributes list all available hash algorithms:

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

1. MD5

MD5 (Message‑Digest Algorithm 5) produces a 128‑bit hash value. It is fast, widely supported, and commonly used for checksums, though it is not collision‑resistant for security‑critical purposes.

import hashlib
string = '任性的90后boy'
md5 = hashlib.md5()
md5.update(string.encode('utf-8'))
print('MD5 digest (hex):', md5.hexdigest())
print('MD5 digest (bytes):', md5.digest())

2. SHA‑1

SHA‑1 generates a 160‑bit hash represented as a 40‑character hexadecimal string.

import hashlib
string = '任性的90后boy'
sha1 = hashlib.sha1()
sha1.update(string.encode('utf-8'))
print('SHA‑1 result:', sha1.hexdigest())

3. SHA‑224

import hashlib
string = '任性的90后boy'
sha224 = hashlib.sha224()
sha224.update(string.encode('utf-8'))
print('SHA‑224 result:', sha224.hexdigest())

4. SHA‑256

import hashlib
string = '任性的90后boy'
sha256 = hashlib.sha256()
sha256.update(string.encode('utf-8'))
print('SHA‑256 result:', sha256.hexdigest())

5. SHA‑384

import hashlib
string = '任性的90后boy'
sha384 = hashlib.sha384()
sha384.update(string.encode('utf-8'))
print('SHA‑384 result:', sha384.hexdigest())

6. SHA‑512

import hashlib
string = '任性的90后boy'
sha512 = hashlib.sha512()
sha512.update(string.encode('utf-8'))
print('SHA‑512 result:', sha512.hexdigest())

7. Simple Key‑Based Encryption

Standard hash functions are vulnerable to rainbow‑table attacks. Adding a custom key (salt) before hashing can improve security.

# Simple MD5 with a custom key
import hashlib
md5 = hashlib.md5()
md5.update('md5'.encode('utf-8'))
print('Plain MD5:', md5.hexdigest())

md5_key = hashlib.md5(b'md512')
md5_key.update('md5_key'.encode('utf-8'))
print('MD5 with key:', md5_key.hexdigest())

Conclusion

We have covered the most commonly used hash algorithms in Python’s hashlib module, demonstrated how to compute their digests, and introduced a basic key‑based approach for enhancing hash security.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonencryptionMD5hash algorithmshashlibSHA-1
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.