我在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'
}
答案 0 :(得分:0)
显然,RS256算法和您指定的其他算法都是不对称算法。您必须生成两个不同的密钥(公共密钥和私有密钥),然后使用私有密钥进行编码,并使用公共密钥进行解码。因此,请使用生成的密钥对。请参考How to create public and private key with openssl?生成密钥。 HS256是一种对称算法,因此仅使用一个密钥。