运行环境:python3
脚本适用:对于大量的文件进行hash256验证,文件是否正确,hash256保存在txt上,保存格式为hash256 /文件名
执行命令:python xxx.py -f hash256.txt
测试文件:https://download.csdn.net/download/weixin_44975787/87542304
# -*- coding:UTF-8 -*-
import argparse
import hashlib
import os
import time
start = time.time()
hash256_list = list()
error_list = list()
parse = argparse.ArgumentParser()
parse.add_argument('-f', '--filename')
args = vars(parse.parse_args())
with open(args['filename'],'r',encoding='UTF-8') as file:
for i in file.readlines():
if len(i)< 64 :
break
hash256= i.split()[0].lower()
if os.path.exists('docs'):
filename = i.replace(hash256,'').replace('\n','')[3::]
else:
filename = i.split("/",)[-1].replace('\n','')
hash256_list.append([filename,hash256])
for i in hash256_list:
if os.path.exists(i[0]):
with open(i[0],'rb') as file:
hash256 = hashlib.sha256(file.read()).hexdigest()
if hash256 == i[1]:
print('[SUCCESS]'+i[0]+'Hash right')
else:
print('[ERROR]'+i[0]+' Source:'+i[1]+' Target:'+hash256)
error_list.append([i[0], i[1]])
else:
print('[ERROR]'+i[0]+'file was not found')
end = time.time()
if len(error_list) > 64:
mingling = input('shifoujixuzhixing(yes/no)')
else:
mingling = 'over'
if mingling == 'yes' or mingling == 'y' or mingling == 'YES' or mingling == 'Y':
for i in error_list:
with open(i[0],'rb') as file:
hash256 = hashlib.sha256(file.read()).hexdigest()
if hash256 == i[1]:
print('[SUCCESS]'+i[0]+'Hash right')
else:
print('[ERROR]'+i[0]+' Source:'+i[1]+' Target:'+hash256)
elif mingling == 'no' or mingling == 'n' or mingling == 'NO' or mingling == 'N':
print('over')
else:
pass