Skip to content

Commit

Permalink
fix: handle 0s in the byte stream
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jun 16, 2023
1 parent ab3b852 commit 532bd3c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/kasa_crypt/_crypt_impl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ cdef extern from "crypt_wrapper.h":
void _decrypt_into(const char * encrypted, char * unencrypted)

cdef char* _decrypt(const char *encrypted):
cdef char* unencrypted = <char *> malloc(((strlen(encrypted)) + 1) * sizeof(char))
cdef Py_ssize_t n = strlen(encrypted)
cdef char* unencrypted = <char *> malloc((n + 1) * sizeof(char))
if not unencrypted:
return NULL # malloc failed
_decrypt_into(encrypted, unencrypted)
return unencrypted
return unencrypted[:n]

cdef char* _encrypt(const char *unencrypted):
cdef char* encrypted = <char *> malloc(((strlen(unencrypted)) + 1) * sizeof(char))
cdef Py_ssize_t n = strlen(unencrypted)
cdef char* encrypted = <char *> malloc((n + 1) * sizeof(char))
if not encrypted:
return NULL # malloc failed
_encrypt_into(unencrypted, encrypted)
return encrypted
return encrypted[:n]

def encrypt(string: str) -> bytes:
return _encrypt(string.encode('utf-8'))
Expand Down

0 comments on commit 532bd3c

Please sign in to comment.