郎世权
郎世权

Reputation: 11

The same content file, the MD5 value is different

As we know,the md5 of two different files(even if their contents are same) is different,just like this:

[[email protected] md5test]$ ll
total 16
drwxrwxr-x  7 langshiquan langshiquan 4096 Mar 28 19:41 output
drwxrwxr-x  3 langshiquan langshiquan 4096 Mar 28 19:44 test
-rw-rw-r--  1 langshiquan langshiquan  100 Mar 28 19:54 test.sh
-rw-rw-r--  1 langshiquan langshiquan   69 Mar 28 19:48 test.sh~
[[email protected] md5test]$ sh test.sh
[[email protected] md5test]$ md5sum output.tar
2b7f05590cd4c8665dd61bbf745bbeee  output.tar
[[email protected] md5test]$ sh test.sh
[[email protected] md5test]$ ll
total 18212
drwxrwxr-x  7 langshiquan langshiquan     4096 Mar 28 19:41 output
-rw-rw-r--  1 langshiquan langshiquan 18606080 Mar 28 19:54 output.tar
drwxrwxr-x  3 langshiquan langshiquan     4096 Mar 28 19:44 test
-rw-rw-r--  1 langshiquan langshiquan      100 Mar 28 19:54 test.sh
-rw-rw-r--  1 langshiquan langshiquan       69 Mar 28 19:48 test.sh~
[[email protected] md5test]$ md5sum output.tar
3601eff99bc78198b152b04ca94c53d0  output.tar

test.sh script shell :

#! bin/bash
cp -rp output ./test/
echo "1" > ./test/output/a.txt
tar -cf output.tar ./test/* -C ./

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
Q1.So I think whether there is a hash function similar to MD5,(e.g.i call it just "HashStr") which makes the same "HashStr" for files with the same contents but different attributes.
Q2.Or when we can make TAR, how can we exclude the attributes of the file to make the same MD5?

Thanks in advance.

Upvotes: 0

Views: 3163

Answers (1)

rossum
rossum

Reputation: 15693

If the files are the same, then the hashes will be the same. If the attributes (rather than the file contents) are different, then you need to add the attributes to the file before hashing. Put the attributes you want to include into a piece of text and add it to the file. Then hash attributes + file as a single piece of data.

Upvotes: 2

Related Questions