TyrantUT
TyrantUT

Reputation: 29

ZLIB seems to be returning CRC32B not CRC32 in C

I have quite a simple program to compute the CRC32 of input strings from stdin and for some reason I am not getting the CRC32, but the CRC32B.

Here is my code

int main( int argc, char *argv[] ) { 

  unsigned long crc=0L;
  unsigned char *stdinput = malloc(1024);

  crc = crc32( 0L, Z_NULL, 0 );

  fgets(stdinput, 1024, stdin);

  crc = crc32( crc, stdinput, strlen(stdinput) - 1 );
  printf("%s 0x%08x\n", stdinput, crc );

}

I know there are overflow problems in the program, but that's not necessarily my issue.

The problem is the output is like so

echo test | ./crc32 results in 0xd87f7e0c

and not 0xaccf8b33 Verified here https://www.tools4noobs.com/online_tools/hash/

The output from zlib is definitely using CRC32B and not CRC32.

How would I modify this so I get the correct output?

I'm running this on a Debian 64 bit machine.

Any help with this would be greatly appreciated. Thank you.

Upvotes: 0

Views: 1067

Answers (1)

user149341
user149341

Reputation:

The results you're getting from your code are correct.

The web site you're referencing is using the (obsolete) mhash library to hash user input, whose "crc32" implementation uses an uncommon polynomial typically only used for Ethernet checksums. The hash implemented by mhash as "crc32b" is, in reality, the one typically referred to as CRC32.

Upvotes: 1

Related Questions