diff --git a/src/kasa_crypt/__init__.py b/src/kasa_crypt/__init__.py index 6c8e6b9..c19e5d2 100644 --- a/src/kasa_crypt/__init__.py +++ b/src/kasa_crypt/__init__.py @@ -1 +1,5 @@ __version__ = "0.0.0" + +from .kasa_crypt import decrypt, encrypt + +__all__ = ["encrypt", "decrypt"] diff --git a/src/kasa_crypt/kasa_crypt.py b/src/kasa_crypt/kasa_crypt.py index 94d04eb..63a1dcd 100644 --- a/src/kasa_crypt/kasa_crypt.py +++ b/src/kasa_crypt/kasa_crypt.py @@ -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 diff --git a/tests/test_init.py b/tests/test_init.py new file mode 100644 index 0000000..9d73653 --- /dev/null +++ b/tests/test_init.py @@ -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) diff --git a/tests/test_main.py b/tests/test_main.py deleted file mode 100644 index 85f4c92..0000000 --- a/tests/test_main.py +++ /dev/null @@ -1,5 +0,0 @@ -from kasa_crypt.main import add - - -def test_add(): - assert add(1, 1) == 2