Member-only story
Building a Hashing Tool with Python
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()