diff --git a/python/opendht.pyx b/python/opendht.pyx index 39f3d8efd54f0e70a11b3501896263dc653c5837..26bac2af388e965e3aaef15ee316b6326c6dfb3f 100644 --- a/python/opendht.pyx +++ b/python/opendht.pyx @@ -462,6 +462,26 @@ cdef class Identity(object): k._key = self._id.first return k +def aesEncrypt(bytes data, str password) -> bytes : + cdef size_t d_len = len(data) + cdef cpp.uint8_t* d_ptr = <cpp.uint8_t*>data + cdef cpp.Blob indat + indat.assign(d_ptr, <cpp.uint8_t*>(d_ptr + d_len)) + cdef cpp.Blob encrypted = cpp.aesEncrypt(indat, password.encode()) + cdef char* encrypted_c_str = <char *>encrypted.data() + cdef Py_ssize_t length = encrypted.size() + return encrypted_c_str[:length] + +def aesDecrypt(bytes data, str password) -> bytes : + cdef size_t d_len = len(data) + cdef cpp.uint8_t* d_ptr = <cpp.uint8_t*>data + cdef cpp.Blob indat + indat.assign(d_ptr, <cpp.uint8_t*>(d_ptr + d_len)) + cdef cpp.Blob decrypted = cpp.aesDecrypt(indat, password.encode()) + cdef char* decrypted_c_str = <char *>decrypted.data() + cdef Py_ssize_t length = decrypted.size() + return decrypted_c_str[:length] + cdef class DhtConfig(object): cdef cpp.DhtRunnerConfig _config def __init__(self): diff --git a/python/opendht_cpp.pxd b/python/opendht_cpp.pxd index 2d374d149935bbe0b2377c548b3a14e6f680bbc4..7b73f16c1c0053bbf2e4e761d2c10e076acbd0e1 100644 --- a/python/opendht_cpp.pxd +++ b/python/opendht_cpp.pxd @@ -94,6 +94,8 @@ ctypedef vector[uint8_t] Blob cdef extern from "opendht/crypto.h" namespace "dht::crypto": ctypedef pair[shared_ptr[PrivateKey], shared_ptr[Certificate]] Identity cdef Identity generateIdentity(string name, Identity ca, unsigned bits) + cdef Blob aesEncrypt(Blob data, string password) except + + cdef Blob aesDecrypt(Blob encrypted, string password) except + cdef cppclass PrivateKey: PrivateKey()