top of page
  • Writer's pictureBrian

Obtaining a Files Hash Value Using Python

Hash values are unique identifiers used to differentiate between data. Microsoft more properly defined hash values as, “ a numeric value of a fixed length that uniquely identifies data.” (Microsoft, 2017) The Python programming language has a useful module that can be used to obtain hash values. The hashlib module can be used to perform various cryptographic functions. “This module implements a common interface to many different secure hash and message digest algorithms.” (Python Software Foundation, 2019)

To use the hashlib method, you will first need to install it using a Python package installer. This can be done through the Linux command line using pip.

I’m using Ubuntu 18.04.3 LTS, and on my system, I installed pip with the command

$ sudo apt install python-pip

$ pip install hashlib

You can then check to see if hashlib is located on your system using the locate command. I piped it to a grep search to highlight the file paths.

Once hashlib was installed, I then wrote a script using the method to obtain the MD5 hash of a selected file. I modified some code I found in an article on, to prompt for input and to function using Python 3. (Torres, 2013) The following code is what I came up with.

From the command line, the code prompted for a file name and returned the MD5 hash value of that file. I ran a test file named test.txt through the program and was returned with the file’s MD5 hash value. To verify that the MD5 hash was correct, I also ran the file trough Hashdeep. Both Hashdeep and the Python script returned the same MD5 hash value.

The hashlib module allows for Python users to utilize various secure hash and message digest algorithms within their programs. (Python Software Foundation, 2019)


Microsoft. (2017, March 29). Ensuring Data Integrity with Hash Codes. Retrieved from

Python Software Foundation. (2019, December 28). hashlib — Secure hashes and message digests. Retrieved from

Torres, A. (2013, May 16). Hashing Files with Python. Retrieved from

114 views0 comments


bottom of page