Search code examples
c++7zipcrc32

C++ checksum CRC32 validation


I am using 7zip CHA32 tool to validate my checksum 32 algorithm. The algorithm gives the correct value if and only if the size of the data buffer is multiple of 4. If the size of the buffer is different, the algorithm gives the proper checksum if I pad an extra '00' byte into the buffer. Are any there any generic C++ algorithms that might work without the contrainsts of the data size ? thanks


Solution

  • CRC-32 doesn't require you to pad your input. For example crc32("abc") and crc32("abc\0") will have different outputs.

    There should be a second function or a parameter that defaults to 0 for the initial value of the hash. This is used for extending the hash. crc32("cd", crc32("ab")) == crc32("abcd"). So just add the last few bytes.

    You will end up with something like this:

    std::uint32_t padded_crc32(const char* data, std::size_t size, std::uint32_t initial_value = 0) {
        const char padding[3] = {0, 0, 0};
        std::uint32_t unpadded_crc32 = crc32(data, size, initial_value);  // Or however you call it
        int padding_size = (4 - (size % 4)) % 4;
        return crc32(padding, padding_size, unpadded_crc32);
    }