从 Python 计算 CRC 校验和
CRC checksum calculation from Python
我正在尝试计算 CRC32-C 校验和值。我参考了很多在线 CRC 计算器和很多网站来编写我自己的 crc 校验和计算器。我没有得到预期的结果。根据我的研究,只有“http://crccalc.com/”给出了我的预期值。
我看到有很多方法有表格,没有表格..等等似乎没有什么对我有用,我正在上传我用来计算 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
我正在努力让它工作。感谢有人对此有解决方案
就我测试的模块使用 CRC-32C(令人讨厌的 ceph 对象)而言,最好的是 crccheck
,它不像大多数实现那样需要 SSE4。
>>> from crccheck.crc import Crc32c
>>> Crc32c().process(b'kwarunek').finalhex()
'aa862086'
http://crccalc.com/ 给出相同的结果。
编辑:计算十六进制值
crccheck
能够计算十六进制值的校验和,只需要将值作为字节传递 - 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()
我正在尝试计算 CRC32-C 校验和值。我参考了很多在线 CRC 计算器和很多网站来编写我自己的 crc 校验和计算器。我没有得到预期的结果。根据我的研究,只有“http://crccalc.com/”给出了我的预期值。
我看到有很多方法有表格,没有表格..等等似乎没有什么对我有用,我正在上传我用来计算 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
我正在努力让它工作。感谢有人对此有解决方案
就我测试的模块使用 CRC-32C(令人讨厌的 ceph 对象)而言,最好的是 crccheck
,它不像大多数实现那样需要 SSE4。
>>> from crccheck.crc import Crc32c
>>> Crc32c().process(b'kwarunek').finalhex()
'aa862086'
http://crccalc.com/ 给出相同的结果。
编辑:计算十六进制值
crccheck
能够计算十六进制值的校验和,只需要将值作为字节传递 - 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()