1

I created a private and a public keys using these commands = "ssh-keygen -q -t rsa -N '' -P "" -b 4096 -m PEM -f private.key openssl rsa -in private.key -pubout -outform PEM -out private.key.pub". Then I used the private key to sign the jwt token. Here I wrote the the sign method:

const privateKey = fs.readFileSync("private.key");
const token = jwt.sign(
      { payload: "text" },
      privateKey,
      { algorithm: 'RS256' },);

However I got some the error secretOrPrivateKey must be an asymmetric key when using RS256, however if I try again to call the function it works as expected. I did not understand why the first time it does not work.

I tried the other solution on stackoverflow but I could not solve. How should I solve this? Why is it happening? I am using ubuntu and my Node.js version is 16.13.00

FaFa
  • 358
  • 2
  • 16
  • If the key is the same then the error message will be the same (I'd guess the private key is not in a format `jwt` . This *probably* isn't enough information to reproduce the problem. – President James K. Polk May 29 '23 at 16:05
  • @PresidentJamesK.Polk I get that error only the first time, the second time I send the request it works. This is the weird part. – FaFa May 30 '23 at 07:15

1 Answers1

0

I had the same error on my Windows laptop. The exception threw at line 110 (code is: "secretOrPrivateKey = createPrivateKey(secretOrPrivateKey)") in the "sign.js" file of the jsonwebtoken library. I saw that there was an exception - "error:25078067:DSO support routines:win32_load:could not load the shared library nodejs". I updated Node to the latest stable version - 18.16.0 and IT HELPED!