• 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 187

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 187
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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.

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.

  • 8
Reply Report
      • 1
    • My guess is that file_get_contents() is behaving differently on the different platforms. Possibly a locale-specific thing? I'm not sure how you could test that hypothesis, too :-/
      • 2
    • Your "answer" is now just to hash the filename instead of the content. What's the point of that? I would strongly expect that you really want to hash the content instead.
    • @JonSkeet: Exactly filename is not an issue , issue is actually generating hash. from Filename if you mean md5_file($image); then this isnt actually file name that is hashed, its actually file it self.

Windows and Linux has different line endings, \r\n and \n. So when the file is read, the 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.

  • 7
Reply Report
      • 1
    • Thanks alot mate you actually technically guided me on what the problem is , but i still want solution about the problem.

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"))
  • 0
Reply Report

Might look at the page for fopen() about how to avoid line endings problems but it basically comes down to using 'wb' and 'rb' when writing and reading the file. Here's the link to the fopen page.

Does mean probably can't use file_get_content() as doesn't seem to have option to set read mode.

  • -1
Reply Report