2

I am using an implementation of md5 algorithm written in C. It works on strings well. However it gives wrong results on files. I am using the code below to convert the file to char* and then send it to md5 function:

    struct stat st; 

    if (stat(fileName, &st) == 0)
    {

        // read the file into a char* in order to send it to md5 function
        std::string fdata = "";
        std::ifstream filePtr(fileName);
        std::string dummy = "";
        while((st.st_mode & 0100000) && !filePtr.eof())
        {
            filePtr >> dummy;
            fdata += dummy;

        }

        if(st.st_mode & 0100000)
        {
            const char* fileData = fdata.c_str();
            unsigned* d = md5(fileData, strlen(fileData));
            WBunion u;

            for (unsigned j = 0; j < 4; ++j)
            {
                u.w = d[j];
                for (unsigned k = 0; k < 4; ++k)
                {
                    printf("%02x",u.b[k]);
                }
            }
            printf("\n");

        }

Note: The bitwise operation is just to check whether the file is a regular file(not a folder).
What am I doing wrong? Is the overloaded >> operator to read the file the problem?

Thank you in advance,


Link to the implementation of md5 algorithm that I am using: http://rosettacode.org/wiki/MD5#C.2B.2B

anilbey
  • 1,286
  • 3
  • 17
  • 34

1 Answers1

2

filePtr >> fdata clears fdata on each iteration of the loop, which means the string ends up containing the very last word in the file. A correct and simple way to slurp the whole file into an std::string is by using a stringstream.

Also, you must open the file in binary mode for MD5 to work correctly, and call fdata.size() instead of strlen(fileData) in order to handle files with NUL characters.

Community
  • 1
  • 1
user4815162342
  • 104,573
  • 13
  • 179
  • 246