如何使用C++插件中内置模块的node.js

Dze*_*nly 7 javascript c++ v8 node.js node.js-addon

我需要在我的C++插件中使用内置模块'crypto'的node.js.我试图找到使用内置模块的C++插件的例子,但是失败了.我查看了node_crypto.h/.cc,与node.js加密文档,受保护的构造函数等相比,它具有如此不同的函数签名.node_crypto.h包含带有一个参数的InitCrypto()声明,但是node_crypto.cc没有这样的定义.功能.只有InitCrypto有四个参数.我试过用一个参数使用InitCrypto并得到"符号查找错误".

我可以将require('crypto')的结果传递给我的插件,然后使用这个对象,但这是不安全的.我们的JS代码可以在客户端的服务器上运行.

现在我觉得C++插件使用像openssl lib而不是内置节点模块'crypto'的smth更简单.

所以我需要一些使用C++插件的工作示例,它使用'crypto'模块或链接到一些关于此的文章.

使用C++插件中的任何内置模块的任何示例都会有所帮助.

pme*_*med 8

当需要加密/解密Nodejs插件中的数据时,我使用了相同的方法.

据我所知,类来自node_crypto.h用于在Nodejs中进行本机绑定,我不能在我的插件中使用它们.

然后我尝试使用Nodejs中的OpenSSL,但由于OpenSSL静态链接到Nodejs可执行文件,所以无法使用它.

之后我尝试从C++调用JavaScript代码,最后得到以下解决方案 - 从C++代码调用Nodejs函数:

using namespace v8;

// persistent handle for the crypto module
static Persistent<Object> node_crypto;

// Addon startup procedure
void Init(Local<Object> exports, Local<Object> module)
{
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    // get `require` function
    Local<Function> require = module->Get(String::NewFromUtf8(isolate, "require")).As<Function>();

    // call require('crypto')
    Local<Value> args[] = { String::NewFromUtf8(isolate, "crypto") };
    Local<Object> crypto = require->Call(module, 1, args).As<Object>();

    // store crypto module in persistent handle for further use
    node_crypto.Reset(isolate, crypto);   
}

NODE_MODULE(addon, Init);

// must be invoked in then Node main thread since the function uses V8 API
std::string encrypt(std::string const& key, std::string const& text)
{
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    // get local handle from persistent
    Local<Object> crypto = Local<Object>::New(isolate, node_crypto);

    // get `createCipher` function from the crypto module
    Local<Function> createCipher = crypto->Get(String::NewFromUtf8(isolate, "createCipher")).As<Function>();

    // call crypto.createCipher("aes256", key)
    Local<Value> create_args[] =
    {
        String::NewFromUtf8(isolate, "aes256"),
        String::NewFromUtf8(isolate, key.c_str())
    };
    Local<Object> cipher = createCipher->Call(crypto, 2, create_args).As<Object>();

    // get update and final functions from the crypto module
    Local<Function> update = cipher->Get(String::NewFromUtf8(isolate, "update")).As<Function>();
    Local<Function> final = cipher->Get(String::NewFromUtf8(isolate, "final")).As<Function>();

    // buf1 = cipher.update(text), buf2 = cipher.final()
    Local<Value> update_args[] = { node::Buffer::New(isolate, text.data(), text.size()) };

    Local<Value> buf1 = update->Call(cipher, 1, update_args);
    Local<Value> buf2 = final->Call(cipher, 0, nullptr);

    // concatenate update and final buffers into result string
    char const* const data1 = node::Buffer::Data(buf1);
    char const* const data2 = node::Buffer::Data(buf2);

    size_t const size1 = node::Buffer::Length(buf1);
    size_t const size2 = node::Buffer::Lenght(buf2);

    std::string result;
    result.reserve(size1 + size2);
    result.append(data1, size1);
    result.append(data2, size2);
    return result;
}

std::string decrypt(std::string const& key, std::string const& text)
{
    // similar as in encrypt, use createDecipher instead
}
Run Code Online (Sandbox Code Playgroud)

如您所见,使用V8 API的C++代码非常详细.在实际项目中,我使用了v8pp库中的实用程序函数来获取对象属性和调用函数,并将数据转换为V8句柄.

  • 谢谢!顺便说一句,Nodejs从版本6.4.0开始在Windows上导出OpenSSL符号(在pull请求https://github.com/nodejs/node/pull/7983中).所以来自node-gyp wiki(https://github.com/nodejs/node-gyp/wiki/Linking-to-OpenSSL)的方法也可行. (2认同)