2022年 11月 5日

CRC算法python简单实现

  1. import copy
  2. import zlib
  3. # https://github.com/Michaelangel007/crc32
  4. def crc8():
  5. test_crc8 = 0x11 # 数据
  6. poly_crc8 = 0x11d # 多项式
  7. for bit in range(8):
  8. if (test_crc8 & 0x80) != 0: # 判断首位是否为1
  9. test_crc8 <<= 1 # 右移
  10. test_crc8 ^= poly_crc8 # 异或计算
  11. else:
  12. test_crc8 <<= 1 # 不等于1则直接移位
  13. print(hex(test_crc8))
  14. crc8()
  15. def crc32():
  16. test_crc32 = 0x01
  17. test_crc32 <<= 24
  18. poly_crc32_1 = 0xedb88320
  19. poly_crc32_2 = 0x104c11db7
  20. for bit in range(8):
  21. if (test_crc32 & 0x80000000) != 0: # 判断首位是否为1
  22. test_crc32 <<= 1 # 右移
  23. test_crc32 ^= poly_crc32_1 # 异或计算
  24. else:
  25. test_crc32 <<= 1 # 不等于1则直接移位
  26. print(hex(test_crc32))
  27. def crc32_table_normal(): # 彩虹表算法,查表算法
  28. crc32_table_normal_list = []
  29. poly_crc32_normal = 0x104c11db7
  30. for byte in range(256):
  31. operator = copy.copy(byte)
  32. operator <<= 24
  33. for bit in range(8):
  34. if (operator & 0x80000000) != 0:
  35. operator <<= 1
  36. operator ^= poly_crc32_normal
  37. else:
  38. operator <<= 1
  39. crc32_table_normal_list.append(operator)
  40. to_print = list(map(hex, crc32_table_normal_list))
  41. print(to_print)
  42. def crc32_table_recip():
  43. crc32_table_recip_list = []
  44. poly_crc32_recip = 0x104c11db7
  45. for byte in range(256):
  46. operator = copy.copy(byte)
  47. operator = int('{:08b}'.format(operator)[::-1], 2) # 倒置
  48. operator <<= 24
  49. for bit in range(8):
  50. if (operator & 0x80000000) != 0:
  51. operator <<= 1
  52. operator ^= poly_crc32_recip
  53. else:
  54. operator <<= 1
  55. operator = int('{:032b}'.format(operator)[::-1], 2) # 倒置
  56. crc32_table_recip_list.append(operator)
  57. to_print = list(map(hex, crc32_table_recip_list))
  58. print(to_print)
  59. def crc32_recip(line):
  60. var = 0xffffffff
  61. for ch in line:
  62. operator = ord(ch)
  63. operator = (operator ^ var) & 0xff
  64. var = crc32_table_recip[operator] ^ (var >> 8)
  65. return var ^ 0xffffffff
  66. # print(hex(zlib.crc32('123456789'.encode('utf-8'))))
  67. # print(hex(crc32_recip('123456789')))
  68. def crc32_normal(line):
  69. var = 0xffffffff
  70. for ch in line:
  71. operator = ord(ch)
  72. operator = int('{:08b}'.format(operator)[::-1], 2)
  73. var = crc32_table_normal[operator]^(var << 8)& 0xffffffff
  74. var = int('{:032b}'.format(operator)[::-1], 2)
  75. return var ^ 0xffffffff
  76. # print(hex(crc32_normal('123456789')))
  77. def crc32_table_polyrev():
  78. crc32_table_polyrev_list = []
  79. poly_rev = 0xedb88320
  80. for byte in range(256):
  81. operator = copy.copy(byte)
  82. for bit in range(8):
  83. if (operator & 0x1) != 0:
  84. operator >>= 1
  85. operator^=poly_rev
  86. else:
  87. operator >>= 1
  88. crc32_table_polyrev_list.append(operator)
  89. to_print_polyrev = list(map(hex, crc32_table_polyrev_list))
  90. print(to_print_polyrev)
  91. # crc逆算法
  92. def crc8_reverse():
  93. test_crc32_result = 0xd0
  94. poly_crc32 = 0x11d
  95. for bit in range(8):
  96. if test_crc32_result & 1 == 1:
  97. test_crc32_result ^= poly_crc32
  98. test_crc32_result <<= 1
  99. test_crc32_result |= 0x80
  100. continue
  101. else:
  102. test_crc32_result <<= 1
  103. print(hex(test_crc32_result & 0x08))
  104. crc32_reverse()