commit 443d11f56ef2f62783329ffef3e58be432372870
parent 1a6abf739e6d6971bfc318d1944ff09e426325aa
Author: nolash <dev@holbrook.no>
Date: Mon, 13 Sep 2021 21:42:19 +0200
Add flags to cli
Diffstat:
2 files changed, 44 insertions(+), 9 deletions(-)
diff --git a/src/python_swarm.c b/src/python_swarm.c
@@ -4,11 +4,11 @@
#include "bmt.h"
+
static PyObject* method_bmt(PyObject *self, PyObject *args) {
bmt_t bmt_content;
const char *input;
- //Py_ssize_t *input_length;
- int input_length;
+ Py_ssize_t input_length;
long long data_length;
int r;
@@ -18,16 +18,17 @@ static PyObject* method_bmt(PyObject *self, PyObject *args) {
}
bmt_init(&bmt_content, (char*)input, input_length, data_length);
bmt_sum(&bmt_content);
- //return PyLong_FromLong(0);
return Py_BuildValue("y", &bmt_content.buf);
}
+
static PyMethodDef SwarmMethods[] = {
{"bmt", method_bmt, METH_VARARGS, "Calculate the BMT hash of the given data"},
{NULL, NULL, 0, NULL},
};
-static struct PyModuleDef bmtmodule = {
+
+static struct PyModuleDef swarmmodule = {
PyModuleDef_HEAD_INIT,
"swarm",
NULL,
@@ -35,6 +36,7 @@ static struct PyModuleDef bmtmodule = {
SwarmMethods,
};
+
PyMODINIT_FUNC PyInit_swarm(void) {
- return PyModule_Create(&bmtmodule);
+ return PyModule_Create(&swarmmodule);
}
diff --git a/test.py b/test.py
@@ -1,6 +1,39 @@
-import swarm
+# standard imports
+import argparse
+import sys
+import logging
+
+logging.basicConfig(level=logging.WARNING)
+logg = logging.getLogger()
+
+argparser = argparse.ArgumentParser()
+argparser.add_argument('-n', action='store_true', help='skip newline at end of output')
+argparser.add_argument('-b', action='store_true', help='output raw bytes')
+argparser.add_argument('-l', dest='data_length', type=int, help='length of data represented by preimage')
+argparser.add_argument('-v', action='store_true', help='verbose output')
+argparser.add_argument('data', type=str, help='data input for BMT hasher')
+largs = argparser.parse_args(sys.argv[1:])
+
+if largs.v:
+ logg.setLevel(logging.DEBUG)
-print('swaaarm {}'.format(swarm))
+input_data = largs.data.encode('utf-8')
+input_data_length = len(input_data)
+data_length = input_data_length
+if largs.data_length != None:
+ data_length = largs.data_length
+
+logg.info('hashing {} bytes input with {} length prefix'.format(int(input_data_length), int(data_length)))
+logg.debug('data "{}"'.format(input_data.hex()))
+
+# TODO: why does this segfault just one line before?
+import swarm
+r = swarm.bmt(input_data, input_data_length, data_length)
-b = swarm.bmt(b'foo', 3, 3)
-print('{}'.format(b[:32].hex()))
+if largs.b:
+ sys.stdout.buffer.write(r[:32])
+else:
+ s = '{}'.format(r[:32].hex())
+ if not largs.n:
+ s += '\n'
+ sys.stdout.write(s)