-3
item = '04010034587C1F0C6D51B6D33B78CA63C1CC7E5910006C5600000000000000'
'%08X' % (binascii.crc32(binascii.a2b_hex(item)) & 0xffffffff)

I can't get the crc32 to give me the correct 32 bits. I have tried several different algorithms. I have been getting '3228F9E4' with this code, but the correct crc32 should be 'E42FDBEC' according to another program used. The polynomial is '04C11DB7' and the initial remainder is '00000000'.

How can I calculate this correctly?

TWagner
  • 69
  • 1
  • 8
  • 5
    Wich method did you use to get the "correct" CRC, how do you assume that it is correct? – Jaay Feb 11 '15 at 16:28
  • I have a program that runs and generates it already...I need my program to match it – TWagner Feb 11 '15 at 16:30
  • CRC is based on a polynom to be calculated (http://en.wikipedia.org/wiki/Cyclic_redundancy_check), that polynom may vary depending on the system you are running it – Jaay Feb 11 '15 at 16:31
  • Oh yea, sorry I forgot to add.... I am using polynomial 0x04C11DB7... – TWagner Feb 11 '15 at 16:34
  • 2
    I get an error trying to reproduce your results, `TypeError: Odd-length string`. Did you copy/paste properly? – Mark Ransom Feb 11 '15 at 16:40
  • I have tried an online calculator as well and got 'D40CEFE5'...which is still incorrect... I just verified again all values are correct as stated above. – TWagner Feb 11 '15 at 16:44
  • @TWagner, open a python interpreter, import binascii, then cut and paste your two lines. I get `Odd-length string` in python2 and 3. You are missing something. – AShelly Feb 11 '15 at 17:15
  • I don't have binutil...what is that module and how are you using it? – TWagner Feb 11 '15 at 17:19
  • ... yes that is what I am using on python 2.7 – TWagner Feb 11 '15 at 17:25
  • Right, and my point is your code does not run when cut and paste from this question. – AShelly Feb 11 '15 at 17:25
  • Oh sorry, It is working fine for me...as in no errors...and it produces a 32 bit output...it is just not the one I am looking for. – TWagner Feb 11 '15 at 17:30
  • So no one knows how to get that output?.... :/ I am hopeless – TWagner Feb 11 '15 at 19:28
  • 1
    Your hex string has an odd number of characters. _It is wrong!_ A hex string of bytes has to have an even number of hexadecimal digits. What is the _actual_ data that you want a CRC of? _Exactly_ what code produced `E42FDBEC` from what input? You have not provided the necessary information to answer your question. – Mark Adler Feb 11 '15 at 22:20
  • Oh god, well don't I feel dumb...I copied the wrong hex string...But that is the correct hex string now...I apologize for that. Anyways, (edited above) this is now the correct "actual" data I want a CRC32 of. I do not know the 'exact code' that produced E42FDBEC. I want to emulate what something else is already doing. It is a CRC32, with poly, XOR, and initial value stated below. I hope this is better information. Thank you for the reply. – TWagner Feb 11 '15 at 22:28
  • 3
    _Something_ produced the `E42FDBEC`. What was it? – Mark Adler Feb 12 '15 at 00:49
  • 1
    I have faith that `binascii.crc32` is producing the proper CRC based on the information you've given, so the only conclusion is that the other code you're using as a reference is doing it improperly. Without seeing *that* code you have no chance of matching it. – Mark Ransom Feb 12 '15 at 17:42
  • 2
    I tried 192 different CRC algorithms with these 88 hexes from the string with 44 bytes decoded and hex through the CRC as is, for a total of 1152 combinations, hypothetical line endings from files and so on, and matched them against both values and their byteswapped versions and couldn't find a match for among any of them. – Antti Haapala -- Слава Україні Feb 13 '15 at 18:43
  • Furthermore, the code you presented here spits out `A3A75A18` which again looks nothing like your result... – Antti Haapala -- Слава Україні Feb 13 '15 at 18:56
  • Ok, well thank you for trying all that. I appreciate it. I will update when I figure it out. At the moment, I still have no answer. Mark Ransom - The "Something" is code that sends a message and calculates a crc32 based on that message. If I had access to the source code, I could easily look how it was created. Hence why I have this question. – TWagner Feb 13 '15 at 22:22

1 Answers1

2

A particular CRC impelmentation is specified by a polynomial, an an optional initial value and a possible final XOR. The bit order is also significant.

The binutils docs say:

Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This is consistent with the ZIP file checksum

Make sure you are using the correct inital value as your reference implementation. (2nd argument to crc32)

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • The polynomial I used is '04C11DB7' and the initial remainder is '00000000'. – TWagner Feb 11 '15 at 17:00
  • The possible final XOR should be FFFFFFFF....I've also read that this polynomial is somewhat common... How can this be this difficult?... – TWagner Feb 11 '15 at 17:09
  • It's difficult because CRC-32 is more a concept than an implementation specification. There are multiple ways to implement it. The polynomial is probably not the issue, the issues come from how to break down that long string into bytes to feed the algorithm. – AShelly Feb 11 '15 at 17:13
  • does binascii.crc32(binascii.a2b_hex(item)) & 0xffffffff not take 'item' in this case as a string? I'm wondering if there is another way someone knows of that can give me this correct output : 'E42FDBEC' – TWagner Feb 11 '15 at 17:18