From 4c1c6cf8e9c7f8da0270d566693bd0cdfe97e699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Mon, 27 Nov 2023 13:53:07 -0500 Subject: [PATCH] python: add aesEncrypt, aesDecrypt with password --- python/opendht.pyx | 20 ++++++++++++++++++++ python/opendht_cpp.pxd | 2 ++ 2 files changed, 22 insertions(+) diff --git a/python/opendht.pyx b/python/opendht.pyx index 39f3d8efd..26bac2af3 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 = data + cdef cpp.Blob indat + indat.assign(d_ptr, (d_ptr + d_len)) + cdef cpp.Blob encrypted = cpp.aesEncrypt(indat, password.encode()) + cdef char* encrypted_c_str = 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 = data + cdef cpp.Blob indat + indat.assign(d_ptr, (d_ptr + d_len)) + cdef cpp.Blob decrypted = cpp.aesDecrypt(indat, password.encode()) + cdef char* decrypted_c_str = 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 2d374d149..7b73f16c1 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()