Cannot properly generate crc32c checksum in Node.js

Viewed 370

I'm writing a Node.js application that receives WebSocket messages from a third-party. The messages include a CRC32C checksum value, and I've received this brief intstruction on how to generate the message checksum:

Generate checksum with generate_crc32c algorithm:

checksum = generate_crc32c(messageAsString, strlen);

Where the above value messageAsString is a stringified version of parts of the JSON WebSocket message. The instructions do not explain what the strlen value is, or how it is generated. I'm also not sure if the above is a particular language or psuedo-code. I've tried and failed to get more specific instructions from the third party.

To generate the checksum, I'm using the Node.js module fast-crc32c and very simply doing this:

const crc32c = require('fast-crc32c');

// .. generate `messageAsString` val

const calcChecksum = crc32c.calculate(messageAsString);

The problem is that I've tested several messages, and the checksums never match. Am I missing something about how this checksum value should be generated? Is the strlen value important? I can't find any docs on what it is. Am I most definitely getting the messageAsString value wrong?

1 Answers

I don't believe you're doing anything wrong in the way you're calling the function. I believe the best approach here is to set up a trivial, reproducible test script to compare your CRC32C checksum with 3rd Party calculations.

The strlen value is redundant in Node.js, since strings incorporate a length property and don't rely on explicitly passing the length as in some languages (like c++)

Something like below:

const crc32c = require('fast-crc32c');

const messageAsString = "ABCDEF";
const calcChecksum = crc32c.calculate(messageAsString);

console.log( { checksum: calcChecksum.toString("16") })

I'm getting a result of:

{ checksum: 'a4b7ce68' }

for this.

If we use the (very helpful) site here: https://crccalc.com/, we can see this matches their calculations for the same input.

Once you get this working, I'd suggest inputting the data from your test messages into this site (ensure you don't input highly sensitive information!), and see if they calculate the same checksum as the message has.

Perhaps the messages contain some padding or whitespace that is changing the checksum? You should at least be able to reproduce with the above method. Hope this helps!

Related