-
Notifications
You must be signed in to change notification settings - Fork 3
/
cryptohellohashmodule.c
66 lines (54 loc) · 1.54 KB
/
cryptohellohashmodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2017 The Sumokoin Projects
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <Python.h>
#include "cryptohello_hash.h"
#include <stdint.h>
static PyObject *cryptohello_hash(PyObject *self, PyObject *args)
{
char *output;
PyObject *value;
#if PY_MAJOR_VERSION >= 3
PyBytesObject *input;
#else
PyStringObject *input;
#endif
int inputlen;
if (!PyArg_ParseTuple(args, "Si", &input, &inputlen))
return NULL;
Py_INCREF(input);
output = (char*)PyMem_Malloc(32);
#if PY_MAJOR_VERSION >= 3
CryptoHello_Hash(output, (char *) PyBytes_AsString((PyObject*) input), inputlen);
#else
CryptoHello_Hash(output, (char *) PyString_AsString((PyObject*) input), inputlen);
#endif
Py_DECREF(input);
#if PY_MAJOR_VERSION >= 3
value = Py_BuildValue("y#", output, 32);
#else
value = Py_BuildValue("s#", output, 32);
#endif
PyMem_Free(output);
return value;
}
static PyMethodDef CryptohelloMethods[] = {
{ "cryptohello_hash", cryptohello_hash, METH_VARARGS, "Hash with cryptohello algorithm" },
{ NULL, NULL, 0, NULL }
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef CryptohelloModule = {
PyModuleDef_HEAD_INIT,
"cryptohello_hash",
"...",
-1,
CryptohelloMethods
};
PyMODINIT_FUNC PyInit_cryptohello_hash(void) {
return PyModule_Create(&CryptohelloModule);
}
#else
PyMODINIT_FUNC initcryptohello_hash(void) {
(void)Py_InitModule("cryptohello_hash", CryptohelloMethods);
}
#endif