46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import hashlib
|
|
import os
|
|
|
|
# import time
|
|
from rq import get_current_job
|
|
|
|
BLOCKSIZE = 65535
|
|
|
|
|
|
def set_meta(job, message):
|
|
job.meta["status"] = message
|
|
job.save_meta()
|
|
|
|
|
|
def check_hash(algorithm, filename, hashval):
|
|
job = get_current_job()
|
|
if job:
|
|
if filename == "" or hashval == "":
|
|
set_meta(job, "FAILED: Invalid Job")
|
|
return
|
|
|
|
if algorithm == "sha256":
|
|
hashfunc = hashlib.sha256()
|
|
else:
|
|
set_meta(job, "FAILED: Unknown Algorithm")
|
|
return
|
|
|
|
filesize = os.path.getsize(filename)
|
|
filepos = 0
|
|
with open(filename, "rb") as f:
|
|
file_buffer = f.read(BLOCKSIZE)
|
|
filepos += len(file_buffer)
|
|
set_meta(job, "Processing: {}%".format(filepos / filesize * 100))
|
|
while len(file_buffer) > 0:
|
|
hashfunc.update(file_buffer)
|
|
file_buffer = f.read(BLOCKSIZE)
|
|
|
|
set_meta(job, "Processing: {}%".format(filesize / filesize * 100))
|
|
|
|
# time.sleep(5)
|
|
|
|
if hashfunc.hexdigest() == hashval:
|
|
set_meta(job, "OK: Hash Value Matches")
|
|
else:
|
|
set_meta(job, "FAILED: Invalid Hash")
|