Will changing a file name affect the MD5 Hash of a file?

隐身守侯 提交于 2019-12-18 10:18:10

问题


Will changing a file name effect the MD5 Hash of a file?


回答1:


Only if the file's name was included in the hash calculation. e.g., in pseudo-code:

$hash1 = md5(contents of file);
$hash2 = md5(name of file + contents of file);

will produce two seperate hashes.




回答2:


No, the hash is of the file contents only. You can see this in the source for md5sum and its MD5 implementation. You can also test this if you have access to md5sum:

$ echo "some arbitrary content" > file1
$ cp file1 file2
$ md5sum file1
f0007cbddd79de02179de7de12bec4e6  file1
$ md5sum file2
f0007cbddd79de02179de7de12bec4e6  file2
$



回答3:


In Linux using EXT filesystem, it will not, because a file name is not stored in a file, it is stored in the directory entry (dentry) that the file lives in, where the inode of the file is then mapped to a name. Changing a filename will have no affect on its md5sum in Linux. In Windows, I cannot be sure.




回答4:


If the hash is computed from the file contents, it shouldn't.




回答5:


In ESXi (Precisely ESXi 5.5) md5sum on same content but different file names is different. That leads me to believe that VMFS-5 file structure includes file name too. If we are not concerned about file name, Is there a way to check only the md5sum of file content? I couldn't see any option. Any suggestions?




回答6:


1.md5 is calculated based on binary content of the FILE. 2.File name,last modified etc. things are meta data.md5 not really rely on meta-data. I have tested this with below steps,lets work with "last modified" meta-data i)I have created a file named "a.txt" and added some content and created a hash say hash is "xyz" ii)Then I have just added a space in the file and again calculated the hash say it returned "abc" iii)I just removed my change in step (ii),on calculating hash again I have got the initial hash("xyz")

This concludes that even though the metadata of file is changed,the hash remains same till the file content remains unaltered.

Hope it helps.



来源:https://stackoverflow.com/questions/5055143/will-changing-a-file-name-affect-the-md5-hash-of-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!