How to generate the valid CRC32 result in Python?

-1

I am trying to generate the correct CRC result 0xDD413F23 through the Python console in command line but cannot obtain it. Tried calculating CRC for different inputs in the console snippet shown below: a) R concatenated with R, last for bytes are 0, 512 bytes b) R concatenated with R, 512 bytes c) R concatenated with 256 bytes of zero, 512 byte in total d) R, 256 bytes

Here is the python snippet in console:

>>> from crccheck import crc
>>> R = '85FF33A990229E0AE4733B7DF20AB659B7B6D19578E88F8AC167DD33C1D747FD574C1F1FDC81811973425ACD7E66648E2037CC55C481C232B00031DEB5A50A5EF6237B0BB29B70AD6BCE01A242442D9E081FF78C76614B76B9314377138A5AFC5DB6D4DE470F25A2FAD88B0AFE900A903008A75C9F3D467634D77A7B1ACD8F35961883B64CF78225578539CE27DEA8748369C42F96FDECB7E55930BF457DF1CAAD9C14AF2B64AA2965A40853B5D5E7ADA35B91B320553922205A61E1F10AAEA6B86C1F8A42B241EEC74509FDFBFA7EC695EBF1B676B16B8297FF14A4E5871ED6E3450AA2FF14347991F3876B8D17C9832D623A605D8DCD6530F870D65184AE01'
>>> "0x%X" % (crc.Crc32.calc(bytearray.fromhex(R + R[:-8] + "00000000")))
'0xE60598CA'
>>> "0x%X" % (crc.Crc32.calc(bytearray.fromhex(R + R)))
'0x340BE2A0'
>>> "0x%X" % (crc.Crc32.calc(bytearray.fromhex(R + "00" * 256)))
'0x94252687'
>>> "0x%X" % (crc.Crc32.calc(bytearray.fromhex(R)))
'0xADB8417F'

This is the C++ snipppet which works fine

'''

uint32_t c = 0;
c = c_cal(ip_buf, 512, c, 0x04C11DB7, 4, FALSE);

uint32_t Functions::c_cal(uint32_t *d_base, uint32_t d_size, uint32_t c, uint32_t poly, uint8_t c_size, bool b_r_ip)
{
    unsigned long d_offset;
    unsigned long d, d_temp;
    unsigned char c_bit;

    d = 0;
    for(d_offset = 0; d_offset < d_size; (d_offset += c_size))
    {
        u32_d_temp = 0;
        d_temp = d_base[d_offset/c_size];
        if(FALSE == b_r_ip)
        {
            d = d_temp;
        }
        else
        {
            d = 0;
            for(c_bit = 0; c_bit < (c_size << 3); c_bit++)
            {
                d <<= 1;
                d |= (d_temp & 1);
                d_temp >>= 1;
            }
        }

        for(c_bit = 0; c_bit < (c_size << 3); c_bit++)
        {
            if(((c >> ((c_size << 3) - 1)) ^ d) & 1)
            {
                c <<= 1;
                d >>= 1;
                c ^= poly;
            }
            else
            {
                c <<= 1;
                d >>= 1;
            }
        }
    }

    return (c & (0xFFFFFFFF >> (32 - (c_size << 3))));
}

'''

python
c++
crc32
uint32
asked on Stack Overflow Sep 29, 2020 by Ashish101 • edited Sep 29, 2020 by Ashish101

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0