mattmccj
mattmccj

Reputation: 11

Calculating CRC32 checksum on string

I am in the process of bringing an API up from 2.7 to 3.8. It is built to communicate with an agent over a TCP socket. I am having an issue with a checksum not being calculated right. I get a message back from my agent that the Checksum is bad.

I know it is not my agent because the CRC check on header only packets are being accepted and the commands are executed correctly on the agent side.

The line of code I am having an issue with is:

body += format(struct.pack("=L", binascii.crc32(format(body).encode()) & 0xFFFFFFFF))

Previously on 2.7 this line of code had no encoding/formatting.

Does anyone know what I am doing wrong?

I have a strong feeling it pertains to the encoding of 'body string'. After breaking the line of code down to its components I confirmed that the int output of binascii.crc32() is different between 3.8 and 2.7 and doing a bit of reading on the variety of byte/char types there are I have become quite lost.

Upvotes: 1

Views: 821

Answers (1)

mattmccj
mattmccj

Reputation: 11

so the correct line of code is to append the checksum and body to the packet buffer simultaneously rather than add the checksum to the body and then add the body to the packet buffer. this avoids a decoding stage that was causing an issue.

buf = buf + format(body).encode() + struct.pack("=L", binascii.crc32(format(body).encode()) & 0xFFFFFFFF)

the original would output: '{"datasetName": "X_train", "fileName": "/test/MNIST/X_train_uint8.h5"}b\'y\\xf8D\\xec\''

the correct solution seen in this answer outputs: '{"datasetName": "X_train", "fileName": "/test/MNIST/X_train_uint8.h5"}y\xf8D\xec'

Upvotes: 0

Related Questions