-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcryptonitehashmodule.c
174 lines (146 loc) · 4.53 KB
/
cryptonitehashmodule.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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 "cryptonite_hash.h"
#include <stdint.h>
static PyObject *cryptonite_hash(PyObject *self, PyObject *args)
{
char *output;
PyObject *value;
#if PY_MAJOR_VERSION >= 3
PyBytesObject *input;
#else
PyStringObject *input;
#endif
int aes_ni_supported;
if (!PyArg_ParseTuple(args, "Si", &input, &aes_ni_supported))
return NULL;
Py_INCREF(input);
output = (char*)PyMem_Malloc(32);
#if PY_MAJOR_VERSION >= 3
cryptonight_hash(output, (char *) PyBytes_AsString((PyObject*) input), aes_ni_supported);
#else
cryptonight_hash(output, (char *) PyString_AsString((PyObject*) input), aes_ni_supported);
#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 PyObject *cryptonite_scanhash(PyObject *self, PyObject *args)
{
#if PY_MAJOR_VERSION >= 3
PyBytesObject *input;
#else
PyStringObject *input;
#endif
int ret;
uint32_t target;
uint32_t max_nonce;
uint64_t hashes_done;
if (!PyArg_ParseTuple(args, "SII", &input, &target, &max_nonce))
return NULL;
Py_INCREF(input);
hashes_done = 0x0ULL;
#if PY_MAJOR_VERSION >= 3
ret = scanhash_cryptonight((char *)PyBytes_AsString((PyObject*)input), target, max_nonce, &hashes_done);
#else
ret = scanhash_cryptonight((char *)PyString_AsString((PyObject*)input), target, max_nonce, &hashes_done);
#endif
Py_DECREF(input);
#if PY_MAJOR_VERSION >= 3
return Py_BuildValue(("OK"), ret ? Py_True : Py_False, hashes_done);
#else
return Py_BuildValue(("OK"), ret ? Py_True : Py_False, hashes_done);
#endif
}
static PyObject *cpu_has_aes_in_supported(PyObject *self, PyObject *args)
{
bool ret;
ret = has_aes_ni();
return Py_BuildValue("O", ret ? Py_True : Py_False);
}
static PyObject *cryptolite_hash(PyObject *self, PyObject *args)
{
char *output;
PyObject *value;
#if PY_MAJOR_VERSION >= 3
PyBytesObject *input;
#else
PyStringObject *input;
#endif
int aes_ni_supported;
if (!PyArg_ParseTuple(args, "Si", &input, &aes_ni_supported))
return NULL;
Py_INCREF(input);
output = (char*)PyMem_Malloc(32);
#if PY_MAJOR_VERSION >= 3
cryptolight_hash(output, (char *)PyBytes_AsString((PyObject*)input), aes_ni_supported);
#else
cryptolight_hash(output, (char *)PyString_AsString((PyObject*)input), aes_ni_supported);
#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 PyObject *cryptolite_scanhash(PyObject *self, PyObject *args)
{
#if PY_MAJOR_VERSION >= 3
PyBytesObject *input;
#else
PyStringObject *input;
#endif
int ret;
uint32_t target;
uint32_t max_nonce;
uint64_t hashes_done;
if (!PyArg_ParseTuple(args, "SII", &input, &target, &max_nonce))
return NULL;
Py_INCREF(input);
hashes_done = 0x0ULL;
#if PY_MAJOR_VERSION >= 3
ret = scanhash_cryptolight((char *)PyBytes_AsString((PyObject*)input), target, max_nonce, &hashes_done);
#else
ret = scanhash_cryptolight((char *)PyString_AsString((PyObject*)input), target, max_nonce, &hashes_done);
#endif
Py_DECREF(input);
#if PY_MAJOR_VERSION >= 3
return Py_BuildValue(("OK"), ret ? Py_True : Py_False, hashes_done);
#else
return Py_BuildValue(("OK"), ret ? Py_True : Py_False, hashes_done);
#endif
}
static PyMethodDef CryptoniteMethods[] = {
{ "cryptonite_hash", cryptonite_hash, METH_VARARGS, "Hash with cryptonight algorithm" },
{ "cryptolite_hash", cryptolite_hash, METH_VARARGS, "Hash with cryptonight-light algorithm" },
{ "cpu_has_aes_in_supported", cpu_has_aes_in_supported, METH_VARARGS, "Check if CPU supports AES-NI" },
{ "cryptonite_scanhash", cryptonite_scanhash, METH_VARARGS, "Cryptonight scan for valid hash" },
{ "cryptolite_scanhash", cryptolite_scanhash, METH_VARARGS, "Cryptonight-lite scan for valid hash" },
{ NULL, NULL, 0, NULL }
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef CryptoniteModule = {
PyModuleDef_HEAD_INIT,
"cryptonite_hash",
"...",
-1,
CryptoniteMethods
};
PyMODINIT_FUNC PyInit_cryptonite_hash(void) {
return PyModule_Create(&CryptoniteModule);
}
#else
PyMODINIT_FUNC initcryptonite_hash(void) {
(void)Py_InitModule("cryptonite_hash", CryptoniteMethods);
}
#endif