Eugene Schwartz
Eugene Schwartz

Reputation: 13

MD5 Hash Doesn't Produce Correct Hash on Linux

I'm trying to hash some strings with md5 hashing algorithm in C (code taken from here), but can't seem to get it working on my Ubuntu vm; I get completely different hash for every string.

Exactly the same code works just fine on Windows 10 (using this site as a reference). I'm compiling with gcc on both oss.

Is there something obvious that I'm missing ?

edit: code example

unsigned char buffer[16];
MDString("some test string" ,buffer);
for(int i = 0; i < 16; i++) printf("%02x" ,buffer[i]);

On windows: c320d73e0eca9029ab6ab49c99e9795d

On linux: bbd22e6dfadec16827873f1a22adf991

On the website: c320d73e0eca9029ab6ab49c99e9795d

edit 2:

void MDString(char * string ,unsigned char * buffer)
{
  MD5_CTX context;
  unsigned char digest[16];
  unsigned int len = strlen (string);

  MD5Init(&context);
  MD5Update(&context ,string ,len);
  MD5Final(digest ,&context);

  for(int i = 0; i < 16; i++)
    buffer[i] = digest[i];
}

Upvotes: 1

Views: 291

Answers (1)

stark
stark

Reputation: 13189

On 64-bit compilations a long is 32-bits in Windows, while 64-bits on Linux. Just changing

typedef unsigned long int UINT4;

to

typedef unsigned int UINT4;

is enough to fix the most glaring issues with the code. It still gives warnings for the old function parameter forms. Output is:

c320d73e0eca9029ab6ab49c99e9795d

Upvotes: 2

Related Questions