This commit is contained in:
AJ ONeal 2016-08-02 12:35:23 -04:00
parent 7e6a2ddba2
commit 324bf5f6d5
3 changed files with 34 additions and 55 deletions

View File

@ -1,39 +1,9 @@
'use strict'; 'use strict';
//var crypto = require('crypto'); function binstrToB64Url(binstr) {
var forge = require('node-forge'); return new Buffer(binstr, 'binary').toString('base64')
.replace(/[+]/g, "-").replace(/\//g, "_").replace(/=/g,"");
function binstrToB64(binstr) {
return new Buffer(binstr, 'binary').toString('base64');
} }
function b64ToBinstr(b64) {
return new Buffer(b64, 'b64').toString('binary');
}
/*
importPemPrivateKey: function(pem) {
var key = forge.pki.privateKeyFromPem(pem);
return {
privateKey: exportPrivateKey(key),
publicKey: exportPublicKey(key)
};
},
importPemCertificate: function(pem) {
return forge.pki.certificateFromPem(pem);
},
privateKeyToPem: function(privateKey) {
var priv = importPrivateKey(privateKey);
return forge.pki.privateKeyToPem(priv);
},
certificateToPem: function(certificate) {
var derCert = base64ToBytes(certificate);
var cert = forge.pki.certificateFromAsn1(forge.asn1.fromDer(derCert));
return forge.pki.certificateToPem(cert);
},
*/
var extrac = module.exports = { var extrac = module.exports = {
// //
@ -44,22 +14,22 @@ var extrac = module.exports = {
return { return {
kty: "RSA" kty: "RSA"
, n: binstrToB64(k.n.toByteArray()) , n: binstrToB64Url(k.n.toByteArray())
, e: binstrToB64(k.e.toByteArray()) , e: binstrToB64Url(k.e.toByteArray())
, d: binstrToB64(k.d.toByteArray()) , d: binstrToB64Url(k.d.toByteArray())
, p: binstrToB64(k.p.toByteArray()) , p: binstrToB64Url(k.p.toByteArray())
, q: binstrToB64(k.q.toByteArray()) , q: binstrToB64Url(k.q.toByteArray())
, dp: binstrToB64(k.dP.toByteArray()) , dp: binstrToB64Url(k.dP.toByteArray())
, dq: binstrToB64(k.dQ.toByteArray()) , dq: binstrToB64Url(k.dQ.toByteArray())
, qi: binstrToB64(k.qInv.toByteArray()) , qi: binstrToB64Url(k.qInv.toByteArray())
}; };
} }
, _forgeToPublicJwk: function (keypair) { , _forgeToPublicJwk: function (keypair) {
var k = keypair._forge || keypair._forgePublic; var k = keypair._forge || keypair._forgePublic;
return { return {
kty: "RSA" kty: "RSA"
, n: binstrToB64(k.n.toByteArray()) , n: binstrToB64Url(k.n.toByteArray())
, e: binstrToB64(k.e.toByteArray()) , e: binstrToB64Url(k.e.toByteArray())
}; };
} }
@ -140,7 +110,7 @@ var extrac = module.exports = {
} }
} }
if (keypair._forge) { if (keypair._forge || keypair._forgePublic) {
return extrac._forgeToPublicJwk(keypair); return extrac._forgeToPublicJwk(keypair);
} }

View File

@ -95,12 +95,14 @@ var ursac = module.exports = {
return; return;
} }
if (keypair.publicKeyJwk) {
keypair._ursaPublic = ursa.createPublicKeyFromComponents.apply( keypair._ursaPublic = ursa.createPublicKeyFromComponents.apply(
ursa ursa
, ursac._publicJwkToComponents(keypair.publicKeyJwk) , ursac._publicJwkToComponents(keypair.publicKeyJwk)
); );
keypair._ursaPublic.toJSON = notToJson; keypair._ursaPublic.toJSON = notToJson;
} }
}
, import: function (keypair) { , import: function (keypair) {
ursac._ursaImportJwk(keypair); ursac._ursaImportJwk(keypair);
ursac._ursaImportPem(keypair); ursac._ursaImportPem(keypair);
@ -142,7 +144,7 @@ var ursac = module.exports = {
if (keypair.publicKeyJwk) { if (keypair.publicKeyJwk) {
ursac._ursaImportPublicJwk(keypair); ursac._ursaImportPublicJwk(keypair);
return keypair._ursa.toPublicPem().toString('ascii'); return keypair._ursaPublic.toPublicPem().toString('ascii');
} }
if (keypair.privateKeyJwk) { if (keypair.privateKeyJwk) {

17
node.js
View File

@ -31,12 +31,19 @@ function create(deps) {
}; };
RSA._internal = require('./lib/node');//.create(deps); RSA._internal = require('./lib/node');//.create(deps);
RSA.thumbprint = function (jwk) { RSA._thumbprintInput = function (n, e) {
jwk = jwk.privateKeyJwk || jwk.publicKeyJwk || jwk; // #L147 const rsaThumbprintTemplate = `{"e":"%s","kty":"RSA","n":"%s"}`
if (!jwk.e || !jwk.n) { return new Buffer('{"e":"'+ e + '","kty":"RSA","n":"'+ n +'"}', 'ascii');
};
RSA.thumbprint = function (keypair) {
var publicKeyJwk = RSA.exportPublicJwk(keypair);
if (!publicKeyJwk.e || !publicKeyJwk.n) {
throw new Error("You must provide an RSA jwk with 'e' and 'n' (the public components)"); throw new Error("You must provide an RSA jwk with 'e' and 'n' (the public components)");
} }
var input = RSA.utils._forgeBytesToBuf('{"e":"'+ jwk.e + '","kty":"RSA","n":"'+ jwk.n +'"}');
var input = RSA._thumbprintInput(publicKeyJwk.n, publicKeyJwk.e);
console.log('thumbprint input', input.toString('ascii'));
var base64Digest = crypto.createHash('sha256').update(input).digest('base64'); var base64Digest = crypto.createHash('sha256').update(input).digest('base64');
return RSA.utils.toWebsafeBase64(base64Digest); return RSA.utils.toWebsafeBase64(base64Digest);
@ -87,7 +94,7 @@ function create(deps) {
} }
if (options.thumprint) { if (options.thumprint) {
keypair.thumbprint = RSA.thumbprint(keypair.privateKeyJwk /*|| keypair.publicKeyJwk*/); keypair.thumbprint = RSA.thumbprint(keypair);
} }
if (options.internal) { if (options.internal) {