文件hash256验证

运行环境: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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值