0

我在计算 /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()

有什么建议么 ??

4

1 回答 1

1
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)
于 2014-06-06T05:42:51.320 回答