1

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

NMN
  • 372
  • 3
  • 16
  • 1
    Not anything near to answer, but you should check [PEP8](https://www.python.org/dev/peps/pep-0008/#imports) on imports. They should be at the beginning of your file, there are no valid reasons to include them in a function (or class, for that matter). It doesn't really hurt, but nevertheless a no-no... [This](https://stackoverflow.com/a/5262532/1878539) answer here explains that a little more. – Kraay89 Jul 05 '17 at 07:58
  • Does [binascii.crc32](https://docs.python.org/3/library/binascii.html) work for your purposes? – gerardw Feb 20 '18 at 15:32

1 Answers1

2

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()
kwarunek
  • 12,141
  • 4
  • 43
  • 48
  • Thanks. But this takes the ascii characters and not the hex, i got that value for hex from multiple modules lice crcmod, crccheck... is there any help for getting the crc for hex values... You can see in my example i'm trying to calculate crc for hex numbers – NMN Jul 06 '17 at 02:46