diff --git a/include/libdap-crypto-python.h b/include/libdap-crypto-python.h index 23a51c07197e4708fa8d3a4bd3aa1fbfdc072797..436697ae7736b62ad4d879edefe919c9b2882eba 100644 --- a/include/libdap-crypto-python.h +++ b/include/libdap-crypto-python.h @@ -1,5 +1,12 @@ #define PY_SSIZE_T_CLEAN #include <python3.7/Python.h> +#include "dap_enc.h" +#include "dap_enc_key.h" +#include "dap_common.h" +#include "wrapping_base58.h" +#include "wrapping_base64.h" + + #ifdef __cplusplus extern "C" { @@ -13,7 +20,11 @@ static PyObject *dap_crypto_deinit(); static PyMethodDef DapCryptoMethods[] = { {"init", dap_crypto_init, METH_VARARGS, "Initialization of the DAP (Deus Applicaions Prototypes) crypto library"}, - {"deinit", dap_crypto_init, METH_NOARGS, "Deinitialization of the DAP (Deus Applicaions Prototypes) crypto library"}, + {"deinit", dap_crypto_deinit, METH_NOARGS, "Deinitialization of the DAP (Deus Applicaions Prototypes) crypto library"}, + {"encode_base58", dap_encode_base58_py, METH_VARARGS, "Encrypts information using the base58 algorithm from the DAP crypto library"}, + {"decode_base58", dap_decode_base58_py, METH_VARARGS, "Dencrypts information using the base58 algorithm from the DAP crypto library"}, + {"encode_base64", dap_encode_base64_py, METH_VARARGS, "Encrypts information using the base64 algorithm from the DAP crypto library"}, + {"decode_base64", dap_decode_base64_py, METH_VARARGS, "Dencrypts information using the base64 algorithm from the DAP crypto library"}, {NULL, NULL, 0, NULL} }; diff --git a/include/wrapping_base58.h b/include/wrapping_base58.h new file mode 100644 index 0000000000000000000000000000000000000000..014faaa53ce949566e29b30040cb0ec9062b1916 --- /dev/null +++ b/include/wrapping_base58.h @@ -0,0 +1,6 @@ +#include "Python.h" +#include "dap_enc_base58.h" + +static PyObject *dap_encode_base58_py(PyObject *self, PyObject *args); + +static PyObject *dap_decode_base58_py(PyObject *self, PyObject *args); \ No newline at end of file diff --git a/include/wrapping_base64.h b/include/wrapping_base64.h new file mode 100644 index 0000000000000000000000000000000000000000..ec339d11682484eb3f7d86d4f9e4a994356570b9 --- /dev/null +++ b/include/wrapping_base64.h @@ -0,0 +1,6 @@ +#include "Python.h" +#include "dap_enc_base64.h" + +static PyObject *dap_encode_base64_py(PyObject *self, PyObject *args); + +static PyObject *dap_decode_base64_py(PyObject *self, PyObject *args); \ No newline at end of file diff --git a/src/libdap-crypto-python.c b/src/libdap-crypto-python.c index 8993d767a5161d86901316f864e5e1b2fb635772..a5810511b28d2e1364394448643f024bf251904d 100644 --- a/src/libdap-crypto-python.c +++ b/src/libdap-crypto-python.c @@ -1,10 +1,20 @@ #include "libdap-crypto-python.h" static PyObject* dap_crypto_init(PyObject *self, PyObject *args){ + if(dap_enc_init()!=0){ + log_it(L_CRITICAL,"Can't init encryption module"); + return PyLong_FromLong(-1); + } + if(dap_enc_key_init()!=0){ + log_it(L_CRITICAL,"Can't init encryption key module"); + return PyLong_FromLong(-2); + } return PyLong_FromLong(0); } static PyObject* dap_crypto_deinit(){ + dap_enc_key_deinit(); + dap_enc_deinit(); return PyLong_FromLong(0); } diff --git a/src/wrapping_base58.c b/src/wrapping_base58.c new file mode 100644 index 0000000000000000000000000000000000000000..2df8ac9157434ed3728fa1c0e361525181658c97 --- /dev/null +++ b/src/wrapping_base58.c @@ -0,0 +1,21 @@ +#include "wrapping_base58.h" + +static PyObject *dap_encode_base58_py(PyObject *self, PyObject *args){ + const char* data; + if (!PyArg_ParseTuple(self, "s", &data)){ + return NULL; + } + char* res; + dap_enc_base58_encode(data, strlen(data), res); + return Py_BuildValue("s", res); +} + +static PyObject *dap_decode_base58_py(PyObject *self, PyObject *args){ + const char* data; + if (!PyArg_ParseTuple(self, "s", &data)){ + return NULL; + } + char* res; + dap_enc_base58_decode(data, res); + return Py_BuildValue("s", res); +} \ No newline at end of file diff --git a/src/wrapping_base64.c b/src/wrapping_base64.c new file mode 100644 index 0000000000000000000000000000000000000000..05295e26f662fa264c2702395ae555de182883a8 --- /dev/null +++ b/src/wrapping_base64.c @@ -0,0 +1,21 @@ +#include "wrapping_base64.h" + +static PyObject *dap_encode_base64_py(PyObject *self, PyObject *args){ + const char* data; + if (!PyArg_ParseTuple(self, "s", &data)){ + return NULL; + } + char* res; + dap_enc_base64_encode(data, strlen(data),res, DAP_ENC_DATA_TYPE_B64); + return Py_BuildValue("s", res); +} + +static PyObject *dap_decode_base64_py(PyObject *self, PyObject *args){ + const char* data; + if (!PyArg_ParseTuple(self, "s", &data)){ + return NULL; + } + char* res; + dap_enc_base64_decode(data, strlen(data), res, DAP_ENC_DATA_TYPE_B64); + return Py_BuildValue("s", res); +} \ No newline at end of file