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