7

I have some bytes I want to calculate the CRC8 on in python.

I don't have such an experience with that but I know, from the tech specs of my device, that this calculation has to be made with a 0x07 polynom, and a 0x00 initialization.

Let's take an use case. I've received this list of bytes where I know the last one is the CRC:

0x00 0x11 0x23 0x32 0x1C 0xAC 0x23 0x3F 0x25 0x47 0x3D 0xB7 0xE2 0xC5 0x6D 0xB5 0xDF 0xFB 0x48 0xD2 0xB0 0x60 0xD0 0xF5 0xA7 0x10 0x96 0xE0 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xC5 0x8A

Now, how can I calculate the CRC my side in order to check if it corresponds to 0x8A?

I've made some research and tried different python modules like crcmod, crc8 and libscrc but I was not able to make them work: sometimes I've got a MemoryError error on the console!

I've also tried with the following code but it doesn't seem to return me what I think is the correct CRC (0x8a):

import crc8
hash = crc8.crc8()
hash.update("0x001123321CAC233F25473DB7E2C56DB5DFFB48D2B060D0F5A71096E00000000000000000C58A".encode('utf-8'))
print( hash.hexdigest() )

What I'm doing wrong?

Is there anyone with some experience with who can help me? Maybe posting a snippet of code I can use to make the calculation?

However, any help will be appreciated! Thank you so much for your support...

orestino
  • 113
  • 1
  • 1
  • 8
  • If you calculate CRC8 on the entire message including the CRC byte, then the calculated CRC8 should be zero. If you calculate CRC8 on all but the CRC8 byte of the message, you should get CRC8 = 0x8a. – rcgldr Sep 18 '18 at 19:27
  • Thank you @rcgldr! I think my problem is even further upstream: don't know how to solve these memory errors. Do you have some code example in python I can use? – orestino Sep 19 '18 at 08:11
  • I'm not that familiar with Python, much less the libraries for it. I could create something in C, but there should already be examples of CRC8 implemented in C. – rcgldr Sep 19 '18 at 13:13
  • 1
    `crc8.crc8()` takes a `bytes` object. You're giving it a string of hexadecimal digits. In Python 3, you can convert this string to bytes with something like `int('0x1234', 16).to_bytes(2, 'big')` (make sure to set the length correctly). Most likely, whatever is giving you the data is already a `bytes`, and you actually don't need to convert. If you understand the difference between `b'00'` and `b'\x00'`, you will understand what's wrong with your code. – jpkotta Sep 20 '18 at 17:19

2 Answers2

7

0x8a corresponds to a standard CRC-8:

width=8  poly=0x07  init=0x00  refin=false  refout=false  xorout=0x00  check=0xf4  residue=0x00  name="CRC-8"

The Python crc8 you linked to will do exactly what you want.

For example (in Python 3):

hash.update(b'\x00\x11\x23\x32\x1C\xAC\x23\x3F\x25\x47\x3D\xB7\xE2\xC5\x6D\xB5\xDF\xFB\x48\xD2\xB0\x60\xD0\xF5\xA7\x10\x96\xE0\x00\x00\x00\x00\x00\x00\x00\x00\xC5')
print(hash.hexdigest())

gives:

8a

If you include the 8a in the data, then the result is zero.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • Thank you Mark, so I'm wondering why my code doesn't seem to return me the correct (at least I think it is) 0x8A CRC code. Here's the code: import crc8 hash = crc8.crc8() hash.update("0x001123321CAC233F25473DB7E2C56DB5DFFB48D2B060D0F5A71096E00000000000000000C58A".encode('utf-8')) print( hash.hexdigest() ) Is there somenthing I'm doing wrong? – orestino Sep 20 '18 at 14:50
  • Put that code in your question. You seem to have a very basic misunderstanding of what a list of bytes actually is. – Mark Adler Sep 20 '18 at 15:47
  • Thank you Mark, I've placed the code in my question now. Yes I know, I have not such an experience in working with bytes in python. However, what do you mean with _list of bytes_? Do you means I have to use the `bytes` object? – orestino Sep 20 '18 at 16:30
  • What do _you_ mean? You're the one who said "I've received this list of bytes". How did you receive it? – Mark Adler Sep 20 '18 at 19:04
  • The CRC needs to be given the actual binary bytes. Not a hexadecimal representation of the bytes in a text string. – Mark Adler Sep 20 '18 at 19:04
0
import crc8

hash = crc8.crc8()
hash.update("001123321CAC233F25473DB7E2C56DB5DFFB48D2B060D0F5A71096E00000000000000000C5".decode("hex"))
print( hash.hexdigest() )

Output:

8a
Andriy Makukha
  • 7,580
  • 1
  • 38
  • 49
soll soll
  • 17
  • 2