I already have working examples that calculate CRC-8, CRC-16, CRC-32 in Java but they are different implementations and I am a bit confused. I tried to read the math tech docs, but it seems a bit too much for my math level. Also, converting code from C/C++ is not as straight forward since Java does not do well with unsigned primitives...
I need a complete example to understand and be able to verify my checksums since there are a lot of different polynomials out there!
See Best CRC Polynomials by Philip Koopman and CRC Hamming Weight Data by Philip Koopman.
I would appreciate if anyone can provide a minimum but detailed example in Java that for e.g. CRC-8, does:
This is what I managed to put together for CRC-8. Still, I cannot fully understand how the polynomial is produced, especially why we should/could use the reversed version! I also miss a CRC-16, CRC-32 and maybe a CRC-64 complete example as the one I provide below. Hopefully someone else will fulfill this need in the future.
This code uses polynomial: 0xa6 = x^8 + x^6 + x^3 + x^2 + 1 (0x14d) <=> (0xb2; 0x165)
as Mark Adler suggests here and based on these tests that shows the strength of this polynomial.
import java.io.UnsupportedEncodingException;
/**
* Calculate CRC8 based on a lookup table.
* CRC-8 : CRC-8K/3 (HD=3, 247 bits max)
* polynomial: 0xa6 = x^8 + x^6 + x^3 + x^2 + 1 (0x14d) <=> (0xb2; 0x165)
* init = 0
*
* There are two ways to define a CRC, forward or reversed bits.
* The implementations of CRCs very frequently use the reversed bits convention,
* which this one does. 0xb2 is 0x4d reversed. The other common convention is
* to invert all of the bits of the CRC, which avoids a sequence of zeros on
* a zero CRC resulting in a zero CRC. The code below does that as well.
*
* usage:
* new Crc8().update("123456789").getHex() == "D8"
* new Crc8().update("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").getHex() == "EF"
*/
public final class Crc8 {
private static final short CRC8_POLYNOMIAL = 0xB2;
public static short[] createLookupTable()
{
short[] lookupTable = new short[256];
for(int i=0; i<256; i++)
{
int rem = i; // remainder from polynomial division
for(int j=0; j<8; j++)
{
if((rem & 1) == 1)
{
rem >>= 1;
rem ^= CRC8_POLYNOMIAL;
}
else
{
rem >>= 1;
}
}
lookupTable[i] = (short)rem;
}
return( lookupTable );
}
private static final short CRC8_INIT_VALU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x5D, 0x1F, 0x21, 0x9B, 0xA5, 0xE7, 0xD9,
0xF6, 0xC8, 0x8A, 0xB4, 0x0E, 0x30, 0x72, 0x4C,
0xEB, 0xD5, 0x97, 0xA9, 0x13, 0x2D, 0x6F, 0x51,
0x7E, 0x40, 0x02, 0x3C, 0x86, 0xB8, 0xFA, 0xC4,
0xA4, 0x9A, 0xD8, 0xE6, 0x5C, 0x62, 0x20, 0x1E,
0x31, 0x0F, 0x4D, 0x73, 0xC9, 0xF7, 0xB5, 0x8B,
0x75, 0x4B, 0x09, 0x37, 0x8D, 0xB3, 0xF1, 0xCF,
0xE0, 0xDE, 0x9C, 0xA2, 0x18, 0x26, 0x64, 0x5A,
0x3A, 0x04, 0x46, 0x78, 0xC2, 0xFC, 0xBE, 0x80,
0xAF, 0x91, 0xD3, 0xED, 0x57, 0x69, 0x2B, 0x15
};
private final boolean useLookupTable;
private short crc8;
public Crc8()
{
this(true);
}
public Crc8(boolean use_lookup_table)
{
useLookupTable = use_lookup_table;
reset();
}
public Crc8 reset()
{
crc8 = CRC8_INIT_VALUE;
return( this );
}
public Crc8 update(byte b)
{
if( useLookupTable )
{
crc8 = CRC8_LOOKUP_TABLE[(crc8 ^ b) & 0xFF];
}
else
{
crc8 ^= b;
crc8 &= 0xFF;
for(int j=0; j<8; j++)
{
if((crc8 & 1) == 1)
{
crc8 >>= 1;
crc8 ^= CRC8_POLYNOMIAL;
}
else
{
crc8 >>= 1;
}
}
}
return( this );
}
public Crc8 update(byte[] data, int offset, int length)
{
for(int i=offset; i < length; i++)
{
update( data[i] );
}
return( this );
}
public Crc8 update(byte[] data)
{
return update(data, 0, data.length);
}
public Crc8 update(String s)
{
try
{
return update(s.getBytes("UTF-8"));
}
catch(UnsupportedEncodingException ex)
{
throw new RuntimeException(ex);
}
}
public short get()
{
return( (short)(crc8 ^ 0xFF) );
}
/**
* Return calculated CRC8 in 2 capital hex digits with leading zeros.
*/
public String getHex()
{
return( String.format("%02X", get()) );
}
}