1

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];
}
Community
  • 1
  • 1
  • 2
    How are you transferring the strings to Linux? Did you change the line endings? – stark Mar 24 '20 at 13:32
  • Do you have some examples? – glglgl Mar 24 '20 at 13:33
  • there are some possible traps: 1.strings conding, and line endings. remember, under windows line end has two characters CR+LF. 2. do some simple tests and print how many bytes are assigned to used integer types. maybe it is the difference. those two points are common traps for multiplatform programming. – Znik Mar 24 '20 at 13:37
  • @glglgl I added in the edit – Eugene Schwartz Mar 24 '20 at 13:42
  • Are you sure the code is the same? Did you download it from somewhere you can point us at, or did you edit it out of the RFC? Does your Linux implementation give the right answers for the test vectors at the bottom of the RFC? – Rup Mar 24 '20 at 14:42
  • @Rup Code on both machines is exactly the same; makefile too. I just copied out stuff from RFC directly. Only thing I changed is instead of printing the hash to save it in a buffer. No it doesn't pass the test. Every single one is wrong. – Eugene Schwartz Mar 24 '20 at 14:58
  • Please post the code for Linux. – stark Mar 24 '20 at 17:37
  • @stark code is pretty much direct copy from RFC (first link in my post) with one slightly changed function, which I added now in the edit. And that same code on W10 generates correct hashes. – Eugene Schwartz Mar 24 '20 at 18:10
  • Code has obvious issues like `typedef unsigned long int UINT4;` – stark Mar 24 '20 at 18:38

1 Answers1

2

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
stark
  • 12,615
  • 3
  • 33
  • 50