Picture of mitop
Registered 3 years 151 days
mitop Monday, 13 February 2023, 12:42 PM
crc32 checksum creation for Javascript Service side
Hi all,

does anybody know where i can find the crc32 function for generating a checksum on javascript service side?
The c++ side do have this functionality, https://sdk.innovaphone.com/13r3/common/ilib/crc.htm , what i try was to implement my own crc32 function for javascript service side like here https://www.webtoolkit.info/javascript_crc32.html#.Y-n_E63MIUE but the values generated are not the same. So i stopped at this point, and decided to ask here if there is a crc32 function for javascript service side that generates the same checksums as the c++ version of the sdk.


Best regards
Mladen Topic


Andreas Fink
Moderator Registered 12 years 277 days
Andreas Fink (innovaphone) Monday, 20 February 2023, 05:59 PM
Re: crc32 checksum creation for Javascript Service side
Hello Mladen,

i just tried a JavaScript implementation from this thread and it works fine in the JavaScript App Service:


var makeCRCTable = function(){
var c;
var crcTable = [];
for(var n =0; n < 256; n++){
c = n;
for(var k =0; k < 8; k++){
c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
//log(c);
crcTable[n] = c;
}
return crcTable;
}
var crcTable = makeCRCTable();

var crc32 = function(str) {

var crc = 0 ^ (-1);

for (var i = 0; i < str.length; i++ ) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
}

return (crc ^ (-1)) >>> 0;
};

var input = "123456789";
var output = crc32(input);
log(input + " crc32 hex: " + output.toString(16));
Output:
123456789 crc32 hex: cbf43926


In the C++ library for App Services i have the same result:
dword checksum = 0;
const char * input = "123456789";
checksum = Crc::crc32(checksum, input, 9);
debug->printf("%x", checksum);
Output: cbf43926


It seems to match also the output of the following Python library:
https://crcmod.sourceforge.net/crcmod.html


It would be great if you post a complete example with input/output for your JavaScript and C++ implementation, and implementation of the calculation itself.

Best Regards
Andreas Fink
Picture of mitop
Registered 3 years 151 days
mitop Tuesday, 21 February 2023, 03:27 PM
Re: crc32 checksum creation for Javascript Service side
Hello Andreas,

thank you for your reply. I do have exactly the same implementation of the crc32 generation on service side like you have. As I look at your example I notice what is different in my output/use of values.On C++ side i convert the returned "dword checksum" with "sprintf(buffer, "%u", checksum)", and not with "%x". That is where the different output comes from, sorry this was my fault, have probably not considered the returned types. A few minutes ago i tried to convert the value on javascript side to match the value from c++ that was generated with "%u", but I have not yet been successful.

Anyway, the crc32 generation works as expected, both of our implementations are in perfect agreement. If you know how to convert the generated javascript hex strings to (unsigned) integer values (like on C++ side with "%u") I would be very grateful again.

Thank you very much.


Best regards
Mladen Topic
Andreas Fink
Moderator Registered 12 years 277 days
Andreas Fink (innovaphone) Tuesday, 21 February 2023, 06:14 PM
Re: crc32 checksum creation for Javascript Service side
Hello Mladen,

in my example i converted a Number to hex via output.toString(16).

If you leave toString(16) and just use output it should be fine.

...
log(input + " crc32 dec: " + output);
Output: 123456789 crc32 dec: 3421780262

Best Regards
Andreas Fink


← You can define your color theme preference here