Building a Hashing Tool with Python

Chris Doucette
Nov 28, 2018 · 2 min read

Introduction

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.

Setup

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 pyhash.py. This will be the file we will write all of our code in.

$ touch pyhash.py

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()
parser.add_argument("file")
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.

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

Running the Tool

We have completed building our tool! Lets create a test file to make sure it works.

echo "This is a test file!" > test.txt

Now when we run it and give it our test file test.txt it should hash it for us.

$ python3 pyhash.py test.txt
test.txt ff22941336956098ae9a564289d1bf1b 3c1bb0cd5d67dddc02fae50bf56d3a3a4cbc7204

And look at that it worked!

Conclusion

Today we built a tool that was able to be given a file and output the MD5 and SHA1 hashes for the file. This can be very useful to determine the integrity of a file you may have downloaded or been given. I hope this article has helped you better understand Python and how it can be very easy to build your own tools.

Thanks for reading!

EdibleSec

Easily consumable information security stories :: https://ediblesec.com

Chris Doucette

Written by

Security Engineer | ediblesec.com | Follow me on Twitter: @thegrumpyape

EdibleSec

EdibleSec

Easily consumable information security stories :: https://ediblesec.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade