PYTHON HELP

hashlib Python (How It Works: A Guide for Developers)

Published February 19, 2025
Share:

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.

Introduction

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

  1. 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.
  2. 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.
  3. 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 IronSoftware is Awesome')
# Get the hexadecimal representation of the hash in  bytes object
hash_hex = hash_object.hexdigest() # hash_hex is digest object
print(hash_hex) # byte string
#Output: 6fc0c7d6af8eb51f0cd89281db55c6a6b76b5310226fa5af2272a8eb42cc1bfe
PYTHON

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
PYTHON

The code generates SHA-256 for input data passed

2. Using Different Digest Sizes

import hashlib
# Hashing with different digest sizes
data = b'Hello, World!' # convert to binary data
hash_md5 = hashlib.md5(data).hexdigest()
hash_sha256 = hashlib.sha256(data).hexdigest()
hash_sha512 = hashlib.sha512(data).hexdigest()
print("MD5 Hash (hex):", hash_md5)
print("SHA-256 Hash (hex):", hash_sha256)
print("SHA-512 Hash (hex):", hash_sha512)
#output hash digest with only hexadecimal digits
MD5 Hash (hex): 65a8e27d8879283831b664bd8b7f0ad4
SHA-256 Hash (hex): dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
SHA-512 Hash (hex): 374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387
PYTHON

The code generates hashs for md5, sha256, and sha512 using the data passed.

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
PYTHON

4. Randomized Hashing

import hashlib
import os
# Randomized hashing example (Salsa20)
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
PYTHON

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

  1. 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.
  2. 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.
  3. Digital Signatures: Hash functions are commonly used to create digital signatures, which verify a message's authenticity and integrity.

Introducing IronPDF

hashlib Python ((How It Works: A Guide for Developers)): Figure 1 - IronPDF for Python: The Python PDF Library

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

  1. IronPDF uses .NET 6.0 as its underlying technology. Hence, make sure .NET 6.0 runtime is installed on your system.
  2. Python 3.0+: You need to have Python version 3 or later installed.
  3. 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 IronSoftware is Awesome</p>"
content += "<h2> Simple hashing example</h2>"
content += "<p></p>"
# 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")
PYTHON

Code Explanation

The provided code showcases the usage of various hashing techniques using Python's `hashlib` library:

  1. Simple Hashing Example: Computes the SHA-256 hash of a specific data string (`b'IronPDF from Iron Software is Awesome").
  2. Hashing with Different Digest Sizes: This section demonstrates hashing using MD5, SHA-256, and SHA-512 algorithms on the exact data string.3. Keyed-Hashing Example: This example uses the `blake2b` hash function with a specified key (`b'pseudorandom key") to perform keyed hashing on the data.

  3. Randomized Hashing Example: Utilizes the PBKDF2-HMAC algorithm with SHA-256 to generate a randomized hash with a randomly generated salt.5. 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

hashlib Python ((How It Works: A Guide for Developers)): Figure 2 - Example console output

PDF

hashlib Python ((How It Works: A Guide for Developers)): Figure 3 - Example PDF ouput using IronPDF

IronPDF License

hashlib Python ((How It Works: A Guide for Developers)): Figure 4 - IronPDF licensing page

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"
PYTHON

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.

Kannaopat Udonpant

Kannapat Udonpant

Software Engineer

 LinkedIn

Before becoming a Software Engineer, Kannapat completed a Environmental Resources PhD from Hokkaido University in Japan. While pursuing his degree, Kannapat also became a member of the Vehicle Robotics Laboratory, which is part of the Department of Bioproduction Engineering. In 2022, he leveraged his C# skills to join Iron Software's engineering team, where he focuses on IronPDF. Kannapat values his job because he learns directly from the developer who writes most of the code used in IronPDF. In addition to peer learning, Kannapat enjoys the social aspect of working at Iron Software. When he's not writing code or documentation, Kannapat can usually be found gaming on his PS5 or rewatching The Last of Us.
NEXT >
XGBoost Python (How It Works: A Guide for Developers)