简体   繁体   English

MD5 哈希不会在 Linux 上产生正确的哈希

[英]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;我正在尝试使用 C 中的 md5 散列算法(取自此处的代码)对一些字符串进行散列,但似乎无法在我的 Ubuntu 虚拟机上运行; I get completely different hash for every string.我为每个字符串得到完全不同的哈希值。

Exactly the same code works just fine on Windows 10 ( using this site as a reference ).完全相同的代码在 Windows 10 上运行良好(使用此站点作为参考)。 I'm compiling with gcc on both oss.我在两个 oss 上都用 gcc 编译。

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在 Windows 上:c320d73e0eca9029ab6ab49c99e9795d

On linux: bbd22e6dfadec16827873f1a22adf991在 linux 上:bbd22e6dfadec16827873f1a22adf991

On the website: c320d73e0eca9029ab6ab49c99e9795d在网站上:c320d73e0eca9029ab6ab49c99e9795d

edit 2:编辑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];
}

On 64-bit compilations a long is 32-bits in Windows, while 64-bits on Linux.在 64 位编译中,long 在 Windows 中为 32 位,而在 Linux 中为 64 位。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM