Different hash value created on windows,linux and Mac for same image Different hash value created on windows,linux and Mac for same image linux linux

Different hash value created on windows,linux and Mac for same image


Windows and Linux has different line endings, \r\n and \n. So when the file is read, the content of files is different.

Try uploading Text file with no new line or a Binary file. Also check difference in bytes read. It should be equal to number of new lines in next file.


Ok i found answer to my question, I still dont know why there are two different hashes been generated for the same code in windows and Linux

move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newname);     "Stored in: " . "upload/" . $_FILES["file"]["name"];    $image = "upload/" . $newname;    $sign = md5(file_get_contents($image));//This is code block that i was implmenting before solution

What i tried here was i replaced my above code with following code

 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newname);        "Stored in: " . "upload/" . $_FILES["file"]["name"];        $image = "upload/" . $newname;        $sign = md5_file($image);// Changed here

From this i think Hash values may be same when generated by md5() but if this function accepts file as input then hash values are calculated differently, i dont know if this is a PHP side issue or really OS level issue but if i go on with using md5_file() for generating hash of file i dont get different hash.


To get the same hash as what's produced in a mac using something like this command:

shasum -a 256 -p {filename} | cut -d' ' -f1"

Replace {filename} with the name of your file to hash on the mac. | cut -d' ' -f1 cuts off the filename from the end of what's returned as were only interested in the hash.

On Windows to get the same hash you need to do this:

Get a stream of your file and call this function I made:

public static string GetSha256Checksum(this Stream stream)    {        stream.Rewind();        using (var sha256 = SHA256Cng.Create())        {            return string.Concat(                sha256.ComputeHash(stream)                    .Select(item => item.ToString("x2"))            );        }    }

This is what's fundamentally different when creating a hash to be the same as a mac:

string.Concat(sha256.ComputeHash(stream)              .Select(item => item.ToString("x2"))