Node.js 中的 RS256 算法未生成 jsonwebtoken 令牌

jsonwebtoken token not generated with RS256 algorithm in Node.js

我在 node.js 中使用了 jsonwebtoken 包来生成令牌。如果我使用 HS256 / HS384 / HS512 算法,一切正常。但是当我使用 RS256 / PS256 / ES256 等其他算法时,它会抛出错误。我想知道我做错了什么。提前致谢。

jwt.sign({ foo: 'bar' }, 'this is a secret key', { algorithm: 'RS256' }, function (err, token) {
          if (err) {
            console.log(err)
          }
          console.log(token);
        });

错误:

Error: error:0909006C:PEM routines:get_name:no start line
    at Sign.sign (internal/crypto/sig.js:84:29)
    at Object.sign (D:\Git\contact-node\node_modules\jwa\index.js:152:45)
    at jwsSign (D:\Git\contact-node\node_modules\jws\lib\sign-stream.js:32:24)
    at SignStream.sign (D:\Git\contact-node\node_modules\jws\lib\sign-stream.js:58:21)
    at SignStream.<anonymous> (D:\Git\contact-node\node_modules\jws\lib\sign-stream.js:46:12)
    at Object.onceWrapper (events.js:299:28)
    at DataStream.emit (events.js:210:5)
    at DataStream.<anonymous> (D:\Git\contact-node\node_modules\jws\lib\data-stream.js:32:12)
    at processTicksAndRejections (internal/process/task_queues.js:75:11) {
  library: 'PEM routines',
  function: 'get_name',
  reason: 'no start line',
  code: 'ERR_OSSL_PEM_NO_START_LINE'
}

显然,RS256 算法和您指定的其他算法是非对称算法。您必须生成两个不同的密钥(public 和私钥)并使用私钥进行编码并使用 public 进行解码。所以使用生成的密钥对。参考 生成密钥。而HS256是一种对称算法,所以只用了一个秘钥。