From 6db1e61244ce230b7134a16e66d0b3871a36df60 Mon Sep 17 00:00:00 2001 From: Alexey Stratulat <alexey.stratulat@demlabs.net> Date: Mon, 1 Jul 2019 23:40:53 +0700 Subject: [PATCH] [+] Added packaging for dap_enc_base58_encode and dap_enc _base58_decode. Also created the basis for dap_enc_base64_encode and dap_enc _base64_decode. --- include/libdap-crypto-python.h | 13 ++++++++++++- include/wrapping_base58.h | 6 ++++++ include/wrapping_base64.h | 6 ++++++ src/libdap-crypto-python.c | 10 ++++++++++ src/wrapping_base58.c | 21 +++++++++++++++++++++ src/wrapping_base64.c | 21 +++++++++++++++++++++ 6 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 include/wrapping_base58.h create mode 100644 include/wrapping_base64.h create mode 100644 src/wrapping_base58.c create mode 100644 src/wrapping_base64.c diff --git a/include/libdap-crypto-python.h b/include/libdap-crypto-python.h index 23a51c07..436697ae 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 00000000..014faaa5 --- /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 00000000..ec339d11 --- /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 8993d767..a5810511 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 00000000..2df8ac91 --- /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 00000000..05295e26 --- /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 -- GitLab