Skip to content

Commit

Permalink
fix: ensure bytes with null chars are returned
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jun 16, 2023
1 parent bab1e20 commit 4a09a40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
43 changes: 27 additions & 16 deletions src/kasa_crypt/_crypt_impl.pyx
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
import cython

from libc.stdlib cimport malloc
from libc.stdlib cimport free, malloc
from libc.string cimport strlen


cdef extern from "crypt_wrapper.h":
void _encrypt_into(const char * unencrypted, char * encrypted)
void _decrypt_into(const char * encrypted, char * unencrypted)

cdef char* _decrypt(const char *encrypted):
cdef void _decrypt(const char *encrypted, char **unencrypted, Py_ssize_t *length):
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[:n]
unencrypted[0] = <char *> malloc((n + 1) * sizeof(char))
if not unencrypted[0]:
return # malloc failed
_decrypt_into(encrypted, unencrypted[0] )
length[0] = n

cdef char* _encrypt(const char *unencrypted):
cdef void _encrypt(const char *unencrypted, char** encrypted, Py_ssize_t *length):
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[:n]
encrypted[0] = <char *> malloc((n + 1) * sizeof(char))
if not encrypted[0]:
return # malloc failed
_encrypt_into(unencrypted, encrypted[0])
length[0] = n

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

cdef char* encrypted = NULL
cdef Py_ssize_t length = 0
_encrypt(string.encode('utf-8'), &encrypted, &length)
try:
return encrypted[:length]
finally:
free(encrypted)

def decrypt(string: bytes) -> str:
return _decrypt(string).decode('utf-8')
cdef char* unencrypted = NULL
cdef Py_ssize_t length = 0
_decrypt(string, &unencrypted, &length)
try:
return unencrypted[:length].decode('utf-8')
finally:
free(unencrypted)
4 changes: 1 addition & 3 deletions src/kasa_crypt/crypt_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ void _encrypt_into(const char * unencrypted, char * encrypted) {
key = key ^ unencrypted_byte;
encrypted[i] = key;
}
encrypted[len] = '\0';
}
void _decrypt_into(const char * encrypted, char * unencrypted) {
uint8_t unencrypted_byte;
uint8_t encrypted_byte;
uint8_t key = 171;
unsigned long len = strlen(encrypted);
for(unsigned i = 0; i < strlen(encrypted); i++) {
for(unsigned i = 0; i < len; i++) {
encrypted_byte = encrypted[i];
unencrypted_byte = key ^ encrypted_byte;
key = encrypted_byte;
unencrypted[i] = unencrypted_byte;
}
unencrypted[len] = '\0';
}

0 comments on commit 4a09a40

Please sign in to comment.