Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got 'undefined' when using this library. #4

Open
hustshawn opened this issue Jul 17, 2017 · 10 comments
Open

Got 'undefined' when using this library. #4

hustshawn opened this issue Jul 17, 2017 · 10 comments

Comments

@hustshawn
Copy link

hustshawn commented Jul 17, 2017

Hi, I tried your library, and used your example code, but got undefined result instead of a JWK.

I want a JWK only contains my Public key, so I runned your code as

var fs = require('fs');
var rsaPemToJWK = require('rsa-pem-to-jwk');

var pem = fs.readFileSync('pubk.pem');
var JWK = rsaPemToJWK(pem, {use: 'sig'}, 'public');
console.log(JWK)  // undefined

Here is my PUBLIC key(the content of pubk.pem).

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwp6e3ABsGZGTjqmUWIGP
99cXucPOoM0sbc2wxdxrXCahlqKRvxcjl+QyCQOZC3Ff3BKI0B7PF+XiU1gcbqXE
r942MlVs6GIbohPF98BIxaUX/nJFDziABfDAQ/EDTpsSJIaE/UhhoWdTjU/XG5nz
4dQZWTKaw7SdD2EvjH+4uG1KbyOavLeeqjrItItIEedHeaXYSIy7P+6N+yZ6qYkW
ALnpm+Lqi0sxrPZDuiQ5Bq4bdYvaKOMBxbHzah5FOZF3bxPLVBjzOH0PtxvUdgLk
9sQ1Hl3k5LbEPrbG709ssEjO5pKJZwx21M6yrxAIZJjLLz6t81s/e0Lx55kTu4h9
QwIDAQAB
-----END PUBLIC KEY-----

Any ideas about this ?

@modu
Copy link

modu commented Jul 19, 2017

I am facing exactly the same issue. I tried using 'private' in place of 'public' for generating var JWK. Same issue.

@hustshawn
Copy link
Author

Hey @modu, I found another Python package to generate JWK and it works.
http://jwcrypto.readthedocs.io/en/latest/jwk.html#examples

@abalmos
Copy link
Member

abalmos commented Jul 19, 2017

This library only supports the RSAPublicKey format. More info can be found here https://github.com/OADA/rsa-pem-to-jwk/blob/master/README.md#pem-format

You should think about using a better implementation as this project uses a quick hack. Since we wrote this pem-jwk was released. It uses asn1.js to read pem files with more success. We now use pem-jwk in our projects.

@steowens
Copy link

Yea this project is broken.

@mattbrictson
Copy link

Make sure there is a newline at the end of your PEM block. It took a few tries but that seemed to be the trick. This works for me:

const rsaPemToJwk = require("rsa-pem-to-jwk");
const pem = `-----BEGIN RSA PRIVATE KEY-----
...
...
...
-----END RSA PRIVATE KEY-----
`;
rsaPemToJwk(pem, {use: "sig"}, "public");
// => { kty: 'RSA', use: 'sig', ... }

@tncowart
Copy link

tncowart commented Aug 6, 2019

Instead of -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY----- you have to have -----BEGIN RSA PUBLIC KEY----- and -----END RSA PUBLIC KEY-----

@ghost
Copy link

ghost commented Mar 5, 2021

Fixed in #6.

@DamianGuilisasti
Copy link

A possible solution could be to add a new line at the end of the PEM file:

  const privateKey = fs.readFileSync(`${__dirname}/../../certs/private.pem`);

  const certString = privateKey.toString("binary"); //Convert from buffer to string

  const cert = certString + "\n"; //Add a new line at the end of the file

  const jwk = rsaPemToJwk(cert, { use: "sig" }, "public");

console.log(jwk);


@jasonycw
Copy link

Seems like this library is using a very old outdated package (https://www.npmjs.com/package/rsa-unpack)
And the expected private key must start with ----BEGIN RSA (PRIVATE|PUBLIC) KEY-----
I made this library works with private key generated with

ssh-keygen -t rsa -b 2048 -m PEM -f private.pem

But not with openssl, since the key started with -----BEGIN PRIVATE KEY----- or -----BEGIN ENCRYPTED PRIVATE KEY-----

@mfechner
Copy link

mfechner commented Jun 7, 2023

This library only supports the RSAPublicKey format. More info can be found here https://github.com/OADA/rsa-pem-to-jwk/blob/master/README.md#pem-format

You should think about using a better implementation as this project uses a quick hack. Since we wrote this pem-jwk was released. It uses asn1.js to read pem files with more success. We now use pem-jwk in our projects.

You should maybe make this clear in the README.md that it is better to use pem-jwk then this lib. This will save many people a lot of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants