• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <openssl/rand.h>
2 #include <openssl/ssl.h>
3 #include <node.h>
4 #include <assert.h>
5 
6 namespace {
7 
RandomBytes(const v8::FunctionCallbackInfo<v8::Value> & info)8 inline void RandomBytes(const v8::FunctionCallbackInfo<v8::Value>& info) {
9   assert(info[0]->IsArrayBufferView());
10   auto view = info[0].As<v8::ArrayBufferView>();
11   auto byte_offset = view->ByteOffset();
12   auto byte_length = view->ByteLength();
13   assert(view->HasBuffer());
14   auto buffer = view->Buffer();
15   auto contents = buffer->GetBackingStore();
16   auto data = static_cast<unsigned char*>(contents->Data()) + byte_offset;
17   assert(RAND_poll());
18   auto rval = RAND_bytes(data, static_cast<int>(byte_length));
19   info.GetReturnValue().Set(rval > 0);
20 }
21 
Initialize(v8::Local<v8::Object> exports,v8::Local<v8::Value> module,v8::Local<v8::Context> context)22 inline void Initialize(v8::Local<v8::Object> exports,
23                        v8::Local<v8::Value> module,
24                        v8::Local<v8::Context> context) {
25   auto isolate = context->GetIsolate();
26   auto key = v8::String::NewFromUtf8(
27       isolate, "randomBytes").ToLocalChecked();
28   auto value = v8::FunctionTemplate::New(isolate, RandomBytes)
29                    ->GetFunction(context)
30                    .ToLocalChecked();
31   assert(exports->Set(context, key, value).IsJust());
32 
33   const SSL_METHOD* method = TLSv1_2_server_method();
34   assert(method != nullptr);
35 }
36 
37 }  // anonymous namespace
38 
39 NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)
40