Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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.
hashlib is an inbuilt module and does not require explicit installation.
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 IronSoftware 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 IronSoftware 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
# 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.
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.
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.
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.
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.
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.
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.
The user can set properties, add security with passwords and permissions, and apply digital signatures to PDFs using IronPDF.
IronPDF allows you to customize PDF documents with headers, footers, page numbers, and adjustable margins. It also supports responsive layouts and custom paper sizes.
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.
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 IronSoftware is Awesome</p>"
content += "<h2> Simple hashing example</h2>"
# Simple hashing example
data = b'IronPDF from IronSoftware 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 IronSoftware is Awesome</p>"
content += "<h2> Simple hashing example</h2>"
# Simple hashing example
data = b'IronPDF from IronSoftware 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")
The provided code showcases the usage of various hashing techniques using Python's hashlib
library:
b'IronPDF from Iron Software is Awesome'
).blake2b
hash function with a specified key (b'pseudorandom key'
) to perform keyed hashing on the data.Each example illustrates different aspects of cryptographic hashing, such as standard hashing, keyed hashing, and randomized hashing techniques.
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"
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.