Skip to content

invalid checksum is possible if bytes are written separately #27

@DonBrinn

Description

@DonBrinn

There is a defect in the crc module used.

Because of that underlying defect, writing the same list of bytes to crc32-stream can result in a different checksum value depending on how the list of bytes is split across different stream.write() calls.

Consider the following:

    const checksum = new CRC32Stream();
    const deadend = new DeadEndStream();
    
    checksum.on('end', function() {
      console.log(checksum.digest().readUInt32BE(0);
      done();
    });
    
    checksum.write(Buffer.from([157, 10, 217, 109]));
    checksum.write(Buffer.from([100, 200, 300]));
    checksum.end();
    checksum.pipe(deadend);

and

    const checksum = new CRC32Stream();
    const deadend = new DeadEndStream();
    
    checksum.on('end', function() {
      console.log(checksum.digest().readUInt32BE(0);
      done();
    });
    
    checksum.write(Buffer.from([157, 10, 217, 109, 100, 200, 300]));
    checksum.end();
    checksum.pipe(deadend);

Those should result in the same checksum value, but the first snippet results in a checksum value of 2178592166 (which is incorrect) and the second snippet results in a checksum value of 2170850123 (which is correct).

Suggested fix: Switch to using the crc-32 module instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions