Jazmaina
Jazmaina

Reputation: 7

Python: How to create one hash value from two files

I'm looking to create a hash from two files but not sure how to do it.

I'm able to hash one file at a time by using the below code.

#functions: create sha384 hash
def create_hash(folder, filename):
    BLOCKSIZE = 65536
    hasher = hashlib.sha384()
    with open(folder + filename, 'rb') as afile:
        buf = afile.read(BLOCKSIZE)
        while len(buf) > 0:
            hasher.update(buf)
            buf = afile.read(BLOCKSIZE)
    #print(hasher.hexdigest())
    return BLOCKSIZE, hasher

BLOCKSIZE, hasher = create_hash(ARCHIVE_FOLDER, ARCHIVE_FILENAME)

Ideally I'd like to make a tweak to this so that it will take two files and generate one hash of the two files? Not sure if that's actually possible.

Edit: I've updated the code as follows. It runs but I'm only getting the following output and I'm not sure why. Anyone have any insights?

combined: built-in method hexdigest of _hashlib.HASH object at 0x000001DFFC9929B8

#functions: create sha384 hash
def create_hash2(folder, filename1, filename2):
    BLOCKSIZE = 65536
    hasher = hashlib.sha384()
    with open(folder + filename1, 'rb') as afile, open(folder + filename2, 'rb') as bfile:
        buf = afile.read(BLOCKSIZE) and bfile.read(BLOCKSIZE)
        while len(buf) > 0:
            hasher.update(buf)
            buf = afile.read(BLOCKSIZE) and bfile.read(BLOCKSIZE)
    #print(hasher.hexdigest())
    return BLOCKSIZE, hasher

        #test
        BLOCKSIZE, hasher = create_hash2(ARCHIVE_FOLDER, '1--2019-06-13-archive.zip', '2--2019-06-13-archive.zip')
        print('combined: ' + str(hasher.hexdigest))

Upvotes: 1

Views: 119

Answers (1)

NPE
NPE

Reputation: 500913

You could just loop over the files:

def create_hash(folder, filenames):  # takes an arbitrary number of filenames
    BLOCKSIZE = 65536
    hasher = hashlib.sha384()
    for filename in filenames:
        with open(folder + filename, 'rb') as afile:
            buf = afile.read(BLOCKSIZE)
            while len(buf) > 0:
                hasher.update(buf)
                buf = afile.read(BLOCKSIZE)
    return BLOCKSIZE, hasher

BLOCKSIZE, hasher = create_hash(ARCHIVE_FOLDER, (ARCHIVE_FILENAME_1, ARCHIVE_FILENAME_2))

Note that the order in which you supply the filenames is important (i.e. if you swap the two filenames round, the hash will almost certainly be different).

Upvotes: 1

Related Questions