def test_reverse_engineering(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) bitstring_set = [ "1110001111001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110010011001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110010111001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110011011001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010" ] bitset = [] crcset = [] for i in bitstring_set: tmp = c.str2bit(i) bitset.append(tmp) crcset.append(c.crc(tmp)) polynomial = c.reverse_engineer_polynomial(bitset, crcset) if polynomial: self.assertEqual(c.bit2str(polynomial), "1000000000000101") self.assertEqual(util.bit2hex(polynomial), "8005")
def test_not_aligned_data_len(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) polynomials = ["8_standard", "16_standard", "16_ccitt", "16_dnp"] crcs = {"8_standard": 0xd5, "16_standard": 0x8005, "16_ccitt": 0x1021, "16_dnp": 0x3d65} for j in polynomials: c.polynomial = c.choose_polynomial(j) inpt = "1" for i in range(0, 32): val = c.bit2int(c.crc(c.str2bit(inpt))) self.assertEqual(val, crcs[j]) inpt = "0" + inpt
def test_reverse_engineering(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) bitstring_set = [ "1110001111001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110010011001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110010111001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010", "1110011011001011100010000101010100000010110111000101100010100100111110111101100110110111011001010010001011101010"] bitset = [] crcset = [] for i in bitstring_set: tmp = c.str2bit(i) bitset.append(tmp) crcset.append(c.crc(tmp)) # print(c.guess_standard_parameters(bitset[0], crcset[0])) polynomial = c.reverse_engineer_polynomial(bitset, crcset) if polynomial: self.assertEqual(c.bit2str(polynomial), "1000000000000101") self.assertEqual(util.bit2hex(polynomial), "8005")
def test_different_crcs(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) bitstring_set = [ "101001001010101010101011101111111000000000000111101010011101011", "101001001010101101111010110111101010010110111010", "00000000000000000000000000000000100000000000000000000000000000000001111111111111", "1111111111111111111111111111111110111111111111111111110111111111111111110000000000" "1" ] for j in c.DEFAULT_POLYNOMIALS: c.polynomial = c.choose_polynomial(j) for i in bitstring_set: # Standard crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) # Special final xor c.final_xor = c.str2bit("0000111100001111") crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.final_xor = [False] * 16 # Special start value c.start_value = c.str2bit("1010101010101010") crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.start_value = [False] * 16 # reverse_polynomial c.reverse_polynomial = True crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.reverse_polynomial = False # lsb_first c.lsb_first = True crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.lsb_first = False # little_endian c.little_endian = True crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.little_endian = False # reverse all c.reverse_all = True crc_new = c.crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_new, crc_old) c.reverse_all = False
def test_different_crcs_fast(self): c = GenericCRC(polynomial="16_standard", start_value=False, final_xor=False, reverse_polynomial=False, reverse_all=False, lsb_first=False, little_endian=False) bitstring_set = [ "10101010", "00000001", "000000010", "000000011", "0000000100000001", "101001001010101010101011101111111000000000000111101010011101011", "101001001010101101111010110111101010010110111010", "00000000000000000000000000000000100000000000000000000000000000000001111111111111", "1111111111111111111111111111111110111111111111111111110111111111111111110000000000" "1" ] for j in c.DEFAULT_POLYNOMIALS: c.polynomial = c.choose_polynomial(j) for i in bitstring_set: for cache in [8, 4, 7, 12, 16]: c.calculate_cache(cache) # Standard crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) # Special final xor c.final_xor = c.str2bit("0000111100001111") crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.final_xor = [False] * 16 # Special start value c.start_value = c.str2bit("1010101010101010") crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.start_value = [False] * 16 # little_endian c.little_endian = True crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.little_endian = False # reverse all c.reverse_all = True crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.reverse_all = False # reverse_polynomial # We need to clear the cache before and after c.cache = [] # c.reverse_polynomial = True crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.reverse_polynomial = False # c.cache = [] # TODO: Does only work for cachesize = 8 # lsb_first c.calculate_cache(8) c.lsb_first = True crc_new = c.cached_crc(c.str2bit(i)) crc_old = c.reference_crc(c.str2bit(i)) self.assertEqual(crc_old, crc_new) c.lsb_first = False