NMN
NMN

Reputation: 372

CRC checksum calculation from Python

I'm trying to calculate the CRC32-C checksum value. I referred lot of online CRC calculators and lot of websites for writing my own crc checksum calculator. I'm not getting the expected result. From my research only "http://crccalc.com/" is giving my expected value.

I see there are many approaches with tables, without tables.. etc Nothing seems to be working for me, I'm uploading simple approach program which i used to calculate crc

POLY_LENGTH = 32

def convertobin(input_message):
    input_message = bin(input_message[::-1])
    return input_message

def find_xor(polynomial, input_message):
    # print polynomial
    print input_message
    xor = polynomial ^ input_message
    return xor

def find_crc(polynomial, packet):
    print len(packet)
    input_messageInBin = bin(int(packet,16))[2:]
    print input_messageInBin, type(input_messageInBin)
    input_messageInBin = input_messageInBin + '0'*31
    inputMessageLength = len(input_messageInBin)
    firstTime = 1
    remainder = 0
    startmarker = 0
    control = 1
    diff = 0
    stopmarker = 0
    import pdb; pdb.set_trace()
    while control:
        if len(input_messageInBin) > (startmarker + 32) :
            if firstTime:
                inputString = input_messageInBin[0:POLY_LENGTH]
                stopmarker = POLY_LENGTH - 1
                xor = find_xor(polynomial, int(inputString,2))

                xor_bits_length = len(bin(xor)) - 2
                remainder = str(bin(xor))[2:]
                diff = POLY_LENGTH - xor_bits_length
                firstTime = 0
                startmarker = startmarker + diff - 1
            else:
                diff_bits = input_messageInBin[stopmarker+1:stopmarker+diff+1]
                inputString = remainder + diff_bits
                print "--------------------------------------------------------"
                print "Total number of different bits are: ", diff
                print "remainder + diff_bits : ", remainder, "+", diff_bits
                print "XOR of :"
                print inputString
                print bin(polynomial)[2:]
                xor = find_xor(polynomial, int(inputString,2))
                xor_bits_length = len(bin(xor)) - 2
                remainder = str(bin(xor))[2:]
                print remainder
                print "--------------------------------------------------------"
                diff = POLY_LENGTH - xor_bits_length
                stopmarker = stopmarker + diff
                startmarker = startmarker + diff

        else:
            control = 0
            remainder = remainder + input_messageInBin[startmarker:stopmarker]
            print stopmarker
            print startmarker

    return remainder
val = find_crc(0x1EDC6F41, 'FFFFFFFFFFFFFFFFFFFF0F0FB01013F2E8FAF0421208')
print val

I'm trying to get this working. Appreciate if somebody has solution for this

Upvotes: 1

Views: 5331

Answers (1)

kwarunek
kwarunek

Reputation: 12587

As far as I tested module to use CRC-32C (nasty mangling ceph objects), the best was crccheck, it does not require SSE4 like the most of its implementations.

>>> from crccheck.crc import Crc32c
>>> Crc32c().process(b'kwarunek').finalhex()
'aa862086'

http://crccalc.com/ gives the same results.

Edit: calc for hex value

crccheck is able to calc checksum for a hex value, just requires the value to be passed as bytes - method for large integers:

x = int('FFFFFFFFFFFFFFFFFFFF0F0FB01013F2E8FAF0421208', 16)
nbytes, rem = divmod(x.bit_length(), 8)
if rem: nbytes += 1
prepared = x.to_bytes(nbytes, 'big')

Crc32c().process(prepared).finalhex()

Upvotes: 2

Related Questions