简体   繁体   English

NodeJS exec与进程之间的二进制文件

[英]NodeJS exec with binary from and to the process

I'm trying to write a function, that would use native openssl to do some RSA heavy-lifting for me, rather than using a js RSA library. 我正在尝试编写一个函数,该函数将使用本机openssl为我执行一些RSA重载操作,而不是使用js RSA库。 The target is to 目标是

  1. Read binary data from a file 从文件中读取二进制数据
  2. Do some processing in the node process, using JS, resulting in a Buffer containing binary data 使用JS在节点进程中进行一些处理,从而导致包含二进制数据的Buffer
  3. Write the buffer to the stdin stream of the exec command 将缓冲区写入exec命令的stdin流
  4. RSA encrypt/decrypt the data and write it to the stdout stream RSA加密/解密数据并将其写入标准输出流
  5. Get the input data back to a Buffer in the JS-process for further processing 将输入数据返回到JS进程中的Buffer进行进一步处理

The child process module in Node has an exec command, but I fail to see how I can pipe the input to the process and pipe it back to my process. Node中的子流程模块具有exec命令,但是我看不到如何将输入传递给流程并将其传递回我的流程。 Basically I'd like to execute the following type of command, but without having to rely on writing things to files (didn't check the exact syntax of openssl) 基本上,我想执行以下类型的命令,但不必依赖于将事情写到文件中(不必检查openssl的确切语法)

cat the_binary_file.data | openssl -encrypt -inkey key_file.pem -certin > the_output_stream

I could do this by writing a temp file, but I'd like to avoid it, if possible. 我可以通过写一个临时文件来做到这一点,但是如果可能的话,我想避免它。 Spawning a child process allows me access to stdin/out but haven't found this functionality for exec. 生成子进程使我可以访问stdin / out,但尚未找到exec的此功能。

Is there a clean way to do this in the way I drafted here? 有没有一种干净的方法可以按照我在这里草拟的方式进行? Is there some alternative way of using openssl for this, eg some native bindings for openssl lib, that would allow me to do this without relying on the command line? 是否有一些使用openssl的替代方法,例如,一些针对openssl lib的本机绑定,可以让我无需依赖命令行即可执行此操作?

You've mentioned spawn but seem to think you can't use it. 您已经提到了spawn但似乎认为您无法使用它。 Possibly showing my ignorance here, but it seems like it should be just what you're looking for: Launch openssl via spawn , then write to child.stdin and read from child.stdout . 可能在这里显示出我的无知,但似乎应该正是您想要的:通过spawn启动openssl,然后写入child.stdin并从child.stdout读取。 Something very roughly like this completely untested code: 类似于完全未经测试的代码:

var util  = require('util'),
    spawn = require('child_process').spawn;

function sslencrypt(buffer_to_encrypt, callback) {
    var ssl = spawn('openssl', ['-encrypt', '-inkey', ',key_file.pem', '-certin']),
        result = new Buffer(SOME_APPROPRIATE_SIZE),
        resultSize = 0;

    ssl.stdout.on('data', function (data) {
        // Save up the result (or perhaps just call the callback repeatedly
        // with it as it comes, whatever)
        if (data.length + resultSize > result.length) {
            // Too much data, our SOME_APPROPRIATE_SIZE above wasn't big enough
        }
        else {
            // Append to our buffer
            resultSize += data.length;
            data.copy(result);
        }
    });

    ssl.stderr.on('data', function (data) {
        // Handle error output
    });

    ssl.on('exit', function (code) {
        // Done, trigger your callback (perhaps check `code` here)
        callback(result, resultSize);
    });

    // Write the buffer
    ssl.stdin.write(buffer_to_encrypt);
}

You should be able to set encoding to binary when you make a call to exec, like.. 调用exec时,应该应该能够将编码设置为二进制,例如。

exec("openssl output_something_in_binary", {encoding: 'binary'}, function(err, out, err) {
   //do something with out - which is in the binary format
});

If you want to write out the content of "out" in binary, make sure to set the encoding to binary again, like.. 如果要用二进制写出“ out”的内容,请确保再次将编码设置为二进制,例如。

fs.writeFile("out.bin", out, {encoding: 'binary'});

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM