2017-07-05 256 views
0

我試圖計算CRC32-C校驗和值。我介紹了很多在線CRC計算器和很多網站來編寫我自己的crc校驗和計算器。我沒有得到預期的結果。從我的研究只有「http://crccalc.com/」給我的預期價值。來自Python的CRC校驗和計算

我看到有有桌子許多方法,不表。等等一切似乎是爲我工作,我上傳簡單​​的方法程序,我用來計算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 

我想得到這個工作。欣賞有人是否有這個解決方案

+0

接近答案沒有什麼,但是您應該檢查導入時的[PEP8](https://www.python.org/dev/peps/pep-0008/#imports)。他們應該在文件的開頭,沒有任何有效的理由將它們包含在函數(或類中)中。它並沒有真正受到傷害,但仍然是一個不...... [This](https://stackoverflow.com/a/5262532/1878539)這裏的答案解釋了一點。 – Kraay89

+0

[binascii.crc32](https://docs.python.org/3/library/binascii.html)是否適用於您的目的? – gerardw

回答

0

據我測試模塊使用CRC-32C(討厭的mangling ceph對象),最好的是crccheck,它不需要SSE4像它的大部分實現。

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

http://crccalc.com/給出了相同的結果。

編輯:計算值十六進制值

crccheck能夠calc下校驗和十六進制值,只是需要以字節爲單位傳遞價值 - 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() 
+0

謝謝。但這需要ascii字符而不是十六進制,我從多個模塊得到了十六進制值的值lice crcmod,crccheck ...有沒有幫助獲取十六進制值的crc ...你可以在我的例子中看到我試圖計算crc爲十六進制數 – NMN

+0

我已經用十六進制例子更新了答案。 – kwarunek