Building a Hashing Tool with Python

Chris Doucette
2 min readNov 28, 2018


I believe that it is very important to understand not only how to use a tool but also how it works. Today I will be demonstrating how you can create a CLI tool in Python to hash files.


I am going to use Python3 to build this tool so you will need to make sure you have Python3 installed. You can do this by running:

$ python3 -V
Python 3.6.7

Once we have verified that Python3 is installed we will create a new file called This will be the file we will write all of our code in.

$ touch

Once the file has been created open it up in your favorite text editor and lets get started.

Building the Tool

We will need some Python standard libraries to accomplish our task.

import os
import hashlib
import argparse

Now we will start by grabbing an argument given when running the file. Here we are creating an ArgumentParser object, getting the first argument and giving it the name file, and then parsing the arguments and assigning them to the variable args.

parser = argparse.ArgumentParser()
args = parser.parse_args()

Now we are going to create the objects that will do the hashing for us.

md5 = hashlib.md5()
sha1 = hashlib.sha1()

Everything is setup so now we are able get the fun part, actually hashing the file! First we are going to make a try/except block in case the file provided doesnt exist. Then we will read the file and add it to a buffer variable to pass it into our hashing objects. Finally we are going to get the MD5 and SHA1 hash of the file and output it all on one line. If the file cannot be found we will generate an error and print it.

with open(args.file, 'rb') as f:
buf =
print("{} {} {}".format(os.path.basename(args.file), md5.hexdigest(), sha1.hexdigest())
except FileNotFoundError as e:

Running the Tool

Chris Doucette

Cybersecurity Analyst and Finance Enthusiast