I cannot use ursa anymore at node v10. So I'm trying to move to native crypto.
But the problem is, native crypto seems not support create public key from components.
So, why don't you support create public key from components method?
const ursa = require('ursa');
// modulus should be a base64/base64Url string
const modulus = new Buffer(modulusStr, 'base64');
// exponent should be base64/base64url
const exponent = new Buffer('AQAB', 'base64');
const pubKey = ursa.createPublicKeyFromComponents(modulus, exponent);
console.info(pubKey.toPublicPem('utf8'));
// ------BEGIN PUBLIC KEY------
// ...
const nodeRSA = require('node-rsa');
const key = new nodeRSA();
const modulus = new Buffer(modulusStr, 'base64');
const exponent = new Buffer('AQAB', 'base64');
const pubKey = key.importKey({ n: modulus, e: exponent }, 'components-public');
console.info(pubKey.exportKey('pkcs8-public-pem'));
// ------BEGIN PUBLIC KEY------
// ...
@nodejs/crypto
This came up in a discussion around the key object API recently. Currently, the only way to do this is to manually create an SPKI structure. That might change in the future, though.
@tniessen Can you clarify? I assume you're not talking about openssl's NETSCAPE_SPKI.
There's DSA_set0_key() and RSA_set0_key(). I'd be okay with adding bindings for those. We already use/expose DH_set0_key().
It might get a bit awkward if we also want to support EC keys through the same API because they're quite different from RSA and DSA keys, and because there's some overlap with the ECDH class.
(ECDH can generate keys from an arbitrary private key. 'Arbitrary' in the sense that the private key is just a bignum encoded as a buffer.)
On the other hand, DSA and RSA keys have well-defined formats. It's not hard to write a npm module that takes some BigInts and spits out PEM. There's no real reason it should live in core except convenience.
Sorry, Ben, I must have missed the notitfication.
Can you clarify? I assume you're not talking about openssl's
NETSCAPE_SPKI.
I meant this, where PEM would encode a SubjectPublicKeyInfo (SPKI) structure:
On the other hand, DSA and RSA keys have well-defined formats. It's not hard to write a npm module that takes some BigInts and spits out PEM. There's no real reason it should live in core except convenience.
I think this discussion should be aligned with the more recent comments in https://github.com/nodejs/node/issues/15113 regarding JWK support. JWK recommends support for EC, by the way.
@belldoor Is there anyway to do this now ?
@jainendra You can use https://www.npmjs.com/package/bursar (I wrote it a while ago in response to this issue but I forgot to publish it... Done just now.)
Some examples here: https://github.com/bnoordhuis/node-bursar/blob/9447b188b9f424a72dac5a318a1ffe6596057822/test.js#L30-L85
I am considering extending #30045 with a .fields property to retrieve components, and adding a KeyObject.from({ type, asymmetricKeyType, params: { ... }, fields: { ... } }) function, but I am open to other ideas, too. (cc @panva)
@tniessen sounds great!
Most helpful comment
I am considering extending #30045 with a
.fieldsproperty to retrieve components, and adding aKeyObject.from({ type, asymmetricKeyType, params: { ... }, fields: { ... } })function, but I am open to other ideas, too. (cc @panva)