如何使用 Python 计算文件系统目录的哈希值? [英] How can I calculate a hash for a filesystem-directory using Python?

查看:35
本文介绍了如何使用 Python 计算文件系统目录的哈希值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码来计算文件的哈希值:

I'm using this code to calculate hash value for a file:

m = hashlib.md5()
with open("calculator.pdf", 'rb') as fh:
    while True:
        data = fh.read(8192)
        if not data:
            break
        m.update(data)
    hash_value = m.hexdigest()

    print  hash_value

当我在文件夹文件夹"上尝试时,我得到了

when I tried it on a folder "folder"I got

IOError: [Errno 13] Permission denied: folder

如何计算文件夹的哈希值?

How could I calculate the hash value for a folder ?

推荐答案

这个 Recipe 提供了一个很好的功能来完成您的要求.我已将其修改为使用 MD5 哈希,而不是 SHA1,正如您最初的问题所问

This Recipe provides a nice function to do what you are asking. I've modified it to use the MD5 hash, instead of the SHA1, as your original question asks

def GetHashofDirs(directory, verbose=0):
  import hashlib, os
  SHAhash = hashlib.md5()
  if not os.path.exists (directory):
    return -1

  try:
    for root, dirs, files in os.walk(directory):
      for names in files:
        if verbose == 1:
          print 'Hashing', names
        filepath = os.path.join(root,names)
        try:
          f1 = open(filepath, 'rb')
        except:
          # You can't open the file for some reason
          f1.close()
          continue

        while 1:
          # Read file in as little chunks
          buf = f1.read(4096)
          if not buf : break
          SHAhash.update(hashlib.md5(buf).hexdigest())
        f1.close()

  except:
    import traceback
    # Print the stack traceback
    traceback.print_exc()
    return -2

  return SHAhash.hexdigest()

你可以这样使用它:

print GetHashofDirs('folder_to_hash', 1)

输出看起来像这样,因为它对每个文件进行了哈希处理:

The output looks like this, as it hashes each file:

...
Hashing file1.cache
Hashing text.txt
Hashing library.dll
Hashing vsfile.pdb
Hashing prog.cs
5be45c5a67810b53146eaddcae08a809

这个函数调用的返回值作为散列返回.在这种情况下,5be45c5a67810b53146eaddcae08a809

The returned value from this function call comes back as the hash. In this case, 5be45c5a67810b53146eaddcae08a809

这篇关于如何使用 Python 计算文件系统目录的哈希值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