blob: 47edb6cb0b2bdff91a15b6f68a0f393cc4c37133 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import hashlib
def feed_file(h, f):
"""Feed data read from an open file into the hashlib instance."""
while True:
data = f.read(65536)
if len(data) == 0:
# end of file
break
h.update(data)
def feed_file_path(h, path):
"""Feed data read from a file (to be opened by this function) into the hashlib instance."""
with open(path, 'rb') as f:
feed_file(h, f)
def file_digest(algorithm, path):
"""Calculate the digest of a file and return it in hexadecimal notation."""
h = algorithm()
feed_file_path(h, path)
return h.hexdigest()
def file_md5(path):
"""Calculate the MD5 checksum of a file and return it in hexadecimal notation."""
return file_digest(hashlib.md5, path)
|