forked from zone117x/node-multi-hashing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultihashing.cc
299 lines (229 loc) · 8.18 KB
/
multihashing.cc
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <node.h>
#include <node_buffer.h>
#include <v8.h>
#include <stdint.h>
extern "C" {
#include "bcrypt.h"
#include "blake.h"
#include "c11.h"
#include "cryptonight.h"
#include "fresh.h"
#include "fugue.h"
#include "groestl.h"
#include "hefty1.h"
#include "keccak.h"
#include "nist5.h"
#include "quark.h"
#include "qubit.h"
#include "scryptjane.h"
#include "scryptn.h"
#include "sha1.h"
#include "shavite3.h"
#include "skein.h"
#include "x11.h"
#include "x13.h"
#include "x15.h"
}
#include "boolberry.h"
using namespace node;
using namespace v8;
#if NODE_MAJOR_VERSION >= 4
#define DECLARE_INIT(x) \
void x(Local<Object> exports)
#define DECLARE_FUNC(x) \
void x(const FunctionCallbackInfo<Value>& args)
#define DECLARE_SCOPE \
v8::Isolate* isolate = args.GetIsolate();
#define SET_BUFFER_RETURN(x, len) \
args.GetReturnValue().Set(Buffer::Copy(isolate, x, len).ToLocalChecked());
#define SET_BOOLEAN_RETURN(x) \
args.GetReturnValue().Set(Boolean::New(isolate, x));
#define RETURN_EXCEPT(msg) \
do { \
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, msg))); \
return; \
} while (0)
#else
#define DECLARE_INIT(x) \
void x(Handle<Object> exports)
#define DECLARE_FUNC(x) \
Handle<Value> x(const Arguments& args)
#define DECLARE_SCOPE \
HandleScope scope
#define SET_BUFFER_RETURN(x, len) \
do { \
Buffer* buff = Buffer::New(x, len); \
return scope.Close(buff->handle_); \
} while (0)
#define SET_BOOLEAN_RETURN(x) \
return scope.Close(Boolean::New(x));
#define RETURN_EXCEPT(msg) \
return ThrowException(Exception::Error(String::New(msg)))
#endif // NODE_MAJOR_VERSION
#define DECLARE_CALLBACK(name, hash, output_len) \
DECLARE_FUNC(name) { \
DECLARE_SCOPE; \
\
if (args.Length() < 1) \
RETURN_EXCEPT("You must provide one argument."); \
\
Local<Object> target = args[0]->ToObject(); \
\
if(!Buffer::HasInstance(target)) \
RETURN_EXCEPT("Argument should be a buffer object."); \
\
char * input = Buffer::Data(target); \
char output[32]; \
\
uint32_t input_len = Buffer::Length(target); \
\
hash(input, output, input_len); \
\
SET_BUFFER_RETURN(output, output_len); \
}
DECLARE_CALLBACK(bcrypt, bcrypt_hash, 32);
DECLARE_CALLBACK(blake, blake_hash, 32);
DECLARE_CALLBACK(c11, c11_hash, 32);
DECLARE_CALLBACK(fresh, fresh_hash, 32);
DECLARE_CALLBACK(fugue, fugue_hash, 32);
DECLARE_CALLBACK(groestl, groestl_hash, 32);
DECLARE_CALLBACK(groestlmyriad, groestlmyriad_hash, 32);
DECLARE_CALLBACK(hefty1, hefty1_hash, 32);
DECLARE_CALLBACK(keccak, keccak_hash, 32);
DECLARE_CALLBACK(nist5, nist5_hash, 32);
DECLARE_CALLBACK(quark, quark_hash, 32);
DECLARE_CALLBACK(qubit, qubit_hash, 32);
DECLARE_CALLBACK(sha1, sha1_hash, 32);
DECLARE_CALLBACK(shavite3, shavite3_hash, 32);
DECLARE_CALLBACK(skein, skein_hash, 32);
DECLARE_CALLBACK(x11, x11_hash, 32);
DECLARE_CALLBACK(x13, x13_hash, 32);
DECLARE_CALLBACK(x15, x15_hash, 32);
DECLARE_FUNC(scrypt) {
DECLARE_SCOPE;
if (args.Length() < 3)
RETURN_EXCEPT("You must provide buffer to hash, N value, and R value");
Local<Object> target = args[0]->ToObject();
if(!Buffer::HasInstance(target))
RETURN_EXCEPT("Argument should be a buffer object.");
unsigned int nValue = args[1]->Uint32Value();
unsigned int rValue = args[2]->Uint32Value();
char * input = Buffer::Data(target);
char output[32];
uint32_t input_len = Buffer::Length(target);
scrypt_N_R_1_256(input, output, nValue, rValue, input_len);
SET_BUFFER_RETURN(output, 32);
}
DECLARE_FUNC(scryptn) {
DECLARE_SCOPE;
if (args.Length() < 2)
RETURN_EXCEPT("You must provide buffer to hash and N factor.");
Local<Object> target = args[0]->ToObject();
if(!Buffer::HasInstance(target))
RETURN_EXCEPT("Argument should be a buffer object.");
unsigned int nFactor = args[1]->Uint32Value();
char * input = Buffer::Data(target);
char output[32];
uint32_t input_len = Buffer::Length(target);
//unsigned int N = 1 << (getNfactor(input) + 1);
unsigned int N = 1 << nFactor;
scrypt_N_R_1_256(input, output, N, 1, input_len); //hardcode for now to R=1 for now
SET_BUFFER_RETURN(output, 32);
}
DECLARE_FUNC(scryptjane) {
DECLARE_SCOPE;
if (args.Length() < 5)
RETURN_EXCEPT("You must provide two argument: buffer, timestamp as number, and nChainStarTime as number, nMin, and nMax");
Local<Object> target = args[0]->ToObject();
if(!Buffer::HasInstance(target))
RETURN_EXCEPT("First should be a buffer object.");
int timestamp = args[1]->Int32Value();
int nChainStartTime = args[2]->Int32Value();
int nMin = args[3]->Int32Value();
int nMax = args[4]->Int32Value();
char * input = Buffer::Data(target);
char output[32];
uint32_t input_len = Buffer::Length(target);
scryptjane_hash(input, input_len, (uint32_t *)output, GetNfactorJane(timestamp, nChainStartTime, nMin, nMax));
SET_BUFFER_RETURN(output, 32);
}
DECLARE_FUNC(cryptonight) {
DECLARE_SCOPE;
bool fast = false;
uint32_t cn_variant = 0;
if (args.Length() < 1)
RETURN_EXCEPT("You must provide one argument.");
if (args.Length() >= 2) {
if(args[1]->IsBoolean())
fast = args[1]->BooleanValue();
else if(args[1]->IsUint32())
cn_variant = args[1]->Uint32Value();
else
RETURN_EXCEPT("Argument 2 should be a boolean or uint32_t");
}
Local<Object> target = args[0]->ToObject();
if(!Buffer::HasInstance(target))
RETURN_EXCEPT("Argument should be a buffer object.");
char * input = Buffer::Data(target);
char output[32];
uint32_t input_len = Buffer::Length(target);
if(fast)
cryptonight_fast_hash(input, output, input_len);
else {
if (cn_variant > 0 && input_len < 43)
RETURN_EXCEPT("Argument must be 43 bytes for monero variant 1+");
cryptonight_hash(input, output, input_len, cn_variant);
}
SET_BUFFER_RETURN(output, 32);
}
DECLARE_FUNC(boolberry) {
DECLARE_SCOPE;
if (args.Length() < 2)
RETURN_EXCEPT("You must provide two arguments.");
Local<Object> target = args[0]->ToObject();
Local<Object> target_spad = args[1]->ToObject();
uint32_t height = 1;
if(!Buffer::HasInstance(target))
RETURN_EXCEPT("Argument 1 should be a buffer object.");
if(!Buffer::HasInstance(target_spad))
RETURN_EXCEPT("Argument 2 should be a buffer object.");
if(args.Length() >= 3) {
if(args[2]->IsUint32())
height = args[2]->Uint32Value();
else
RETURN_EXCEPT("Argument 3 should be an unsigned integer.");
}
char * input = Buffer::Data(target);
char * scratchpad = Buffer::Data(target_spad);
char output[32];
uint32_t input_len = Buffer::Length(target);
uint64_t spad_len = Buffer::Length(target_spad);
boolberry_hash(input, input_len, scratchpad, spad_len, output, height);
SET_BUFFER_RETURN(output, 32);
}
DECLARE_INIT(init) {
NODE_SET_METHOD(exports, "bcrypt", bcrypt);
NODE_SET_METHOD(exports, "blake", blake);
NODE_SET_METHOD(exports, "boolberry", boolberry);
NODE_SET_METHOD(exports, "c11", c11);
NODE_SET_METHOD(exports, "cryptonight", cryptonight);
NODE_SET_METHOD(exports, "fresh", fresh);
NODE_SET_METHOD(exports, "fugue", fugue);
NODE_SET_METHOD(exports, "groestl", groestl);
NODE_SET_METHOD(exports, "groestlmyriad", groestlmyriad);
NODE_SET_METHOD(exports, "hefty1", hefty1);
NODE_SET_METHOD(exports, "keccak", keccak);
NODE_SET_METHOD(exports, "nist5", nist5);
NODE_SET_METHOD(exports, "quark", quark);
NODE_SET_METHOD(exports, "qubit", qubit);
NODE_SET_METHOD(exports, "scrypt", scrypt);
NODE_SET_METHOD(exports, "scryptjane", scryptjane);
NODE_SET_METHOD(exports, "scryptn", scryptn);
NODE_SET_METHOD(exports, "sha1", sha1);
NODE_SET_METHOD(exports, "shavite3", shavite3);
NODE_SET_METHOD(exports, "skein", skein);
NODE_SET_METHOD(exports, "x11", x11);
NODE_SET_METHOD(exports, "x13", x13);
NODE_SET_METHOD(exports, "x15", x15);
}
NODE_MODULE(multihashing, init)