Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crc32 gives incorrect checksum when seed is 0 #66

Closed
DonBrinn opened this issue Nov 18, 2020 · 1 comment
Closed

crc32 gives incorrect checksum when seed is 0 #66

DonBrinn opened this issue Nov 18, 2020 · 1 comment

Comments

@DonBrinn
Copy link

Consider the following:

const {crc32} = require('crc');
> crc32([1], 0);
2298007401
> crc32([1], undefined);
2768625435

A seed value of 0 should be treated the same as no seed value (i.e seed value undefined).

2768625435 is the value that should be returned from the call to crc32([1], 0).

@alexgorbatchev
Copy link
Owner

alexgorbatchev commented Jan 1, 2022

Hi @DonBrinn, I believe this is the expected behavior. The default initial value for CRC32 is 0xFFFFFFFF, which is -1. This could be verified over at http://www.sunshine2k.de/coding/javascript/crc/crc_js.html for example. CRC32 of helloworld is 0xF9EB20AD with default CRC32 Initial Value of 0xFFFFFFFF. Changing initial value to 0 produces 0xE59EB724.

I've also verified this with Ruby:

require 'zlib'

puts("no initial value   : #{(Zlib::crc32('helloworld')).to_s(16)}")
puts("with initial value : #{Zlib::crc32('world', Zlib::crc32('hello')).to_s(16)}")
>
no initial value   : f9eb20ad
with initial value : f9eb20ad

The same output is produced by node-crc CRC32 function:

const together = crc32(Buffer.from('helloworld'));
let separately = crc32(Buffer.from('hello'));
separately = crc32(Buffer.from('world'), separately);
expect(separately).to.equal(together);

console.log({
  crc: crc32.model,
  together: together.toString(16),
  separately: separately.toString(16),
});
>
{ crc: 'crc-32', together: 'f9eb20ad', separately: 'f9eb20ad' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants