Skip to content

Commit

Permalink
feat: add pyx
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jun 16, 2023
1 parent 0d60c86 commit 1ca559e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/kasa_crypt/_crypt_impl.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import cython

from libc.stdlib cimport malloc


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 char* unencrypted = <char *> malloc((len(encrypted)) * sizeof(char))
if not unencrypted:
return NULL # malloc failed
_decrypt_into(encrypted, unencrypted)
return unencrypted

cdef char* _encrypt(const char *unencrypted):
cdef char* encrypted = <char *> malloc((len(unencrypted)) * sizeof(char))
if not encrypted:
return NULL # malloc failed
_decrypt_into(unencrypted, encrypted)
return encrypted

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


def decrypt(string: bytes) -> str:
return _decrypt(string).decode('ascii')
4 changes: 2 additions & 2 deletions src/kasa_crypt/crypt_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <string.h>


void _encrypt_into(char * unencrypted, char * encrypted) {
void _encrypt_into(const char * unencrypted, char * encrypted) {
uint8_t unencrypted_byte;
uint8_t key = 171;
for(unsigned i = 0; i < strlen(unencrypted); i++) {
Expand All @@ -14,7 +14,7 @@ void _encrypt_into(char * unencrypted, char * encrypted) {
encrypted[i] = key;
}
}
void _decrypt_into(char * encrypted, char * unencrypted) {
void _decrypt_into(const char * encrypted, char * unencrypted) {
uint8_t unencrypted_byte;
uint8_t encrypted_byte;
uint8_t key = 171;
Expand Down

0 comments on commit 1ca559e

Please sign in to comment.