Algorithm


  1. Import the hashlib library:

    • Use the hashlib library, which provides a common interface to various secure hash and message digest algorithms.
  2. Open the file in binary mode:

    • Open the file using the open function in binary mode ('rb').
  3. Initialize the hash object:

    • Choose a hash algorithm (e.g., MD5, SHA-1, SHA-256) and create a hash object using the hashlib.new method.
  4. Read the file in chunks:

    • Read the file in small chunks to avoid loading the entire file into memory at once.
  5. Update the hash object:

    • Update the hash object with each chunk of data using the update method.
  6. Finalize the hash and get the digest:

    • After reading the entire file, call the hexdigest method on the hash object to get the final hash value.
  7. Close the file:

    • Close the file using the close method to free up system resources.

 

Code Examples

#1 Code Example- Find SHA256 Hash of a File in Python

Code - Python Programming

import hashlib # hashlib module
import os.path # For file handling
from os import path
 
def hash_file(filename):
 
  if path.isfile(filename) is False:
    raise Exception("File not found for hash operation") 
 
  # make a hash object
  h_sha256 = hashlib.sha256()
 
  # open file for reading in binary mode
  with open(filename,'rb') as file:
 
    # read file in chunks and update hash
    chunk = 0
    while chunk != b'':
      chunk = file.read(1024) 
      h_sha256.update(chunk)
 
  # return the hex digest
  return h_sha256.hexdigest()
 
####### Example Usage
message = hash_file("Python")
print(message)
Copy The Code & Try With Live Editor

Output

x
+
cmd
8964ed69cac034a6bc88ad33089500b6a26a62f19e1574f2f8fbfddeb9a30667

#2 Code Example- Find SHA Hash of a File in Python (Not recommended)

Code - Python Programming

import hashlib # hashlib module
import os.path # For file handling
from os import path
 
def hash_file(filename):
 
  if path.isfile(filename) is False:
    raise Exception("File not found for hash operation") 
 
  # make a hash object
  h = hashlib.sha1()
 
  # open file for reading in binary mode
  with open(filename,'rb') as file:
 
    # read file in chunks and update hash
    chunk = 0
    while chunk != b'':
      chunk = file.read(1024) 
      h.update(chunk)
 
  # return the hex digest
  return h.hexdigest()
 
####### Example Usage
message = hash_file("Python")
print(message)
Copy The Code & Try With Live Editor

Output

x
+
cmd
498fd2d318447f9d0fac30c6e0997c03861c679b

#3 Code eXAMPLE- Find MD5 Hash of a File in Python (Not recommended)

Code - Python Programming

import hashlib # hashlib module
import os.path # For file handling
from os import path
 
def hash_file(filename):
 
  if path.isfile(filename) is False:
    raise Exception("File not found for hash operation") 
 
  # make a hash object
  md5_h = hashlib.md5()
 
  # open file for reading in binary mode
  with open(filename,'rb') as file:
 
    # read file in chunks and update hash
    chunk = 0
    while chunk != b'':
      chunk = file.read(1024) 
      md5_h.update(chunk)
 
  # return the hex digest
  return md5_h.hexdigest()
 
####### Example Usage
message = hash_file("Python")
print(message)
Copy The Code & Try With Live Editor

Output

x
+
cmd
ee6dcaf64b270667125a33f9b7bebb75
Advertisements

Demonstration


Python Programing Example to Find Hash of File-DevsEnv