8000 Avoid temp array during encrypt and decrypt by bdraco · Pull Request #204 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Avoid temp array during encrypt and decrypt #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 23, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions kasa/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,24 @@ async def query(host: str, request: Union[str, Dict], retry_count: int = 3) -> D
# make mypy happy, this should never be reached..
raise SmartDeviceException("Query reached somehow to unreachable")

@staticmethod
def _generate_tplink(unencrypted):
key = TPLinkSmartHomeProtocol.INITIALIZATION_VECTOR
for unencryptedbyte in unencrypted:
key = key ^ unencryptedbyte
yield key

@staticmethod
def encrypt(request: str) -> bytes:
"""Encrypt a request for a TP-Link Smart Home Device.

:param request: plaintext request data
:return: ciphertext to be send over wire, in bytes
"""
key = TPLinkSmartHomeProtocol.INITIALIZATION_VECTOR

plainbytes = request.encode()
buffer = bytearray(struct.pack(">I", len(plainbytes)))

for plainbyte in plainbytes:
cipherbyte = key ^ plainbyte
key = cipherbyte
buffer.append(cipherbyte)

return bytes(buffer)
return struct.pack(">I", len(plainbytes)) + bytes(
TPLinkSmartHomeProtocol._generate_tplink(plainbytes)
)

@staticmethod
def decrypt(ciphertext: bytes) -> str:
Expand Down
0