Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i am creating Hash values with following code, now what happens is that when i test the hash value on Windows local Xampp server i get hash value which is different for same code that runs on 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));

Now i dont know why is this happening. For the same code that i just pasted above.

EDIT: Opening question again. The solution i found worked only for Linux which means linux and windows now give me same hash but when an image is uploaded from Mac(IOS) it is still generating different Hash.

like image 757
noobie-php Avatar asked May 23 '14 12:05

noobie-php


People also ask

Why are the hashes of the same file different?

So, a Word file and the PDF file published from the Word file may contain the same content, but the HASH value will be different. Even copying the content from one file to another in the same software program can result in different HASH values, or even different file sizes.

Is it possible to get the same exact hash out of two files that are different?

Two files can have the same MD5 hash even if there are different. As the MD5 algorithm can take an infinity of input and give a limited number of output, it's not impossible, even if the probability of collision is very low.

What does different hash values mean?

Hash values can be thought of as fingerprints for files. The contents of a file are processed through a cryptographic algorithm, and a unique numerical value – the hash value - is produced that identifies the contents of the file.

Can a hash value be changed?

When you change a file's name from within Word by saving the file under a new name, you alter the embedded file name and thus change the hash value. You may be changing other embedded metadata as well, but any change within the file is enough to alter the hash value.


2 Answers

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.

like image 89
Sharad D Avatar answered Oct 12 '22 13:10

Sharad D


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.

like image 45
noobie-php Avatar answered Oct 12 '22 13:10

noobie-php