Karma Doe Karma Doe - 4 months ago 49
Node.js Question

How to create a Key Object in node js?

I´ve been trying for the last few days to Decrypt RSA base64 strings in node.
But most of the modules are poorly documented or I´m missing something.
The problem is that the private key is passphrase protected and therefore to use crypto.privateDecrypt(private_key, buffer) the key is needed to be inserted as an object.

private_key can be an object or a string. If private_key is a string, it is treated as the key with no passphrase and will use RSA_PKCS1_OAEP_PADDING. If private_key is an object, it is interpreted as a hash object with the keys:

key : <String> - PEM encoded private key
passphrase : <String> - Optional passphrase for the private key
padding : An optional padding value, one of the following:
crypto.constants.RSA_NO_PADDING
crypto.constants.RSA_PKCS1_PADDING
crypto.constants.RSA_PKCS1_OAEP_PADDING


All paddings are defined in crypto.constants.

Since node js is not my native framework, is anyone kind enough to tell me how said object is created ? I already have the PEM string and the passphrase needed.

Thanks in advance.

Answer

Nodejs is not really a framework, more a Javascript engine...
I guess you can use pure Javascript here:

private_key = {
    key: 'PEM encoded private key',
    passphrase: 'Optional passphrase for the private key',
    padding: crypto.constants.RSA_PKCS1_OAEP_PADDING
}