Evaluating the differences in CRC-32 implementatio

2019-07-18 11:33发布

问题:

I have seen many different implementations of the same basic CRC-32 algorithm shown below:

int remain;
int sbox[SIZESBOX];
int dividend;
int bit;

for(dividend = 0; dividend < SIZESBOX; dividend++)
{
    remain = dividend << 24;
    for(bit = 0; bit < 8; bit++)
    {
        if(remain & TOPBIT)
        {
            remain = (remain << 1) ^ POLYNOMIAL;
        }
        else
        {
            remain = (remain << 1);
        }
    }
    sbox[dividend] = remain;
}

Some of them XOR the dividend before going into the sbox. Others XOR before going into the bit loop, and others use bitwise reflection.

Are there differences that I need to consider between the varying implementations of CRC-32 for a given use case? Is one that uses bitwise reflection or XOR-OUT necessarily better than one that doesn't? Why are there so many different implementations anyway?

回答1:

CRC32 isn't secure in any way, so from a crypto standpoint the variations aren't relevant. It might affect the distribution properties, but I doubt that's relevant either.

A CRC is just a checksum, protecting against random changed(in particular bitflips) but can't be used as a cryptographic hash. You should use SHA-1 or better for that.



回答2:

CRC32 is not a cryptographic algorithm, so your question makes me think you need to consider what you're using it for long and hard.