这段代码可以帮助你。专为 PKCS7 设计,但您可以根据需要修改 de openssl 命令行。
var util = require('util');
var spawn = require('child_process').spawn;
var Promise = require('promise');
// Expose methods.
exports.sign = sign;
/**
* Sign a file.
*
* @param {object} options Options
* @param {stream.Readable} options.content Content stream
* @param {string} options.key Key path
* @param {string} options.cert Cert path
* @param {string} [options.password] Key password
* @param {function} [cb] Optional callback
* @returns {object} result Result
* @returns {string} result.pem Pem signature
* @returns {string} result.der Der signature
* @returns {string} result.stdout Strict stdout
* @returns {string} result.stderr Strict stderr
* @returns {ChildProcess} result.child Child process
*/
function sign(options, cb) {
return new Promise(function (resolve, reject) {
options = options || {};
if (!options.content)
throw new Error('Invalid content.');
if (!options.key)
throw new Error('Invalid key.');
if (!options.cert)
throw new Error('Invalid certificate.');
var command = util.format(
'openssl smime -sign -text -signer %s -inkey %s -outform DER -nodetach',
options.cert,
options.key
);
if (options.password)
command += util.format(' -passin pass:%s', options.password);
var args = command.split(' ');
var child = spawn(args[0], args.splice(1));
var der = [];
child.stdout.on('data', function (chunk) {
der.push(chunk);
});
child.on('close', function (code) {
if (code !== 0)
reject(new Error('Process failed.'));
else
resolve({
child: child,
der: Buffer.concat(der)
});
});
options.content.pipe(child.stdin);
})
.nodeify(cb);
}
我的文件名为:signHelper。这是调用它的代码:
signHelper.sign({
content: s,
key: path.join(__dirname, '../certs/test/' + "keyfile.key")//,
cert: path.join(__dirname, '../certs/test/' + "certfile.crt"),
password: 'password'
}).catch(function (err) {
logger.error("Error signing: " + err.stack);
callback(err);
}).then(function (result) {
logger.info("signTicket ++++++++++++");
callback(null, result.der); //result.der is the signed certificate
});
您只需要了解如何使用 openssl 做您需要的事情。我希望这个对你有用。