Fixed file hash with sha512

181 Views Asked by At

I am using this code to hash files however the hashes changes each time I run the script... How to have a fixed hash please ? Maybe there is a random seed, i don't want any for this.

I just have a list of files in folder and I need their unique and fixed hash for each one :

import sys
import os
import hashlib

# BUF_SIZE is totally arbitrary, change for your app!
BUF_SIZE = 65536  # lets read stuff in 64kb chunks!

files = [f for f in os.listdir('.') if os.path.isfile(f)]

sha512 = hashlib.sha512()

hashes = []

for file in files:
    with open(file, 'rb') as f:
        while True:
            data = f.read(BUF_SIZE)
            if not data:
                break
            sha512.update(data)
    hashes.append(file+"|"+sha512.hexdigest())

with open("hash.txt", 'w+') as f:
    for h in hashes:
        f.write(h+'\n')
        print(h)

Output is a file with each filename and file hash. All file hash must be the same each time I run the script (not the case rn)

0

There are 0 best solutions below