Skip to content

Commit

Permalink
python: add aesEncrypt, aesDecrypt with password
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Béraud authored and aberaud committed Nov 28, 2023
1 parent 09046ec commit 4c1c6cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions python/opendht.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions python/opendht_cpp.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 4c1c6cf

Please sign in to comment.