import copy
import zlib
# def crc8():
test_crc8 = 0x11 # 資料
poly_crc8 = 0x11d # 多項式
for bit in range(8):
if (test_crc8 & 0x80) != 0: # 判斷首位是否為1
test_crc8 <<= 1 # 右移
test_crc8 ^= poly_crc8 # 異或計算
else:
test_crc8 <<= 1 # 不等於1則直接移位
print(hex(test_crc8))
crc8()
def crc32():
test_crc32 = 0x01
test_crc32 <<= 24
poly_crc32_1 = 0xedb88320
poly_crc32_2 = 0x104c11db7
for bit in range(8):
if (test_crc32 & 0x80000000) != 0: # 判斷首位是否為1
test_crc32 <<= 1 # 右移
test_crc32 ^= poly_crc32_1 # 異或計算
else:
test_crc32 <<= 1 # 不等於1則直接移位
print(hex(test_crc32))
def crc32_table_normal(): # 彩虹表演算法,查表演算法
crc32_table_normal_list =
poly_crc32_normal = 0x104c11db7
for byte in range(256):
operator = copy.copy(byte)
operator <<= 24
for bit in range(8):
if (operator & 0x80000000) != 0:
operator <<= 1
operator ^= poly_crc32_normal
else:
operator <<= 1
to_print = list(map(hex, crc32_table_normal_list))
print(to_print)
def crc32_table_recip():
crc32_table_recip_list =
poly_crc32_recip = 0x104c11db7
for byte in range(256):
operator = copy.copy(byte)
operator = int(''.format(operator)[::-1], 2) # 倒置
operator <<= 24
for bit in range(8):
if (operator & 0x80000000) != 0:
operator <<= 1
operator ^= poly_crc32_recip
else:
operator <<= 1
operator = int(''.format(operator)[::-1], 2) # 倒置
to_print = list(map(hex, crc32_table_recip_list))
print(to_print)
def crc32_recip(line):
var = 0xffffffff
for ch in line:
operator = ord(ch)
operator = (operator ^ var) & 0xff
var = crc32_table_recip[operator] ^ (var >> 8)
return var ^ 0xffffffff
# print(hex(zlib.crc32('123456789'.encode('utf-8'))))
# print(hex(crc32_recip('123456789')))
def crc32_normal(line):
var = 0xffffffff
for ch in line:
operator = ord(ch)
operator = int(''.format(operator)[::-1], 2)
var = crc32_table_normal[operator]^(var << 8)& 0xffffffff
var = int(''.format(operator)[::-1], 2)
return var ^ 0xffffffff
# print(hex(crc32_normal('123456789')))
def crc32_table_polyrev():
crc32_table_polyrev_list =
poly_rev = 0xedb88320
for byte in range(256):
operator = copy.copy(byte)
for bit in range(8):
if (operator & 0x1) != 0:
operator >>= 1
operator^=poly_rev
else:
operator >>= 1
to_print_polyrev = list(map(hex, crc32_table_polyrev_list))
print(to_print_polyrev)
# crc逆演算法
def crc8_reverse():
test_crc32_result = 0xd0
poly_crc32 = 0x11d
for bit in range(8):
if test_crc32_result & 1 == 1:
test_crc32_result ^= poly_crc32
test_crc32_result <<= 1
test_crc32_result |= 0x80
continue
else:
test_crc32_result <<= 1
print(hex(test_crc32_result & 0x08))
crc32_reverse()
CRC32演算法實現
crc32 檢錯能力極強,開銷小,易於用編碼器及檢測電路實現。從其檢錯能力來看,它所不能發現的錯誤的機率僅為0.0047 以下。從效能上和開銷上考慮,均遠遠優於奇偶校驗及算術和校驗等方式。因而,在資料儲存和資料通訊領域,crc無處不在 著名的通訊協議x.25的fcs 幀檢錯序列 採用的是crc cc...
Python 實現CRC校驗計算
class crc 迴圈冗餘檢驗 parameters info list 需要被編碼的資訊 crc n int,default 32 生成多項式的階數 p list 生成多項式 q list crc後得到的商 check code list crc後得到的餘數,即計算得到的校驗碼 code lis...
C 實現CRC校驗演算法
2007 06 16 10 06 by l,2734 visits,收藏,編輯 region crc校驗 crc高位校驗碼checkcrchigh static byte arraycrchigh crc地位校驗碼checkcrclow static byte checkcrclow crc校驗 校...