0

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)

acnguy2
  • 21
  • 2
  • 1
    Your hash includes the current file and all previous files. Is that what you want? It'll mean the hashes will be different if the files get read in a different order, or if a file is added or changed. – psmears Oct 27 '21 at 10:14
  • Thnaks for your answer. I don't understand. I just have a list of files in folder and I need their unique and fixed hash for each one – acnguy2 Oct 27 '21 at 10:47
  • Try moving the line that starts `sha512 =` to just after the line starting `with` – psmears Oct 27 '21 at 12:38
  • Thanks you !! incredible ! – acnguy2 Oct 27 '21 at 12:52

0 Answers0