Skip to main content
added 327 characters in body
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

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 i // 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'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); ssl.stdout.on('data', function (data) { // Save up the result (or perhaps just call the callback repeatedly with i // as it comes, whatever) 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); }); // Write the buffer ssl.stdin.write(buffer_to_encrypt); } 

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); } 
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

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); ssl.stdout.on('data', function (data) { // Save up the result (or perhaps just call the callback repeatedly with i // as it comes, whatever) 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); }); // Write the buffer ssl.stdin.write(buffer_to_encrypt); }