-
Notifications
You must be signed in to change notification settings - Fork 30.1k
/
binding.cc
61 lines (53 loc) · 2.12 KB
/
binding.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
#include <assert.h>
#include <node.h>
#include <openssl/md5.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
namespace {
inline void RandomBytes(const v8::FunctionCallbackInfo<v8::Value>& info) {
assert(info[0]->IsArrayBufferView());
auto view = info[0].As<v8::ArrayBufferView>();
auto byte_offset = view->ByteOffset();
auto byte_length = view->ByteLength();
assert(view->HasBuffer());
auto buffer = view->Buffer();
auto contents = buffer->GetBackingStore();
auto data = static_cast<unsigned char*>(contents->Data()) + byte_offset;
assert(RAND_poll());
auto rval = RAND_bytes(data, static_cast<int>(byte_length));
info.GetReturnValue().Set(rval > 0);
}
inline void Hash(const v8::FunctionCallbackInfo<v8::Value>& info) {
assert(info[0]->IsArrayBufferView());
auto view = info[0].As<v8::ArrayBufferView>();
auto byte_offset = view->ByteOffset();
auto len = view->ByteLength();
assert(view->HasBuffer());
auto buffer = view->Buffer();
auto contents = buffer->GetBackingStore();
auto data = static_cast<unsigned char*>(contents->Data()) + byte_offset;
unsigned char md[MD5_DIGEST_LENGTH];
MD5_CTX c;
auto rval = MD5_Init(&c) && MD5_Update(&c, data, len) && MD5_Final(md, &c);
info.GetReturnValue().Set(rval > 0);
}
inline void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> context) {
auto isolate = context->GetIsolate();
auto key = v8::String::NewFromUtf8(
isolate, "randomBytes").ToLocalChecked();
auto value = v8::FunctionTemplate::New(isolate, RandomBytes)
->GetFunction(context)
.ToLocalChecked();
assert(exports->Set(context, key, value).IsJust());
const SSL_METHOD* method = TLSv1_2_server_method();
assert(method != nullptr);
key = v8::String::NewFromUtf8(isolate, "hash").ToLocalChecked();
value = v8::FunctionTemplate::New(isolate, Hash)
->GetFunction(context)
.ToLocalChecked();
assert(exports->Set(context, key, value).IsJust());
}
} // anonymous namespace
NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)