-
Notifications
You must be signed in to change notification settings - Fork 5
/
avon.cc
167 lines (136 loc) · 3.06 KB
/
avon.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
#include <stdio.h>
#include <node.h>
#include "nan.h"
#include "blake2.h"
#include "avon.h"
#include "streamer.h"
#define HERE() ({fprintf(stderr, "@%d\n", __LINE__);})
using namespace v8;
//------ operate on files
class FileWorker : public Nan::AsyncWorker
{
public:
FileWorker(Nan::Callback *callback, int algo, char* fname)
: Nan::AsyncWorker(callback)
, algorithm(algo)
, filename(fname)
{
}
~FileWorker()
{
}
void Execute()
{
blake2fn streamFunc;
switch (algorithm)
{
case B:
streamFunc = blake2b_stream;
length = BLAKE2B_OUTBYTES;
break;
case BP:
streamFunc = blake2bp_stream;
length = BLAKE2B_OUTBYTES;
break;
case S:
streamFunc = blake2s_stream;
length = BLAKE2S_OUTBYTES;
break;
case SP:
streamFunc = blake2sp_stream;
length = BLAKE2S_OUTBYTES;
break;
default:
SetErrorMessage("Unknown hash type.");
return;
}
FILE *f = NULL;
f = fopen(filename, "rb");
if (!f)
{
SetErrorMessage("Could not open file to hash.");
return;
}
if (streamFunc(f, hash, length) < 0)
{
SetErrorMessage("Failed to calculate hash.");
return;
}
fclose(f);
}
void HandleOKCallback()
{
Nan::HandleScope();
Local<Value> argv[] =
{
Nan::Null(),
Nan::CopyBuffer(hash, length).ToLocalChecked()
};
callback->Call(2, argv);
};
private:
int algorithm;
char* filename;
size_t length;
// The 2S hashes emit 32 bytes instead of 64, so we get away with this size.
char hash[BLAKE2B_OUTBYTES];
};
NAN_METHOD(HashFile)
{
int algo = info[0]->Uint32Value();
Nan::Utf8String* name = new Nan::Utf8String(info[1]);
Nan::Callback *callback = new Nan::Callback(info[2].As<Function>());
Nan::AsyncQueueWorker(new FileWorker(callback, algo, **name));
}
NAN_METHOD(HashBufferSync)
{
int algo = info[0]->Uint32Value();
Local<Object> buffer = info[1].As<Object>();
size_t length = node::Buffer::Length(buffer);
char* data = node::Buffer::Data(buffer);
blake2bufferfn func;
size_t resultLen;
char hash[BLAKE2B_OUTBYTES];
switch (algo)
{
case B:
func = blake2b_buffer;
resultLen = BLAKE2B_OUTBYTES;
break;
case BP:
func = blake2bp_buffer;
resultLen = BLAKE2B_OUTBYTES;
break;
case S:
func = blake2s_buffer;
resultLen = BLAKE2S_OUTBYTES;
break;
case SP:
func = blake2sp_buffer;
resultLen = BLAKE2S_OUTBYTES;
break;
default:
Nan::ThrowTypeError("Unknown hash type.");
return;
}
if (func(data, length, hash) < 0)
{
Nan::ThrowTypeError("Failed to calculate hash.");
return;
}
info.GetReturnValue().Set(Nan::CopyBuffer(hash, resultLen).ToLocalChecked());
}
// ------------ ceremony
NAN_MODULE_INIT(InitAll)
{
Nan::Set(target,
Nan::New<String>("b2_file").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(HashFile)).ToLocalChecked()
);
Nan::Set(target,
Nan::New<String>("b2_buffer").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(HashBufferSync)).ToLocalChecked()
);
AvonStream::Initialize(target);
}
NODE_MODULE(blake2, InitAll)