Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Heap queue or heapq in Python
Next article icon

hashlib module in Python

Last Updated : 20 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A Cryptographic hash function is a function that takes in input data and produces a statistically unique output, which is unique to that particular set of data. The hash is a fixed-length byte stream used to ensure the integrity of the data. In this article, you will learn to use the hashlib module to obtain the hash of a file in Python. The hashlib module is a built-in module that comes by default with Python's standard library so there is no need to install it manually, you can just import it directly:

import hashlib

What is the Hashlib Module?

The hashlib module implements a common interface for many secure cryptographic hash and message digest algorithms. There is one constructor method named for each type of hash. All return a hash object with the same simple interface. Constructors for hash algorithms are always present in this module. 

hashlib.algorithms_guaranteed

A set containing the names of the hash algorithms is guaranteed to be supported by this module on all platforms.

>>> print(hashlib.algorithms_guaranteed)

{'sha3_512', 'sha1', 'sha224', 'shake_256', 'sha3_384', 'sha512', 'sha384', 'blake2s', 'md5', 'sha3_224', 'sha256', 'blake2b', 'sha3_256', 'shake_128'}

hashlib.algorithms_available

A set containing the names of the hash algorithms available in the running Python interpreter.  The same algorithm may appear multiple times in this set under different names (due to OpenSSL).

>>> print(hashlib.algorithms_available)

{'sha384', 'sha3_224', 'whirlpool', 'ripemd160', 'blake2s', 'md5-sha1', 'sm3', 'sha256', 'shake_256', 'sha1', 'sha3_384', 

'sha512', 'blake2b', 'sha512_256', 'sha3_256', 'shake_128', 'sha3_512', 'sha224', 'md5', 'mdc2', 'sha512_224', 'md4'}

Explanation of SHA-256 Algorithm and its Features

This article will use the FIPS secure hash algorithm SHA-256 to obtain the file hash. Other secure hash algorithms include:

  • MD5 (Message Digest 5)
  • SHA-512 (Secure Hashing Algorithm 512 bits)
  • RC4 (Rivest Cipher 4)

The reason for the usage of SHA-256 is it is one of the most renowned and secure hashing algorithms currently used while offering less time required to compute a hash. The algorithm belongs to the SHA-2 Family, which is succeeded by the SHA-3 family based on sponge construction structure.

Obtaining a Cryptographic Hash of a File

In the following example, a path to a file would be provided as a command line argument. Then the SHA 256 (Secured Hashing Algorithm-256bits) hash would be obtained for the file and displayed. 

Hash of the following file:

hashlib module in Python
test.txt

Firstly the hashlib and sys modules are imported. The sys module is imported to allow command-line arguments in the code. Then the function that would be used to obtain the SHA-256 hash of the file is defined. In the function, a Buffer size is defined (65536 in our case). This buffer size is the number of bytes read from the file (at a time) and fed into the SHA-256 hash function. This allows larger files to be operated without producing memory constraints. At the end of the function, the hexdigest function is called on the hash to produce its hexadecimal representation. The function call to the above function (hashfile) contains the first argument (sys.argv[1]) that is provided while calling the function from the command line (the 0th argument is the Python file name). In the end, the hash of the file is displayed.

Python3
# importing sys for getting commandline arguments import sys  # importing hashlib for getting sha256() hash function import hashlib   def hashfile(file):      # A arbitrary (but fixed) buffer size     # 65536 = 65536 bytes = 64 kilobytes     BUF_SIZE = 65536      # Initializing the sha256() method     sha256 = hashlib.sha256()      # Opening the file provided as the first      # commandline argument     with open("test.txt", 'rb') as f:         while True:             # reading data = BUF_SIZE from the              # file and saving it in a variable             data = f.read(BUF_SIZE)              # True if eof = 1             if not data:                 break              # Passing that data to that sh256 hash              # function (updating the function with that data)             sha256.update(data)      # sha256.hexdigest() hashes all the input data passed     # to the sha256() via sha256.update()     # Acts as a finalize method, after which      # all the input data gets hashed     # hexdigest() hashes the data, and returns      # the output in hexadecimal format     return sha256.hexdigest()   # Calling hashfile() function to obtain hash of the file  # and saving the result in a variable file_hash = hashfile(sys.argv[1])  print(f"Hash:{file_hash}") 

