7

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
11

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.

  • 1
    In general, all hashes have collisions. A non-broken cryptographic hash implies that there is no efficient way to generate a collision. – Tyson Williams Jan 23 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. – Gilles 'SO- stop being evil' Jan 23 at 22:23
  • 1
    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. – Tyson Williams Jan 23 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. – Gilles 'SO- stop being evil' Jan 23 at 22:33
  • 1
    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." – Tyson Williams Jan 23 at 22:36
8

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
    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 '15 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.