-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing.cpp
50 lines (38 loc) · 1.24 KB
/
hashing.cpp
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
#include <node.h>
#include <node_buffer.h>
#include <stdint.h>
#include <v8.h>
extern "C" {
#include "yescrypt.h"
}
using namespace node;
using namespace v8;
// NODE_MAJOR_VERSION >=13
void yescrypt(const FunctionCallbackInfo<Value> &args) {
v8::Isolate *isolate = args.GetIsolate();
if (args.Length() < 1) {
do {
isolate->ThrowException(Exception::Error(
String::NewFromUtf8(isolate, "You must provide one argument.")
.ToLocalChecked()));
return;
} while (0);
}
Local<Context> context = isolate->GetCurrentContext();
Local<Object> target = args[0]->ToObject(context).ToLocalChecked();
if (!Buffer::HasInstance(target)) {
do {
isolate->ThrowException(Exception::Error(
String::NewFromUtf8(isolate, "Argument should be a buffer object.")
.ToLocalChecked()));
return;
} while (0);
}
char *input = Buffer::Data(target);
char output[32];
uint32_t input_len = Buffer::Length(target);
yescrypt_hash(input, output);
args.GetReturnValue().Set(Buffer::Copy(isolate, output, 32).ToLocalChecked());
}
void init(Local<Object> exports) { NODE_SET_METHOD(exports, "hash", yescrypt); }
NODE_MODULE(hashing, init)