Skip to content

Commit

Permalink
combine getModulus and getModulusFromProtected (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuefeng-Zhu authored and Dexus committed Jul 6, 2016
1 parent de06db9 commit be54d61
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Where

Use `getModulus` to get the modulus for a certificate, a CSR or a private key. Modulus can be useful to check that a Private Key Matches a Certificate

pem.getModulus(certificate, callback)
pem.getModulus(certificate, [password,] callback)

Where

Expand Down
43 changes: 8 additions & 35 deletions lib/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports.readCertificateInfo = readCertificateInfo;
module.exports.getPublicKey = getPublicKey;
module.exports.getFingerprint = getFingerprint;
module.exports.getModulus = getModulus;
module.exports.getModulusFromProtected = getModulusFromProtected;
module.exports.getDhparamInfo = getDhparamInfo;
module.exports.createPkcs12 = createPkcs12;
module.exports.readPkcs12 = readPkcs12;
Expand Down Expand Up @@ -436,9 +435,15 @@ function readCertificateInfo(certificate, callback) {
* get the modulus from a certificate, a CSR or a private key
*
* @param {String} certificate PEM encoded, CSR PEM encoded, or private key
* @param {String} password password for the certificate
* @param {Function} callback Callback function with an error object and {modulus}
*/
function getModulus(certificate, callback) {
function getModulus(certificate, password, callback) {
if (!callback && typeof password === 'function') {
callback = password;
password = undefined;
}

certificate = Buffer.isBuffer(certificate) && certificate.toString() || certificate;

var type = '';
Expand All @@ -455,45 +460,13 @@ function getModulus(certificate, callback) {
'-in',
'--TMPFILE--'
];
spawnWrapper(params, certificate, function(err, code, stdout) {
if (err) {
return callback(err);
}
var match = stdout.match(/Modulus=([0-9a-fA-F]+)$/m);
if (match) {
return callback(null, {
modulus: match[1]
});
} else {
return callback(new Error('No modulus'));
}
});
}

function getModulusFromProtected(key, password, callback){
key = Buffer.isBuffer(key) && key.toString() || key;

var type = '';
if (key.match(/BEGIN(\sNEW)? CERTIFICATE REQUEST/)) {
type = 'req';
} else if (key.match(/BEGIN RSA PRIVATE KEY/) || key.match(/BEGIN PRIVATE KEY/)) {
type = 'rsa';
} else {
type = 'x509';
}
var params = [type,
'-noout',
'-modulus',
'-in',
'--TMPFILE--'
];

if (password) {
params.push( '-passin');
params.push( 'pass:' + password);
}

spawnWrapper(params, key, function(err, code, stdout) {
spawnWrapper(params, certificate, function(err, code, stdout) {
if (err) {
return callback(err);
}
Expand Down
2 changes: 1 addition & 1 deletion test/pem.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ exports['General Tests'] = {
test.ok(certmodulus);
test.ok(certmodulus.match(/^[0-9A-F]*$/));
test.ok(fs.readdirSync('./tmp').length === 0);
pem.getModulusFromProtected(key, 'password' ,function(error, data) {
pem.getModulus(key, 'password' ,function(error, data) {
var keymodulus = (data && data.modulus || '').toString();
test.ifError(error);
test.ok(keymodulus);
Expand Down

0 comments on commit be54d61

Please sign in to comment.