Output:

Hash of a String in Python

Obtaining a Cryptographic Hash of a String

The above method could also be used to obtain the hash of a finite-length string. For that, the string needs to be converted to a byte stream before it is sent as an argument. For short strings, the process could be accomplished in a single call. The following example demonstrates this in practice:

Firstly a byte literal is initialized and is stored to a variable (due to the b prefix of the string). Then the sha256 function is initialized, and the byte literal is passed as an argument to the update function. This updates the sha256 algorithm with the data. After which, the hash digest is computed, and its hexadecimal equivalent is requested using the hexdigest function. At the end, this hash value is displayed.

Python3
# importing hashlib for getting sha256() hash function import hashlib   # A string that has been stored as a byte stream # (due to the prefix b) string = b"My name is apple and I am a vegetable?"  # Initializing the sha256() method sha256 = hashlib.sha256()  # Passing the byte stream as an argument sha256.update(string)  # sha256.hexdigest() hashes all the input data # passed to the sha256() via sha256.update() # Acts as a finalize method, after which all # the input data gets hashed # hexdigest() hashes the data, and returns # the output in hexadecimal format string_hash = sha256.hexdigest()   print(f"Hash:{string_hash}") 

Output:

Hash:252f8ca07a6fcaae293e5097151c803a7f16504e48c4eb60f651c11341e83217

Next Article
Heap queue or heapq in Python

V

vasudev4
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Technical Scripter 2022
Practice Tags :
  • python

Similar Reads

  • Python Modules
    Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work. In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we
    7 min read
  • Python Arrays
    Lists in Python are the most flexible and commonly used data structure for sequential storage. They are similar to arrays in other languages but with several key differences: Dynamic Typing: Python lists can hold elements of different types in the same list. We can have an integer, a string and even
    10 min read
  • asyncio in Python
    Asyncio is a Python library that is used for concurrent programming, including the use of async iterator in Python. It is not multi-threading or multi-processing. Asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance network and web servers, databa
    4 min read
  • Calendar in Python
    Python has a built-in Python Calendar module to work with date-related tasks. Using the module, we can display a particular month as well as the whole calendar of a year. In this article, we will see how to print a calendar month and year using Python. Calendar in Python ExampleInput: yy = 2023 mm =
    2 min read
  • Python Collections Module
    The collection Module in Python provides different types of containers. A Container is an object that is used to store different objects and provide a way to access the contained objects and iterate over them. Some of the built-in containers are Tuple, List, Dictionary, etc. In this article, we will
    13 min read
  • Working with csv files in Python
    Python is one of the important fields for data scientists and many programmers to handle a variety of data. CSV (Comma-Separated Values) is one of the prevalent and accessible file formats for storing and exchanging tabular data. In article explains What is CSV. Working with CSV files in Python, Rea
    10 min read
  • Python datetime module
    In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. In this article, we will explore How DateTime in Python
    14 min read
  • Functools module in Python
    Functools module is for higher-order functions that work on other functions. It provides functions for working with other functions and callable objects to use or extend them without completely rewriting them. This module has two classes - partial and partialmethod. Partial class A partial function
    6 min read
  • hashlib module in Python
    A Cryptographic hash function is a function that takes in input data and produces a statistically unique output, which is unique to that particular set of data. The hash is a fixed-length byte stream used to ensure the integrity of the data. In this article, you will learn to use the hashlib module
    5 min read
  • Heap queue or heapq in Python
    A heap queue or priority queue is a data structure that allows us to quickly access the smallest (min-heap) or largest (max-heap) element. A heap is typically implemented as a binary tree, where each parent node's value is smaller (for a min-heap) or larger (for a max-heap) than its children. Howeve
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences