md5sum

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


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


Solution

  • The usual definition of "MD5 hash of a file" is that the hash is based on the file contents. The name can be freely changed.

    $hash1 = md5(file);
    // change file name
    $hash2 = md5(file);
    

    The two hash codes will be the same.

    In some (fairly specialized) use cases, file metadata (name, time stamp(s), etc.) are part of the data used to compute the hash. Then

    $hash1 = md5(file);
    // change file name
    $hash2 = md5(file);
    

    will produce two separate hashes.