Building a Hashing Tool with Python

Introduction

Setup

$ python3 -V
Python 3.6.7
$ touch pyhash.py

Building the Tool

import os
import hashlib
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("file")
args = parser.parse_args()
md5 = hashlib.md5()
sha1 = hashlib.sha1()
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

echo "This is a test file!" > test.txt
$ python3 pyhash.py test.txt
test.txt ff22941336956098ae9a564289d1bf1b 3c1bb0cd5d67dddc02fae50bf56d3a3a4cbc7204

Conclusion

Cybersecurity Analyst and Finance Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store