CRC32 or something like that in JUCE?

Just wondering if there’s a CRC32 function or similar in JUCE already. Couldn’t find anything, and the forum search also didn’t give me anything I could use.

The idea is to take a MemoryBlock and create some check-number that I can compare with another number to be sure that the file is not corrupted.

EG: piano_sound.sound - at the end of the file I add the CRC32 number, and load everything BUT that number into the MemoryBlock. Now I create the CRC32 from the memory block and check if both matches.

Cheers, WilliamK

Another option would add a parity bit to the stream, but that will make the file much larger. But at the same time, it will be easier and faster to load, I think…

There is one in ZipFile you could check out.

Rail

MD5, SHA-256, or Whirlpool?

https://docs.juce.com/master/group__juce__cryptography-hashing.html

Matt

Thanks guys, I forgot to search for MD5…

anyone know what flags to use to access the crc32 functions? seem to be undefined by default

Sorry to dig this up but it’s one of the very few topics about CRC on this forum and I think my version might help people searching for this.

So just FYI, this is the CRC32 function I use:

static uint32_t calculateCRC32(const juce::Array<uint32_t>& data) {
	uint32_t crc = 0xFFFFFFFF; // initial vector
	for (uint32_t d : data) {
		crc = crc ^ d;
		for (int j = 31; j >= 0; j--) {
			uint32_t mask = (uint32_t)(-((int32_t)(crc & 1)));
			crc = (crc >> 1) ^ (0xEDB88320 & mask); // polynomial is 0x04C11DB7
		}
	}
	return crc; // not ~crc as we used the JAMCRC variant with XorOut cancelled
}

It’s the typical CRC-32 but the JAMCRC variant, i.e. Poly = 0x04C11DB7, Init = 0xFFFFFFFF, RefIn = true, RefOut = true, XorOut = 0

So it’s the same as this in PHP, for example: $crc = crc32($data) ^ 0xFFFFFFFF;

See https://crccalc.com/ for more info

To get the “normal” CRC (not the JAMCRC variant), simply change the last line to return ~crc;.