Search code examples
pythonfunctionhashhashlib

Calling argument to function and getting the file hashes


I'm attempting to get the hashes of the file which is the argument supplied. Here is my current code:

import hashlib
import argparse


md5 = hashlib.md5()
sha1 = hashlib.sha1()
sha256 = hashlib.sha256()
BUF_SIZE = 32768

parse = argparse.ArgumentParser()
parse.add_argument("-test", help = 'testing')
args = parse.parse_args()


def hashing(hashThis=args.test):
    with open(hashThis, 'rb') as f:
        while True:
            data = f.read(BUF_SIZE)
            if not data:
                break
        md5.update(data)
        sha1.update(data)
        sha256.update(data)
    #print hashes
    print('MD5: {0}'.format(md5.hexdigest()))   
    print('SHA1: {0}'.format(sha1.hexdigest()))
    print('SHA256: {0}'.format(sha256.hexdigest()))

hashing(hashThis=args.test)

This gives me the following output:

user@user:~/Testing$ python test.py -test test.txt
MD5: d41d8cd98f00b204e9800998ecf8427e
SHA1: da39a3ee5e6b4b0d3255bfef95601890afd80709
SHA256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

The issue is that the hashes given are for an empty file, by using sha256sum of the same file I get

user@user:~/Testing$ sha256sum test.txt
8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4  test.txt

Its not pulling the data from the file, and it works if I use the same code outside of a function. I feel like I'm missing something obvious, but can't figure it out.


Solution

  • You need to be updating the hash objects within the while loop - right now the while loop only exits once 'data' is empty, so all you hash is that empty byte array