def _program_data_process(self, program_data) -> tuple:
        """ extract the segment of the program data corresponding to
        application memory and perform CRC16 calculation """

        if self._current_program_data == program_data:
            return (self._current_program_segment, self._current_checksum)

        try:
            program_segment = program_data[self.starting_address * 2:
                                           self.starting_address * 2
                                           + self.memory_length * 2]
        except BaseException as e:
            raise RuntimeError(
                "dimension of program does not fit memory") from e

        try:
            data = program_segment
            crc_calculator = CrcCalculator(Crc16.CCITT)
            checksum = crc_calculator.calculate_checksum(data)
            logging.info(f"Calculated checksum is {checksum}")
        except BaseException as e:
            raise RuntimeError("calculation of CRC failed") from e

        self._current_program_segment = program_segment
        self._current_checksum = checksum
        return (program_segment, checksum)
Example #2
0
	def __init__(self, authID='', nukiCommand=None, nonce='', publicKey='', privateKey=''):
		self.byteSwapper = ByteSwapper()
		self.crcCalculator = CrcCalculator()
		self.authID = authID
		self.command = nukiCommand
		self.nonce = nonce
		if nonce == '':
			self.nonce = nacl.utils.random(24).hex()
		self.publicKey = publicKey
		self.privateKey = privateKey
Example #3
0
class Nuki_EncryptedCommand(object):
	def __init__(self, authID='', nukiCommand=None, nonce='', publicKey='', privateKey=''):
		self.byteSwapper = ByteSwapper()
		self.crcCalculator = CrcCalculator()
		self.authID = authID
		self.command = nukiCommand
		self.nonce = nonce
		if nonce == '':
			self.nonce = nacl.utils.random(24).hex()
		self.publicKey = publicKey
		self.privateKey = privateKey

	def generate(self, format='BYTE_ARRAY'):
		unencrypted = self.authID + self.command.generate(format='HEX')[:-4]
		crc = self.byteSwapper.swap(self.crcCalculator.crc_ccitt(unencrypted))
		unencrypted = unencrypted + crc
		sharedKey = crypto_box_beforenm(bytes(bytearray.fromhex(self.publicKey)),bytes(bytearray.fromhex(self.privateKey))).hex()
		box = nacl.secret.SecretBox(bytes(bytearray.fromhex(sharedKey)))
		encrypted = box.encrypt(bytes(bytearray.fromhex(unencrypted)), bytes(bytearray.fromhex(self.nonce))).hex()[48:]
		length = self.byteSwapper.swap("%04X" % int((len(encrypted)/2)))
		msg = self.nonce + self.authID + length + encrypted
		if format == 'BYTE_ARRAY':
			return bytearray.fromhex(msg)
		else:
			return msg
Example #4
0
	def __init__(self, macAddress, cfg='/home/pi/nuki/nuki.cfg'):	
		self._charWriteResponse = ""
		self.parser = nuki_messages.NukiCommandParser()
		self.crcCalculator = CrcCalculator()
		self.byteSwapper = ByteSwapper()
		self.macAddress = macAddress
		self.config = ConfigParser.RawConfigParser()
		self.config.read(cfg)
		self.device = None
Example #5
0
class Nuki_Command(object):
    def __init__(self, payload=""):
        self.crcCalculator = CrcCalculator()
        self.byteSwapper = ByteSwapper()
        self.parser = NukiCommandParser()
        self.payload = payload

    def generate(self, format='BYTE_ARRAY'):
        msg = self.byteSwapper.swap(type(self).command) + self.payload
        crc = self.byteSwapper.swap(self.crcCalculator.crc_ccitt(msg))
        msg = msg + crc
        if format == 'BYTE_ARRAY':
            return bytearray.fromhex(msg)
        else:
            return msg

    def isError(self):
        return type(self) == Nuki_ERROR
Example #6
0
class Nuki_Command(object):
    def __init__(self, payload=""):
        self.crcCalculator = CrcCalculator()
        self.byteSwapper = ByteSwapper()
        self.parser = NukiCommandParser()
        self.command = ''
        self.payload = payload

    def generate(self, format='BYTE_ARRAY'):
        msg = self.byteSwapper.swap(self.command) + self.payload
        crc = self.byteSwapper.swap(self.crcCalculator.crc_ccitt(msg))
        msg = msg + crc
        if format == 'BYTE_ARRAY':
            return array.array('B', msg.decode("hex"))
        else:
            return msg

    def isError(self):
        return self.command == '0012'
Example #7
0
	def __init__(self, payload=""):
		self.crcCalculator = CrcCalculator()
		self.byteSwapper = ByteSwapper()
		self.parser = NukiCommandParser()
		self.command = ''
		self.payload = payload