コード例 #1
0
ファイル: nuki_messages.py プロジェクト: giejay/nukiPyBridge
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
コード例 #2
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
コード例 #3
0
ファイル: nuki_messages.py プロジェクト: nawrasg/nukiPyBridge
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'