How to find the SHA-1 hash of a file in Python

1 Answer

0 votes
import hashlib


def hash_file(filename):
    hl = hashlib.sha1()
    with open(filename, 'rb') as file:
        buf = 0
        while buf != b'':
            buf = file.read(1024)
            hl.update(buf)
    return hl.hexdigest()


hf = hash_file("d:\\data.txt")

print(hf)


'''
run:

e409096be250b204802c86b95ee1250232b7e2e9

'''

 



answered Jun 19, 2017 by avibootz

Related questions

2 answers 267 views
1 answer 112 views
2 answers 198 views
1 answer 182 views
1 answer 174 views
...