坚持使用 MD5 是个好主意。只是为了确保我将文件长度或块数附加到您的文件哈希表中。
是的,您可能会遇到两个具有相同 MD5 哈希的文件,但这不太可能(如果您的文件大小合适)。因此,将块的数量添加到您的哈希中可能会帮助您减少这种情况,因为现在您必须找到两个大小相同且 MD5 相同的文件。
# This is the algorithm you described, but also returns the number of chunks.
new_file_hash, nchunks = hash_for_tile(new_file)
store_file(new_file, nchunks, hash)
def store_file(file, nchunks, hash):
"" Tells you whether there is another file with the same contents already, by
making a table lookup ""
# This can be a DB lookup or some way to obtain your hash map
big_table = ObtainTable()
# Two level lookup table might help performance
# Will vary on the number of entries and nature of big_table
if nchunks in big_table:
if hash in big_table[hash]:
raise DuplicateFileException,\
'File is dup with %s' big_table[nchunks][lookup_hash]
else:
big_table[nchunks] = {}
big_table[nchunks].update({
hash: file.filename
})
file.save() # or something
为了减少这种可能性,请切换到 SHA1 并使用相同的方法。如果性能不是问题,甚至使用两者(连接)。
当然,请记住,这仅适用于二进制级别的重复文件,而不适用于“相同”但具有不同签名的图像、声音、视频。