Ethernet CRC32 calculation - software vs algorithmic result

前端 未结 4 1777
太阳男子
太阳男子 2020-12-29 08:27

I\'m trying to calculate the Frame Check Sequence (FCS) of an Ethernet packet byte by byte. The polynomial is 0x104C11DB7. I did follow the XOR-SHIFT algorithm

4条回答
  •  春和景丽
    2020-12-29 09:15

    This snippet writes the correct CRC for Ethernet.

    Python 3

    # write payload
    for byte in data:
        f.write(f'{byte:02X}\n')
    # write FCS
    crc = zlib.crc32(data) & 0xFFFF_FFFF
    for i in range(4):
        byte = (crc >> (8*i)) & 0xFF
        f.write(f'{byte:02X}\n')
    

    Python 2

    # write payload
    for byte in data:
        f.write('%02X\n' % ord(byte))
    # write FCS
    crc = zlib.crc32(data) & 0xFFFFFFFF
    for i in range(4):
        byte = (crc >> (8*i)) & 0xFF
        f.write('%02X\n' % byte)
    

    Would have saved me some time if I found this here.

提交回复
热议问题