cipher = pyaes.blockfeeder.Decrypter(pyaes.AESModeOfOperationECB(self.key)) # no IV, auto pads to 16
cipher = pyaes.blockfeeder.Decrypter(pyaes.AESModeOfOperationECB(self.key)) # no IV, auto pads to 16
plain_text = cipher.feed(enc)
plain_text = cipher.feed(enc)
plain_text += cipher.feed() # flush final block
plain_text += cipher.feed() # flush final block
return plain_text
return plain_text
def _pad(self, s):
def _pad(self, s):
padnum = self.bs - len(s) % self.bs
padnum = self.bs - len(s) % self.bs
return s + padnum * chr(padnum).encode()
return s + padnum * chr(padnum).encode()
@staticmethod
@staticmethod
def _unpad(s):
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
return s[:-ord(s[len(s)-1:])]
def bin2hex(x, pretty=False):
def bin2hex(x, pretty=False):
if pretty:
if pretty:
space = ' '
space = ' '
else:
else:
space = ''
space = ''
if IS_PY2:
if IS_PY2:
result = ''.join('%02X%s' % (ord(y), space) for y in x)
result = ''.join('%02X%s' % (ord(y), space) for y in x)
else:
else:
result = ''.join('%02X%s' % (y, space) for y in x)
result = ''.join('%02X%s' % (y, space) for y in x)
return result
return result
def hex2bin(x):
def hex2bin(x):
if IS_PY2:
if IS_PY2:
return x.decode('hex')
return x.decode('hex')
else:
else:
return bytes.fromhex(x)
return bytes.fromhex(x)
# This is intended to match requests.json payload at https://github.com/codetheweb/tuyapi
# This is intended to match requests.json payload at https://github.com/codetheweb/tuyapi
# device20 or device22 are to be used depending on the length of dev_id (20 or 22 chars)
# Items device20 and device22 are to be used depending on the length of dev_id (20 or 22 chars)
payload_dict = {
payload_dict = {
"device20": {
"device20": {
"status": {
"status": {
"hexByte": "0a",
"hexByte": "0a",
"command": {"gwId": "", "devId": ""}
"command": {"gwId": "", "devId": ""}
},
},
"set": {
"set": {
"hexByte": "07",
"hexByte": "07",
"command": {"devId": "", "uid": "", "t": ""}
"command": {"devId": "", "uid": "", "t": ""}
},
},
"prefix": "000055aa00000000000000", # Next byte is command byte ("hexByte") some zero padding, then length of remaining payload, i.e. command + suffix (unclear if multiple bytes used for length, zero padding implies could be more than one byte)
"prefix": "000055aa00000000000000", # Next byte is command byte ("hexByte") some zero padding, then length of remaining payload, i.e. command + suffix (unclear if multiple bytes used for length, zero padding implies could be more than one byte)
"suffix": "000000000000aa55"
"suffix": "000000000000aa55"
},
},
"device22": {
"device22": {
"status": {
"status": {
"hexByte": "0d",
"hexByte": "0d",
"command": {"devId": "", "uid": "", "t": ""}
"command": {"devId": "", "uid": "", "t": ""}
},
},
"set": {
"set": {
"hexByte": "07",
"hexByte": "07",
"command": {"devId": "", "uid": "", "t": ""}
"command": {"devId": "", "uid": "", "t": ""}
},
},
"prefix": "000055aa00000000000000", # Next byte is command byte ("hexByte") some zero padding, then length of remaining payload, i.e. command + suffix (unclear if multiple bytes used for length, zero padding implies could be more than one byte)
"prefix": "000055aa00000000000000", # Next byte is command byte ("hexByte") some zero padding, then length of remaining payload, i.e. command + suffix (unclear if multiple bytes used for length, zero padding implies could be more than one byte)