gpt4 book ai didi

Python 递归 Hashlib

转载 作者:太空宇宙 更新时间:2023-11-04 11:08:59 24 4
gpt4 key购买 nike

我在计算/bin/* 目录下所有文件的所有校验和时遇到问题。我正在用 Python 实现 HIDS,所以我需要计算每个文件的校验和并将其保存在列表中……所以我这里的代码只返回/bin/* 目录的第一个校验和。

import sys
import haslib
path = sys.argv[1] #PATH OF THE FILES, ex: /etc/shadow, /bin/*, etc.

with open(path,'rb') as fh:
md5 = hashlib.md5()
while True:
data = fh.read(8192)
if not data:
break
md5.update(data)
print md5.hexdigest()

有什么建议吗??

最佳答案

import sys
from os import listdir
from os.path import isfile, join
import hashlib
path = sys.argv[1] #PATH OF THE FILES, ex: /etc/shadow, /bin/*, etc.
files = [ f for f in listdir(path) if isfile(join(path,f)) ]
my_files = {}
for fil in files:
with open(fil,'rb') as fh:
md5 = hashlib.md5()
while True:
data = fh.read(8192)
if not data:
break
md5.update(data)
my_files[fil] = md5.hexdigest()
for k,v in my_files.iteritems():
print 'file_name is {} | hash is {}'.format(k,v)

关于Python 递归 Hashlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24074841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com