-
Notifications
You must be signed in to change notification settings - Fork 162
/
multihashing.cc
181 lines (132 loc) · 4.92 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
#include <node.h>
#include <node_buffer.h>
#include <v8.h>
#include <stdint.h>
#include <nan.h>
#include "multihashing.h"
extern "C" {
#include "cryptonight.h"
#include "cryptonight_light.h"
}
#define THROW_ERROR_EXCEPTION(x) Nan::ThrowError(x)
void callback(char* data, void* hint) {
free(data);
}
using namespace node;
using namespace v8;
using namespace Nan;
NAN_METHOD(cryptonight) {
bool fast = false;
if (info.Length() < 1)
return THROW_ERROR_EXCEPTION("You must provide one argument.");
if (info.Length() >= 2) {
if(!info[1]->IsBoolean())
return THROW_ERROR_EXCEPTION("Argument 2 should be a boolean");
fast = info[1]->ToBoolean()->BooleanValue();
}
Local<Object> target = info[0]->ToObject();
if(!Buffer::HasInstance(target))
return THROW_ERROR_EXCEPTION("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
cryptonight_hash(input, output, input_len);
v8::Local<v8::Value> returnValue = Nan::CopyBuffer(output, 32).ToLocalChecked();
info.GetReturnValue().Set(
returnValue
);
}
class CNAsyncWorker : public Nan::AsyncWorker{
public:
CNAsyncWorker(Nan::Callback *callback, char * input, uint32_t input_len)
: Nan::AsyncWorker(callback), input(input), input_len(input_len){}
~CNAsyncWorker() {}
void Execute () {
cryptonight_hash(input, output, input_len);
}
void HandleOKCallback () {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Null()
, v8::Local<v8::Value>(Nan::CopyBuffer(output, 32).ToLocalChecked())
};
callback->Call(2, argv);
}
private:
uint32_t input_len;
char * input;
char output[32];
};
NAN_METHOD(CNAsync) {
if (info.Length() != 2)
return THROW_ERROR_EXCEPTION("You must provide two arguments.");
Local<Object> target = info[0]->ToObject();
Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
char * input = Buffer::Data(target);
uint32_t input_len = Buffer::Length(target);
Nan::AsyncQueueWorker(new CNAsyncWorker(callback, input, input_len));
}
class CNLAsyncWorker : public Nan::AsyncWorker{
public:
CNLAsyncWorker(Nan::Callback *callback, char * input, uint32_t input_len)
: Nan::AsyncWorker(callback), input(input), input_len(input_len){}
~CNLAsyncWorker() {}
void Execute () {
cryptonight_light_hash(input, output, input_len);
}
void HandleOKCallback () {
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
Nan::Null()
, v8::Local<v8::Value>(Nan::CopyBuffer(output, 32).ToLocalChecked())
};
callback->Call(2, argv);
}
private:
uint32_t input_len;
char * input;
char output[32];
};
NAN_METHOD(CNLAsync) {
if (info.Length() != 2)
return THROW_ERROR_EXCEPTION("You must provide two arguments.");
Local<Object> target = info[0]->ToObject();
Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
char * input = Buffer::Data(target);
uint32_t input_len = Buffer::Length(target);
Nan::AsyncQueueWorker(new CNLAsyncWorker(callback, input, input_len));
}
NAN_METHOD(cryptonight_light) {
bool fast = false;
if (info.Length() < 1)
return THROW_ERROR_EXCEPTION("You must provide one argument.");
if (info.Length() >= 2) {
if(!info[1]->IsBoolean())
return THROW_ERROR_EXCEPTION("Argument 2 should be a boolean");
fast = info[1]->ToBoolean()->BooleanValue();
}
Local<Object> target = info[0]->ToObject();
if(!Buffer::HasInstance(target))
return THROW_ERROR_EXCEPTION("Argument should be a buffer object.");
char * input = Buffer::Data(target);
char output[32];
uint32_t input_len = Buffer::Length(target);
if(fast)
cryptonight_light_fast_hash(input, output, input_len);
else
cryptonight_light_hash(input, output, input_len);
v8::Local<v8::Value> returnValue = Nan::CopyBuffer(output, 32).ToLocalChecked();
info.GetReturnValue().Set(
returnValue
);
}
NAN_MODULE_INIT(init) {
Nan::Set(target, Nan::New("cryptonight").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(cryptonight)).ToLocalChecked());
Nan::Set(target, Nan::New("CNAsync").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(CNAsync)).ToLocalChecked());
Nan::Set(target, Nan::New("cryptonight_light").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(cryptonight_light)).ToLocalChecked());
Nan::Set(target, Nan::New("CNLAsync").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(CNAsync)).ToLocalChecked());
}
NODE_MODULE(multihashing, init)