1

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 command
  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 command, 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 command, 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 command line?

0

2 Answers 2

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 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); } 
Sign up to request clarification or add additional context in comments.

Comments

1

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.