0

My question is how should I calculate CRC? I know that when you calculate CRC, you have a checksum and you need to append this to the original data, and when you send the data+checksum together, the receiver can detect if there are any errors by simply dividing the data by a polynomial and if there is any remainder other than 0. I don't know how to append since everyone is only talking about using the codes like the following:

crc32 = crcmod.mkCrcFun(0x104c11db7, 0, False, 0xFFFFFFFF)
bytes_read = f.read(BUFFER_SIZE)
this_chunk_crc=crc32(bytes_read)#will return some integer
4
  • what crc? for what? what's crcmod and mkCrcFun? Commented Apr 8, 2022 at 18:09
  • Incidentally if your question is 'how do I go about implementing a crc algorithim in python', the wikipedia page actually contains not only a description of the algorithm but an example implementation in python. If you just want a recommended library, the question is off-topic here. Commented Apr 8, 2022 at 18:12
  • @2e0byo my problem is CRC should be calculated and appended to the end of the data and after that, if anyone (receiver) calculates the CRC for themselves they need to see the remainder of 0. could you verify this statement? Commented Apr 8, 2022 at 19:11
  • crcmod is a library import crcmod Commented Apr 8, 2022 at 19:13

1 Answer 1

2

You're already calculating the CRC. You are apparently asking how to append the CRC, an integer, to your message, a byte string.

You would use crc.to_bytes(4, 'big') to convert the returned CRC to a string of four bytes in big-endian order. You can then append that to your message. E.g.:

msg += crc32(msg).to_bytes(4, 'big')

I picked big-endian order because your CRC is defined with a False in the third argument. If that had been True (a reflected CRC), then I would have picked little-endian order, 'little'.

Having done all that correctly, the CRC of the resulting message with CRC appended will always be the same constant. But not zero in this case. The constant for that CRC definition is 0x38fb2284, or 955982468 in decimal.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for your answer, So it doesn't relate to the message and always would be 955982468 on the receive side. am I right?
Correct........
How can I remove the checksum from the file after the receiver received it? I tried bytes_read -= crc32(bytes_read).to_bytes(4, 'big') without any luck.
And could you please help me with my follow up question here: stackoverflow.com/questions/71837210/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.