• We just launched and are currently in beta. Join us as we build and grow the community.

How to Find Hash of File in Terminal Console using Python

TheCuess

Cyber Defense Assessor
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
117
Likes
50
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we’ll learn how to program "How to Find the Hash of a File in Python." We’ll focus on accurately hashing the targeted file into a secure sequence of characters. The objective is to achieve a proper hash of a given file format. I'll provide a sample program to demonstrate the coding process, making it easy to understand and implement. So, let’s get started with coding!

This topic is straightforward to understand. Just follow the instructions I provide, and you'll be able to complete it with ease. The program I'll show you demonstrates the correct way to hash a file. I'll also provide a simple and efficient method to achieve this effectively. So, let’s start coding!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import

    hashlib

  2. def

    compute_file_hash(

    file_path,

    algorithm=

    'sha256'

    )

    :
  3. hash_func =

    hashlib.new

    (

    algorithm)

  4. with

    open

    (

    file_path,

    'rb'

    )

    as

    file

    :
  5. while

    chunk

    :=

    file

    .read

    (

    8192

    )

    :
  6. hash_func.update

    (

    chunk

    )

  7. return

    hash_func.hexdigest

    (

    )

  8. def

    main(

    )

    :
  9. while

    True

    :
  10. print

    (

    "\n

    ================= Find Hash of File =================\n

    \n

    "

    )
  11. file_path =

    input

    (

    "Enter the full filename: "

    )
  12. print

    (

    "\n

    "

    )
  13. algorithm =

    input

    (

    "Enter the hash algorithm (e.g., md5, sha1, sha256): "

    )
  14. print

    (

    "\n

    "

    )
  15. try

    :
  16. file_hash =

    compute_file_hash(

    file_path,

    algorithm)
  17. print

    (

    f"The {algorithm} hash of the file is: {file_hash}"

    )
  18. except

    FileNotFoundError:
  19. print

    (

    "File not found. Please enter a valid file path."

    )
  20. except

    ValueError

    :
  21. print

    (

    f"Invalid hash algorithm: {algorithm}. Please enter a valid algorithm (e.g., md5, sha1, sha256)."

    )

  22. opt =

    input

    (

    "\n

    Do you want to try again?(yes/no): "

    )

  23. if

    opt.lower

    (

    )

    ==

    'yes'

    :
  24. ret=

    False
  25. elif

    opt.lower

    (

    )

    ==

    'no'

    :
  26. ret=

    True
  27. print

    (

    "Exiting program...."

    )
  28. else

    :
  29. print

    (

    "Please enter yes/no:"

    )
  30. break

  31. if

    ret ==

    False

    :
  32. continue
  33. if

    __name__ ==

    "__main__"

    :
  34. main(

    )

This program calculates the cryptographic hash of a file based on a specified algorithm (such as MD5, SHA-1, or SHA-256). It reads the file in binary mode in chunks, updating the hash until the file is fully processed, and then returns the file's hash digest in hexadecimal format. The user can specify the hash algorithm and file path, and the program will validate both inputs, catching cases where the file doesn’t exist or the algorithm name is invalid. After each hash calculation, the user has the option to repeat the process or exit.

Output:

how-to-find-hash-of-file-in-python-1.jpg

There you have it we successfully created How to Find Hash of File in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top