javascript - NodeJS exec with binary from and to the process - Stack Overflow

I'm trying to write a function, that would use native openssl to do some RSA heavy-lifting for me,

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. 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
  3. Write the buffer to the stdin stream of the exec mand
  4. RSA encrypt/decrypt the data and write it to the stdout stream
  5. Get the input data back to a Buffer in the JS-process for further processing

The child process module in Node has an exec mand, but I fail to see how I can pipe the input to the process and pipe it back to my process. Basically I'd like to execute the following type of mand, but without having to rely on writing things to files (didn't check the exact syntax of 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.

Is there a clean way to do this in the way I drafted here? Is there some alternative way of using openssl for this, e.g. some native bindings for openssl lib, that would allow me to do this without relying on the mand line?

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. 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
  3. Write the buffer to the stdin stream of the exec mand
  4. RSA encrypt/decrypt the data and write it to the stdout stream
  5. Get the input data back to a Buffer in the JS-process for further processing

The child process module in Node has an exec mand, but I fail to see how I can pipe the input to the process and pipe it back to my process. Basically I'd like to execute the following type of mand, but without having to rely on writing things to files (didn't check the exact syntax of 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.

Is there a clean way to do this in the way I drafted here? Is there some alternative way of using openssl for this, e.g. some native bindings for openssl lib, that would allow me to do this without relying on the mand line?

Share Improve this question asked Nov 10, 2011 at 8:42 Kai InkinenKai Inkinen 2,6312 gold badges22 silver badges21 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

You've mentioned spawn but seem to think you can't use it. 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. Something very roughly like this pletely 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 es, 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("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..

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

I hope this helps!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745185831a4615634.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信