hashlib Python (How It Works: A Guide for Developers)
The hashlib module in Python is a powerful tool for working with secure hash and message digest algorithms. This module provides a standard interface to many secure hash algorithms, making it a versatile choice for developers needing to ensure data integrity and security. Later in the article, we will also look into a versatile PDF generation Library from IronSoftware called IronPDF and write a script using both libraries to demonstrate their usage.
The hashlib module is part of Python's standard library, so there's no need to install it separately. It includes various cryptographic hash functions, such as MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, and the SHA-3 series. These functions are used to create hash objects, which can then be used to generate hashes of data.
Key Features
- Wide Range of Algorithms: hashlib supports multiple hash algorithms, including older ones like MD5 and SHA-1 and more modern ones like SHA-256 and SHA-3.
- Simple Interface: Each hash algorithm has a constructor method that returns a hash object. This object can be fed with data using the update method and produce the hash value using the digest or hexdigest methods.
- Security: While some algorithms like MD5 and SHA-1 have known vulnerabilities, hashlib includes more secure options like SHA-256 and SHA-3.
Installation
hashlib is an inbuilt module and does not require explicit installation.
Basic Usage
Here's a simple example of how to use hashlib to generate an SHA-256 hash using the hashlib hash constructor:
import hashlib
# Creating hash objects with SHA-256
hash_object = hashlib.sha256()
# Update the hash object with data
hash_object.update(b'IronPDF from Iron Software is Awesome')
# Get the hexadecimal representation of the hash
hash_hex = hash_object.hexdigest() # hash_hex is the hexadecimal digest
print(hash_hex) # Output the hash
# Output: 6fc0c7d6af8eb51f0cd89281db55c6a6b76b5310226fa5af2272a8eb42cc1bfe
import hashlib
# Creating hash objects with SHA-256
hash_object = hashlib.sha256()
# Update the hash object with data
hash_object.update(b'IronPDF from Iron Software is Awesome')
# Get the hexadecimal representation of the hash
hash_hex = hash_object.hexdigest() # hash_hex is the hexadecimal digest
print(hash_hex) # Output the hash
# Output: 6fc0c7d6af8eb51f0cd89281db55c6a6b76b5310226fa5af2272a8eb42cc1bfe
Advanced Features
- Multithreading Support: With the cryptographic hash function, hashlib releases the Global Interpreter Lock (GIL) while computing a hash if more than 2047 bytes of data are supplied at once, allowing for better performance in multithreaded applications.
- Custom Hash Algorithms: If your Python distribution's hashlib is linked against a build of OpenSSL that provides additional algorithms, you can access them via the new() method.
Various Types of Hashing using HashLib module
1. Simple Hashing
import hashlib
# Simple hashing example
data = b'Hello, World!'
hash_object = hashlib.sha256(data)
hex_dig = hash_object.hexdigest()
print("SHA-256 Hash:", hex_dig)
# Output: SHA-256 Hash: dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
import hashlib
# Simple hashing example
data = b'Hello, World!'
hash_object = hashlib.sha256(data)
hex_dig = hash_object.hexdigest()
print("SHA-256 Hash:", hex_dig)
# Output: SHA-256 Hash: dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
The code generates an SHA-256 hash for the input data.
2. Using Different Digest Sizes
import hashlib
# Hashing with different digest sizes
data = b'Hello, World!'
# MD5, SHA-256, and SHA-512 hash generation
hash_md5 = hashlib.md5(data).hexdigest()
hash_sha256 = hashlib.sha256(data).hexdigest()
hash_sha512 = hashlib.sha512(data).hexdigest()
# Print each hash
print("MD5 Hash (hex):", hash_md5)
print("SHA-256 Hash (hex):", hash_sha256)
print("SHA-512 Hash (hex):", hash_sha512)
# Output:
# MD5 Hash (hex): 65a8e27d8879283831b664bd8b7f0ad4
# SHA-256 Hash (hex): dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
# SHA-512 Hash (hex): 374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387
import hashlib
# Hashing with different digest sizes
data = b'Hello, World!'
# MD5, SHA-256, and SHA-512 hash generation
hash_md5 = hashlib.md5(data).hexdigest()
hash_sha256 = hashlib.sha256(data).hexdigest()
hash_sha512 = hashlib.sha512(data).hexdigest()
# Print each hash
print("MD5 Hash (hex):", hash_md5)
print("SHA-256 Hash (hex):", hash_sha256)
print("SHA-512 Hash (hex):", hash_sha512)
# Output:
# MD5 Hash (hex): 65a8e27d8879283831b664bd8b7f0ad4
# SHA-256 Hash (hex): dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
# SHA-512 Hash (hex): 374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387
The code generates hashes for MD5, SHA-256, and SHA-512 using the provided data.
3. Keyed-Hashing
import hashlib
from hashlib import blake2b
# Keyed hashing example
h = blake2b(key=b'pseudorandom key', digest_size=16)
h.update(b'message data')
print(h.hexdigest())
# Output: 3d363ff7401e02026f4a4687d4863ced
import hashlib
from hashlib import blake2b
# Keyed hashing example
h = blake2b(key=b'pseudorandom key', digest_size=16)
h.update(b'message data')
print(h.hexdigest())
# Output: 3d363ff7401e02026f4a4687d4863ced
This code demonstrates creating a keyed hash using the Blake2b algorithm.
4. Randomized Hashing
import hashlib
import os
# Randomized hashing example using PBKDF2-HMAC
data = b'Hello, World!'
salt = os.urandom(16) # Generate a random salt
hash_object = hashlib.pbkdf2_hmac('sha256', data, salt, 100000)
hex_dig = hash_object.hex()
print("Randomized Hash (SHA-256):", hex_dig)
# Output: Randomized Hash (SHA-256): a2a3c1a30a2add1867d55eac97fd9c84dc679691c0f15ae09c01e1bcc63ba47a
import hashlib
import os
# Randomized hashing example using PBKDF2-HMAC
data = b'Hello, World!'
salt = os.urandom(16) # Generate a random salt
hash_object = hashlib.pbkdf2_hmac('sha256', data, salt, 100000)
hex_dig = hash_object.hex()
print("Randomized Hash (SHA-256):", hex_dig)
# Output: Randomized Hash (SHA-256): a2a3c1a30a2add1867d55eac97fd9c84dc679691c0f15ae09c01e1bcc63ba47a
These examples cover basic hashing using different digest sizes. Adjustments can be made based on specific requirements or preferences, such as using different algorithms or parameters.
Practical Applications
- Data Integrity: Hash functions are generally used to confirm records integrity. By comparing the hash of the original data with the hash of the received data, you can ensure that the data has not been altered.
- Password Storage: Hash functions are often used to securely store passwords. The system stores the password's hash instead of the actual password. When a user logs in, the hash of the entered password is compared with the stored hash.
- Digital Signatures: Hash functions are commonly used to create digital signatures, which verify a message's authenticity and integrity.
Introducing IronPDF
IronPDF is a powerful Python library for creating, editing, and signing PDFs using HTML, CSS, images, and JavaScript. It provides high-performance capabilities with minimal memory usage. Users can generate PDFs from HTML, merge or split PDF documents, extract text and images from PDFs, apply watermarks, rasterize a PDF to image formats like JPEG and PNG, encrypt PDF files, and more. IronPDF offers a wide range of PDF operations.
Key features of IronPDF
HTML to PDF Conversion
Users can convert HTML files, HTML strings, and URLs to PDFs. For example, render a webpage as a PDF using IronPDF's Chrome PDF renderer from IronPDF.
Cross-Platform Support
IronPDF is designed for Python 3+ versions and runs on Windows, Mac, Linux, or Cloud Platforms.
IronPDF is also available in .NET, Java, Python, and Node.js.
Editing and Signing
The user can set properties, add security with passwords and permissions, and apply digital signatures to PDFs using IronPDF.
Page Templates and Settings
IronPDF allows you to customize PDF documents with headers, footers, page numbers, and adjustable margins. It also supports responsive layouts and custom paper sizes.
Standards Compliance
The IronPDF package also adheres to PDF standards such as PDF/A and PDF/UA. It supports UTF-8 character encoding and handles assets like images, CSS, and fonts.
Generate PDF Documents using IronPDF and HashLib module
IronPDF Prerequisites
- IronPDF uses .NET 6.0 as its underlying technology. Hence, make sure .NET 6.0 runtime is installed on your system.
- Python 3.0+: You need to have Python version 3 or later installed.
- Pip: Install Python package installer pip to install IronPDF package.
To start, let's create a Python file to add our scripts. For this example, we use Visual Studio Code as the code editor.
Open Visual Studio Code and create a file, hashlibDemo.py.
Install IronPDF library:
pip install ironpdf
Then add the below code to demonstrate the usage of IronPDF and Hashlib python packages
import hashlib
import os
from hashlib import blake2b
from ironpdf import *
# Apply your license key
License.LicenseKey = "your key"
# Create a PDF from a HTML string using Python
content = "<h1>Awesome Iron PDF with hashlib</h1>"
content += "<p>Data for all the below examples = IronPDF from Iron Software is Awesome</p>"
content += "<h2> Simple hashing example</h2>"
# Simple hashing example
data = b'IronPDF from Iron Software is Awesome'
content += "<p>hashlib.sha256(data)</p>"
hash_object = hashlib.sha256(data)
hex_dig = hash_object.hexdigest()
print("SHA-256 Hash:", hex_dig)
content += "<p>SHA-256 Hash:"+str(hex_dig)+"</p>"
content += "<h2> Hashing with different digest sizes</h2>"
# Hashing with different digest sizes
hash_md5 = hashlib.md5(data).hexdigest()
content += "<p>hashlib.md5(data).hexdigest()</p>"
hash_sha256 = hashlib.sha256(data).hexdigest()
content += "<p>hashlib.sha256(data).hexdigest()</p>"
hash_sha512 = hashlib.sha512(data).hexdigest()
content += "<p>hashlib.sha512(data).hexdigest()</p>"
print("MD5 Hash (hex):", hash_md5)
print("SHA-256 Hash (hex):", hash_sha256)
print("SHA-512 Hash (hex):", hash_sha512)
content += "<p>MD5 Hash (hex):"+str(hash_md5)+"</p>"
content += "<p>SHA-256 Hash (hex):"+str(hash_sha256)+"</p>"
content += "<p>SHA-512 Hash (hex):"+str(hash_sha512)+"</p>"
# Keyed hashing example
content += "<h2> Keyed hashing example</h2>"
h = blake2b(key=b'pseudorandom key', digest_size=16)
content += "<p></p>"
h.update(data)
print(h.hexdigest())
content += "<p>Keyed Hash (hex):"+str(h.hexdigest())+"</p>"
# Randomized hashing example
content += "<h2> Randomized hashing example </h2>"
salt = os.urandom(16) # Generate a random salt
hash_object = hashlib.pbkdf2_hmac('sha256', data, salt, 100000)
content += "<p>hashlib.pbkdf2_hmac('sha256', data, salt, 100000)</p>"
hex_dig = hash_object.hex()
print("Randomized Hash (SHA-256):", hex_dig)
content += "<p>Randomized Hash (SHA-256):"+str(hex_dig)+"</p>"
# Generate PDF using IronPDF
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(content)
# Export to a file or Stream
pdf.SaveAs("Demo-hashlib.pdf")
import hashlib
import os
from hashlib import blake2b
from ironpdf import *
# Apply your license key
License.LicenseKey = "your key"
# Create a PDF from a HTML string using Python
content = "<h1>Awesome Iron PDF with hashlib</h1>"
content += "<p>Data for all the below examples = IronPDF from Iron Software is Awesome</p>"
content += "<h2> Simple hashing example</h2>"
# Simple hashing example
data = b'IronPDF from Iron Software is Awesome'
content += "<p>hashlib.sha256(data)</p>"
hash_object = hashlib.sha256(data)
hex_dig = hash_object.hexdigest()
print("SHA-256 Hash:", hex_dig)
content += "<p>SHA-256 Hash:"+str(hex_dig)+"</p>"
content += "<h2> Hashing with different digest sizes</h2>"
# Hashing with different digest sizes
hash_md5 = hashlib.md5(data).hexdigest()
content += "<p>hashlib.md5(data).hexdigest()</p>"
hash_sha256 = hashlib.sha256(data).hexdigest()
content += "<p>hashlib.sha256(data).hexdigest()</p>"
hash_sha512 = hashlib.sha512(data).hexdigest()
content += "<p>hashlib.sha512(data).hexdigest()</p>"
print("MD5 Hash (hex):", hash_md5)
print("SHA-256 Hash (hex):", hash_sha256)
print("SHA-512 Hash (hex):", hash_sha512)
content += "<p>MD5 Hash (hex):"+str(hash_md5)+"</p>"
content += "<p>SHA-256 Hash (hex):"+str(hash_sha256)+"</p>"
content += "<p>SHA-512 Hash (hex):"+str(hash_sha512)+"</p>"
# Keyed hashing example
content += "<h2> Keyed hashing example</h2>"
h = blake2b(key=b'pseudorandom key', digest_size=16)
content += "<p></p>"
h.update(data)
print(h.hexdigest())
content += "<p>Keyed Hash (hex):"+str(h.hexdigest())+"</p>"
# Randomized hashing example
content += "<h2> Randomized hashing example </h2>"
salt = os.urandom(16) # Generate a random salt
hash_object = hashlib.pbkdf2_hmac('sha256', data, salt, 100000)
content += "<p>hashlib.pbkdf2_hmac('sha256', data, salt, 100000)</p>"
hex_dig = hash_object.hex()
print("Randomized Hash (SHA-256):", hex_dig)
content += "<p>Randomized Hash (SHA-256):"+str(hex_dig)+"</p>"
# Generate PDF using IronPDF
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(content)
# Export to a file or Stream
pdf.SaveAs("Demo-hashlib.pdf")
Code Explanation
The provided code showcases the usage of various hashing techniques using Python's hashlib
library:
- Simple Hashing Example: Computes the SHA-256 hash of a specific data string (
b'IronPDF from Iron Software is Awesome'
). - Hashing with Different Digest Sizes: This section demonstrates hashing using MD5, SHA-256, and SHA-512 algorithms on the exact data string.
- Keyed-Hashing Example: This example uses the
blake2b
hash function with a specified key (b'pseudorandom key'
) to perform keyed hashing on the data. - Randomized Hashing Example: Utilizes the PBKDF2-HMAC algorithm with SHA-256 to generate a randomized hash with a randomly generated salt.
- PDF Generation: After demonstrating the hashing examples, the code generates a PDF document using IronPDF, which includes the HTML content showcasing the hashing examples.
Each example illustrates different aspects of cryptographic hashing, such as standard hashing, keyed hashing, and randomized hashing techniques.
Output
IronPDF License
IronPDF runs on the Python license key. IronPDF for Python offers a free trial license key to allow users to test its extensive features before purchase.
Place the License Key at the start of the script before using IronPDF package:
from ironpdf import *
# Apply your license key
License.LicenseKey = "key"
from ironpdf import *
# Apply your license key
License.LicenseKey = "key"
Conclusion
The hashlib module is essential to Python's standard library, providing robust and secure hash functions for various applications. Whether you're ensuring data integrity, securely storing passwords, or creating digital signatures, hashlib offers the necessary tools. On the other hand, IronPDF is a powerful PDF generation and PDF manipulation library. With both these libraries, developers can quickly generate hashes and store them in PDF format.