Hashlib 的 md5 为相同的输入产生不同的输出
Hashlib's md5 produces different output for same input
我正在尝试编写一个脚本,为指定根目录中的所有文件名和目录名生成哈希值。
到目前为止,这是我的脚本:
import hashlib
import os
import sys
class Hasher:
def __init__(self):
self.hash_func = hashlib.md5()
def hash_file(self, file_path):
with open(file_path, "rb") as file:
self.hash_func.update(file.read())
return self.hash_func.digest()
def hash_dir(self, dir_path):
for dirpath, dirnames, filenames in os.walk(dir_path):
self.hash_func.update(dirpath.encode("utf-8"))
for file_path in filenames:
self.hash_func.update(file_path.encode("utf-8"))
return self.hash_func.digest()
hasher = Hasher()
root_dir = "D:/folder/"
hash_1 = str(hasher.hash_dir(root_dir))
hash_2 = str(hasher.hash_dir(root_dir))
print(hash_1)
print(hash_2)
出于某种原因,它为同一目录生成两个不同的哈希值,而目录没有任何变化。如果目录保持不变,如何才能生成相同的哈希值?
问题是 hashlib.md5
对象每次都被重复使用,所以你 return 对累积数据进行散列,而不仅仅是 last/intended 数据。
你可以通过每次创建一个新的 Hasher
对象来解决这个问题(所以在这种情况下调用 Hasher().hash_dir(root_dir)
两次)。但是由于您的 Hasher
class 除了 md5
对象和两个可能是静态的方法之外不包含任何其他数据,我建议将两个 class 方法都设为静态,并且在方法本身中创建 hashlib.md5
对象:
import hashlib
import os
class Hasher:
@staticmethod # make it a static method
def hash_file(file_path): # no 'self' as first argument
hash_func = hashlib.md5() # create the hashlib.md5 object here
with open(file_path, "rb") as file:
hash_func.update(file.read())
return hash_func.digest()
@staticmethod # make it a static method
def hash_dir(dir_path): # no 'self' as first argument
hash_func = hashlib.md5() # create the hashlib.md5 object here
for dirpath, _, filenames in os.walk(dir_path):
hash_func.update(dirpath.encode("utf-8"))
for file_path in filenames:
hash_func.update(file_path.encode("utf-8"))
return hash_func.digest()
def main():
root_dir = "D:/folder/"
hash_1 = str(Hasher.hash_dir(root_dir))
hash_2 = str(Hasher.hash_dir(root_dir))
print(hash_1)
print(hash_2)
if __name__ == "__main__":
main()
我正在尝试编写一个脚本,为指定根目录中的所有文件名和目录名生成哈希值。 到目前为止,这是我的脚本:
import hashlib
import os
import sys
class Hasher:
def __init__(self):
self.hash_func = hashlib.md5()
def hash_file(self, file_path):
with open(file_path, "rb") as file:
self.hash_func.update(file.read())
return self.hash_func.digest()
def hash_dir(self, dir_path):
for dirpath, dirnames, filenames in os.walk(dir_path):
self.hash_func.update(dirpath.encode("utf-8"))
for file_path in filenames:
self.hash_func.update(file_path.encode("utf-8"))
return self.hash_func.digest()
hasher = Hasher()
root_dir = "D:/folder/"
hash_1 = str(hasher.hash_dir(root_dir))
hash_2 = str(hasher.hash_dir(root_dir))
print(hash_1)
print(hash_2)
出于某种原因,它为同一目录生成两个不同的哈希值,而目录没有任何变化。如果目录保持不变,如何才能生成相同的哈希值?
问题是 hashlib.md5
对象每次都被重复使用,所以你 return 对累积数据进行散列,而不仅仅是 last/intended 数据。
你可以通过每次创建一个新的 Hasher
对象来解决这个问题(所以在这种情况下调用 Hasher().hash_dir(root_dir)
两次)。但是由于您的 Hasher
class 除了 md5
对象和两个可能是静态的方法之外不包含任何其他数据,我建议将两个 class 方法都设为静态,并且在方法本身中创建 hashlib.md5
对象:
import hashlib
import os
class Hasher:
@staticmethod # make it a static method
def hash_file(file_path): # no 'self' as first argument
hash_func = hashlib.md5() # create the hashlib.md5 object here
with open(file_path, "rb") as file:
hash_func.update(file.read())
return hash_func.digest()
@staticmethod # make it a static method
def hash_dir(dir_path): # no 'self' as first argument
hash_func = hashlib.md5() # create the hashlib.md5 object here
for dirpath, _, filenames in os.walk(dir_path):
hash_func.update(dirpath.encode("utf-8"))
for file_path in filenames:
hash_func.update(file_path.encode("utf-8"))
return hash_func.digest()
def main():
root_dir = "D:/folder/"
hash_1 = str(Hasher.hash_dir(root_dir))
hash_2 = str(Hasher.hash_dir(root_dir))
print(hash_1)
print(hash_2)
if __name__ == "__main__":
main()