def test_crc(self): # http://depa.usst.edu.cn/chenjq/www2/software/crc/CRC_Javascript/CRCcalculation.htm # CRC-16: polynomial="16_standard", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False # CRC-16-CCITT: polynomial="16_ccitt", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False # http://www.lammertbies.nl/comm/info/crc-calculation.html <- Fehler # CRC-16: polynomial="16_standard", start_value = False, final_xor = False, reverse_polynomial=False, reverse_all=False c = crc_generic(polynomial="8_en") e = Encoder() bitstr = [ "010101010110100111011010111011101110111011100110001011101010001011101110110110101101", "010101010110101001101110111011101110111011100110001011101010001011101110110111100101", "010101010110100111010010111011101110111011100110001011101010001011101110110110100101" ] expected = ["0x78", "0xc9", "0xf2"] for value, expect in zip(bitstr, expected): nv = "" for i in range(0, len(value)): if value[i] == "1": nv += "0" else: nv += "1" self.assertEqual(e.bit2hex(c.crc(e.str2bit(value[4:-8]))), expect)
def test_reverse_engineering(self): c = crc_generic(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(c.bit2hex(polynomial), "0x8005")
def __init__(self, chain=None): if chain is None: chain = [] self.mode = 0 self.external_decoder = "" self.external_encoder = "" self.multiple = 1 self.src = [ ] # [[True, True], [True, False], [False, True], [False, False]] self.dst = [ ] # [[False, False], [False, True], [True, False], [True, True]] self.carrier = "1_" self.cutmark = array.array("B", [True, False]) self.cutmode = 0 # 0 = before, 1 = after, 2 = before_pos, 3 = after_pos self.morse_low = 1 self.morse_high = 3 self.morse_wait = 1 self.__symbol_len = 1 # Configure CC1101 Date Whitening polynomial = array.array( "B", [False, False, True, False, False, False, False, True]) # x^5+x^0 sync_bytes = array.array("B", [ True, True, True, False, True, False, False, True, True, True, False, False, True, False, True, False, True, True, True, False, True, False, False, True, True, True, False, False, True, False, True, False ]) # "e9cae9ca" # sync_bytes = self.str2bit("01100111011010000110011101101000") # "67686768" (RWE Default) # sync_bytes = self.str2bit("01101001111101100110100111110111") # "69f669f7" (Special RWE) self.data_whitening_polynomial = polynomial # Set polynomial self.data_whitening_sync = sync_bytes # Sync Bytes self.data_whitening_crc = array.array("B", [False] * 16) # CRC is 16 Bit long self.data_whitening_preamble = array.array("B", [True, False] * 16) # 010101... self.lfsr_state = array.array("B", []) self.data_whitening_apply_crc = True # Apply CRC with XOR self.data_whitening_preamble_rm = True # Remove Preamble self.data_whitening_sync_rm = True # Remove Sync Bytes self.data_whitening_crc_rm = False # Remove CRC # Get CRC Object self.c = crc_generic(polynomial="16_standard", start_value=True) # Set Chain self.chain = [] self.set_chain(chain)
def test_crc(self): c = crc_generic(polynomial="8_en") e = Encoder() bitstr = [ "010101010110100111011010111011101110111011100110001011101010001011101110110110101101", "010101010110101001101110111011101110111011100110001011101010001011101110110111100101", "010101010110100111010010111011101110111011100110001011101010001011101110110110100101" ] print() for v in bitstr: nv = "" for i in range(0, len(v)): if v[i] == "1": nv += "0" else: nv += "1" xorsum = 0 for i in range(12, len(v) - 8, 4): bin = e.str2bit(v[0 + i:4 + i]) xorsum ^= (bin[0] * 8 + bin[1] * 4 + bin[2] * 2 + bin[3]) print(e.bit2hex(e.str2bit(v[1:])), hex(xorsum))
def enocean_crc8(inpt): c = crc_generic(polynomial="8_en") return c.crc(inpt)
def enocean_crc8(inpt): c = crc_generic(polynomial="8_en") return array.array("B", c.crc(inpt))