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

1

There are 1 best solutions below

0
Andrew Androsow On

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!