26

Does the hash of a file change if the filename or path or timestamp or permissions change?

$ echo some contents > testfile
$ shasum testfile 
3a2be7b07a1a19072bf54c95a8c4a3fe0cdb35d4  testfile
2

2 Answers 2

33

The hash of a file is the hash of its contents. Metadata such as the file name, timestamps, permissions, etc. have no influence on the hash.

Assuming a non-broken cryptographic hash, two files have the same hash if and only if they have the same contents. The most common such hashes are the SHA-2 family (SHA-256, SHA-384, SHA-512) and the SHA3 family. This does not include MD5 or SHA-1 which are broken, nor a CRC such as with cksum which is not a cryptographic hash.

13
  • 7
    In general, all hashes have collisions. A non-broken cryptographic hash implies that there is no efficient way to generate a collision. Jan 23, 2019 at 15:29
  • @TysonWilliams That's true but irrelevant. If you have two files with the same hash, and the hash is a non-broken cryptographic hash, then the two files have the same hash if and only if they have the same contents. If you could find a collision, it wouldn't be a non-broken cryptographic hash. Jan 23, 2019 at 22:23
  • 4
    What you said is false. Every (practical) hash function, even non-broken cryptographic ones, have collisions. There are more inputs than outputs, so by the pigeonhole principle, there must be a collision. Jan 23, 2019 at 22:29
  • @TysonWilliams But I never claimed that there were no collisions. Of course there are collisions. But 1. it's extremely rare to find collisions for what is generally considered to be a non-broken cryptographic hash function, and 2. if someone did find a collision then that function would no longer be a non-broken cryptographic hash function. In practical terms, if you have two files with the same SHA-256 hash, they have identical contents. Jan 23, 2019 at 22:33
  • 2
    The statement "f you have two files with the same hash, and the hash is a non-broken cryptographic hash, then the two files have the same hash if and only if they have the same contents." is equivalent to "non-broken cryptographic hash [functions] have no collisions". I agree that "In practical terms, if you have two files with the same SHA-256 hash, they have identical contents." Jan 23, 2019 at 22:36
18

Not as far as I can tell after a simple test.

$ echo some contents > testfile
$ shasum testfile 
3a2be7b07a1a19072bf54c95a8c4a3fe0cdb35d4  testfile
$ mv testfile newfile
$ shasum newfile 
3a2be7b07a1a19072bf54c95a8c4a3fe0cdb35d4  newfile
1
  • 3
    But note that if you blindly compare the outputs of shasum, they will not match since the output includes the filename/path (as shown in your example). A good workaround is to do something like shasum - < testfile.
    – DoxyLover
    Aug 3, 2015 at 22:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .