CRC32 Checksum Corruption Detection

Verify payload integrity with java.util.zip.CRC32: chunked update calls produce the same value as one call, and any single flipped byte shifts the checksum.

JavaData Integrity

Code

snippet
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;

public class ManifestChecksum {

    public static void main(String[] args) {
        byte[] payload = "deploy manifest v7\nartifact=app.jar\n".getBytes(StandardCharsets.UTF_8);

        CRC32 checksum = new CRC32();
        checksum.update(payload);
        long sent = checksum.getValue();
        System.out.println("sent:   " + sent);

        // 接收端分块重算:CRC 是流式算法,分块 update 与一次性 update 结果一致
        CRC32 verify = new CRC32();
        verify.update(payload, 0, 10);
        verify.update(payload, 10, payload.length - 10);
        System.out.println("verify: " + verify.getValue());
        System.out.println("match:  " + (sent == verify.getValue()));

        // 单字节翻转必然改变校验值:corruption 在接收端重算时被检出
        payload[3] ^= 0x01;
        CRC32 corrupted = new CRC32();
        corrupted.update(payload);
        System.out.println("corrupted match: " + (sent == corrupted.getValue()));
    }
}

Inside the snippet

CRC32 Checksum Corruption Detection comes down to the class ManifestChecksum and the function main.

Putting it to use

The demo output is labeled (sent:, verify:, match:), so every printed line maps back to its originating operation.

Core lines

byte[] payload = "deploy manifest v7\nartifact=app.jar\n".getBytes(StandardCharsets.UTF_8);
verify.update(payload, 10, payload.length - 10);
long sent = checksum.getValue();

Interactive Tools for This Topic

FAQ

What does CRC32 Checksum Corruption Detection print when run?
Run it as a complete program — the output lines carry labels such as `sent:`, `verify:`, `match:`.