35 lines
595 B
Python
35 lines
595 B
Python
import hashlib
|
|
import os
|
|
from io import BytesIO
|
|
|
|
|
|
def hash_file(f: BytesIO, buffer_size=65536, algo='SH1') -> str:
|
|
"""
|
|
Generate the MD5 Hash of the file and return
|
|
|
|
f of
|
|
buffer_size in MB
|
|
|
|
"""
|
|
|
|
algorithm = ['MD5', 'SH1']
|
|
assert algo in algorithm
|
|
|
|
if algo == 'MD5':
|
|
algo = hashlib.md5()
|
|
else:
|
|
algo = hashlib.sha1()
|
|
|
|
buffer_size = buffer_size * 1024 * 1024
|
|
|
|
while True:
|
|
data = f.read(buffer_size)
|
|
if not data:
|
|
break
|
|
algo.update(data)
|
|
|
|
algohex = algo.hexdigest()
|
|
f.seek(0)
|
|
|
|
return algohex
|