Skip to content

Commit

Permalink
feat: add code
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jun 16, 2023
1 parent dad4184 commit 7fea718
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/kasa_crypt/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
__version__ = "0.0.0"

from .kasa_crypt import decrypt, encrypt

__all__ = ["encrypt", "decrypt"]
2 changes: 1 addition & 1 deletion src/kasa_crypt/kasa_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def encrypt(string: str) -> bytes:
def decrypt(string: bytes) -> str:
"""Decrypt."""
key = 171
result = bytearray(string)
result = bytearray(len(string))
for idx, i in enumerate(string):
a = key ^ i
key = i
Expand Down
76 changes: 76 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import json

from kasa_crypt import decrypt, encrypt

# from
# https://github.com/python-kasa/python-kasa/blob/master/kasa/tests/test_protocol.py


def test_encrypt():
d = json.dumps({"foo": 1, "bar": 2})
encrypted = encrypt(d)
# encrypt adds a 4 byte header
encrypted = encrypted[4:]
assert d == decrypt(encrypted)


def test_encrypt_unicode():
d = "{'snowman': '\u2603'}"

e = bytes(
[
208,
247,
132,
234,
133,
242,
159,
254,
144,
183,
141,
173,
138,
104,
240,
115,
84,
41,
]
)

encrypted = encrypt(d)
# encrypt adds a 4 byte header
encrypted = encrypted[4:]

assert e == encrypted


def test_decrypt_unicode():
e = bytes(
[
208,
247,
132,
234,
133,
242,
159,
254,
144,
183,
141,
173,
138,
104,
240,
115,
84,
41,
]
)

d = "{'snowman': '\u2603'}"

assert d == decrypt(e)
5 changes: 0 additions & 5 deletions tests/test_main.py

This file was deleted.

0 comments on commit 7fea718

Please sign in to comment.